generics

Unable to add object to generic list using wildcard [duplicate]

人盡茶涼 提交于 2020-01-07 01:21:11
问题 This question already has answers here : Can't add value to the Java collection with wildcard generic type (4 answers) Closed 5 years ago . Why can't I add any object here , I was hoping I would be allowed to add any kind of object in such a list. List<?> l = new ArrayList<Object>(); l.add(new Object()); l.add(new String("hello")); 回答1: Why not just: List<Object> l = new ArrayList<>(); l.add(new Object()); l.add(new String("hello")); 来源: https://stackoverflow.com/questions/24756464/unable-to

Swift 3 extension constrained to a type

白昼怎懂夜的黑 提交于 2020-01-06 18:57:42
问题 I'd like to extend an RXSwift protocol, namely OsbervableConvertibleType, but I only want to create an extension method only on OsbervableConvertibleTypes, that has a Result object in them. Now, Result is generic again. But I'd like to retain the generic type in my extension function, so the return type of my function is generic as well. Something like this: extension ObservableConvertibleType where E: Result<T> { public func asResultDriver() -> RxCocoa.SharedSequence<RxCocoa

Adding explicit return to closure causes compiler error: A compiler bug?

我的未来我决定 提交于 2020-01-06 18:53:21
问题 Consider this generic method (only the types are important, not what it does): func flatMap<SourceType, TargetType>(source: [SourceType], transform: SourceType [TargetType]) -> [TargetType] { return [] } Following call to the method compiles nicely: let seq = flatMap(["some", "string"], { s in [1, 2] }) However, just adding explicit return to closure cases compile error: let seq = flatMap(["some", "string"], { s in return [1, 2] }) //ERROR: Cannot convert the expression's type ... to type

Kotlin vararg of A to vararg of B

≡放荡痞女 提交于 2020-01-06 18:05:40
问题 I am trying to implement a Filter class that can be composed of other Filter classes.The goal is that each filter can process an object and return a boolean, and if it is made of several filters it computes the AND of all its filters. I am trying to get one of the constructors to take a vararg of predicates, which needs to be converted into a vararg of Filters. The compiler says that none of the functions (the primary and first secondary constructors) can be called with these parameters. My

How can I implement a method that accepts a Consumer<Optional<T>> that is contravariant in T?

元气小坏坏 提交于 2020-01-06 17:59:52
问题 In the following sample, I can pass a Consumer<Optional<Integer> to foo , but not a Consumer<Optional<Number>> . On the other hand, I can pass either type to foo2 , but then I can't call the accept method of the consumer from the method body. Is there a way to change the foo method so that this works? My initial intuition was to try void foo(Consumer<Result<? super T>> c) but that apparently doesn't mean what I would assume. import java.util.Optional; import java.util.function.Consumer;

How can I implement a method that accepts a Consumer<Optional<T>> that is contravariant in T?

北战南征 提交于 2020-01-06 17:59:15
问题 In the following sample, I can pass a Consumer<Optional<Integer> to foo , but not a Consumer<Optional<Number>> . On the other hand, I can pass either type to foo2 , but then I can't call the accept method of the consumer from the method body. Is there a way to change the foo method so that this works? My initial intuition was to try void foo(Consumer<Result<? super T>> c) but that apparently doesn't mean what I would assume. import java.util.Optional; import java.util.function.Consumer;

Scala: Copying a generic case class into another

*爱你&永不变心* 提交于 2020-01-06 17:27:55
问题 I have the following setup, where I want to copy an instance of baseData into that of moreData : sealed trait baseData { def weight: Int def priority: Int } sealed trait moreData { def weight: Int def priority: Int def t: String def id: String } case class data1(override val weight: Int, override val priority: Int) extends baseData case class moreData1 (override val weight:Int, override val priority: Int, override val t: String, override val id: String)extends moreData So copying myData into

Generics with extends

给你一囗甜甜゛ 提交于 2020-01-06 15:43:13
问题 In: public class Organic<E> { void react(E e) { } static void main(String[] args) { Organic<? extends Elem> compound = new Organic<Elem>(); compound.react(new Elem()); } } class Elem {} Why do I get the following compilation error? The method react(capture#1-of ? extends Elem) in the type Organic<capture#1-of ? extends Elem> is not applicable for the arguments ( Elem ) 回答1: From http://docs.oracle.com/javase/tutorial/extra/generics/wildcards.html List is an example of a bounded wildcard. The

“import and using may not appear after a type declaration” — the haxe using magic

♀尐吖头ヾ 提交于 2020-01-06 15:29:28
问题 I am trying to add an less-than-or-equal-to method (non-intrusively) to basic types such as Int, Float or existing/library types that I cannot change. (see my other question how to write a generic compare function in Haxe (haxe3)) I read that the "using" keyword is the way to do it. What I intend to do is this: class IntOrder { static public function le(x:Int,y:Int):Bool { return x <= y; } } class FloatOrder { static public function le(x:Float,y:Float):Bool { return x <= y; } } class

convert list<int> to list<long>

為{幸葍}努か 提交于 2020-01-06 15:24:07
问题 How to convert List<int> to List<long> in C#? 回答1: Like this: List<long> longs = ints.ConvertAll(i => (long)i); This uses C# 3.0 lambda expressions; if you're using C# 2.0 in VS 2005, you'll need to write List<long> longs = ints.ConvertAll<int, long>( delegate(int i) { return (long)i; } ); 回答2: List<int> ints = new List<int>(); List<long> longs = ints.Select(i => (long)i).ToList(); 回答3: var longs = ints.Cast<long>().ToList(); 来源: https://stackoverflow.com/questions/3296055/convert-listint-to