generic-method

Why method defined like “cons[B >: A](v: B)” accepts argument of type which is not supertype of A?

人盡茶涼 提交于 2019-11-30 15:41:51
问题 I am studying variance in scala right now, and I think I have a good understanding of contravariance. For example given trait List[-A] , I know that List[Int] is a supertype of List[AnyVal] . But say that I have the following trait: trait List[+A] { def cons(hd: A): List[A] } Why is cons parameter type wrong? Why it is necessary to have def cons[B >: A](v: B): List[B] ? For example: val animal_list: List[Animal] = List(tiger, dog) if we call: animal_list.cons(tiger) since Tiger <: Animal ,

Why method defined like “cons[B >: A](v: B)” accepts argument of type which is not supertype of A?

放肆的年华 提交于 2019-11-30 15:00:13
I am studying variance in scala right now, and I think I have a good understanding of contravariance. For example given trait List[-A] , I know that List[Int] is a supertype of List[AnyVal] . But say that I have the following trait: trait List[+A] { def cons(hd: A): List[A] } Why is cons parameter type wrong? Why it is necessary to have def cons[B >: A](v: B): List[B] ? For example: val animal_list: List[Animal] = List(tiger, dog) if we call: animal_list.cons(tiger) since Tiger <: Animal , doesn't cons ran into problem? Since B is Tiger and A is Animal and B >: A is not true. TeWu Why is cons

scala generic method overriding

放肆的年华 提交于 2019-11-30 05:00:45
I have an abstract class : abstract class Foo(...){ def bar1(f : Foo) : Boolean def bar2(f : Foo) : Foo } multiple classes extend Foo and override the methods class FooImpl(...) extends Foo{ override def bar1(f : Foo) : Boolean { ... } override def bar2(f : Foo) : Foo { ... } } Is it possible, using generics (or something) to make the overriding methods have the parametertype of the subclass implementing it? Like this : class FooImpl(...) extends Foo{ override def bar1(f : FooImpl) : Boolean { ... } override def bar2(f : FooImpl) : FooImpl { ... } } I was thinking something along the line of

scala generic method overriding

家住魔仙堡 提交于 2019-11-29 02:14:26
问题 I have an abstract class : abstract class Foo(...){ def bar1(f : Foo) : Boolean def bar2(f : Foo) : Foo } multiple classes extend Foo and override the methods class FooImpl(...) extends Foo{ override def bar1(f : Foo) : Boolean { ... } override def bar2(f : Foo) : Foo { ... } } Is it possible, using generics (or something) to make the overriding methods have the parametertype of the subclass implementing it? Like this : class FooImpl(...) extends Foo{ override def bar1(f : FooImpl) : Boolean

Java generic method inheritance and override rules

亡梦爱人 提交于 2019-11-28 18:39:04
I have an abstract class that has a generic method and I want to override the generic method by substituting specific types for the generic parameter. So in pseudo-code I have the following: public abstract class GetAndParse { public SomeClass var; public abstract <T extends AnotherClass> void getAndParse(T... args); } public class Implementor extends GetAndParse { // some field declarations // some method declarations @Override public <SpecificClass> void getAndParse(SpecificClass... args) { // method body making use of args } } But for some reason I'm not allowed to do this? Am I making some

C# Generic Method Without Specifying Type

谁都会走 提交于 2019-11-28 13:17:58
Ok so I'm a Java guy starting to use C# and I was coding and started making a generic method and what I wrote runs and compiles but it goes against everything I know about how generics should work so I'm hoping someone can explain this to me: So I have a generic method defined as follows: public static List<T> CopyAsList<T>(IEnumerable<T> list, Object lockObject) { if (list != null) { lock (lockObject) { return new List<T>(list); } } return null; } But the weird thing to me is that I can call this generic method without ever specifying T and it will work: List<String> strings = new List<string

Use Reflection to call generic method on object instance with signature: SomeObject.SomeGenericInstanceMethod<T>(T argument)

天大地大妈咪最大 提交于 2019-11-28 02:10:51
How do I call SomeObject.SomeGenericInstanceMethod<T>(T arg) ? There are a few posts about calling generic methods, but not quite like this one. The problem is that the method argument parameter is constrained to the generic parameter. I know that if the signature were instead SomeObject.SomeGenericInstanceMethod<T>(string arg) then I could get the MethodInfo with typeof (SomeObject).GetMethod("SomeGenericInstanceMethod", new Type[]{typeof (string)}).MakeGenericMethod(typeof(GenericParameter)) So, How do I go about getting the MethodInfo when the regular arguments are of a generic type? Thanks

C# Generic Method Without Specifying Type

老子叫甜甜 提交于 2019-11-27 07:36:16
问题 Ok so I'm a Java guy starting to use C# and I was coding and started making a generic method and what I wrote runs and compiles but it goes against everything I know about how generics should work so I'm hoping someone can explain this to me: So I have a generic method defined as follows: public static List<T> CopyAsList<T>(IEnumerable<T> list, Object lockObject) { if (list != null) { lock (lockObject) { return new List<T>(list); } } return null; } But the weird thing to me is that I can call

Java generic method inheritance and override rules

浪子不回头ぞ 提交于 2019-11-27 00:53:36
问题 I have an abstract class that has a generic method and I want to override the generic method by substituting specific types for the generic parameter. So in pseudo-code I have the following: public abstract class GetAndParse { public SomeClass var; public abstract <T extends AnotherClass> void getAndParse(T... args); } public class Implementor extends GetAndParse { // some field declarations // some method declarations @Override public <SpecificClass> void getAndParse(SpecificClass... args) {

Why can this generic method with a bound return any type?

烈酒焚心 提交于 2019-11-26 20:13:48
Why does the following code compile? The method IElement.getX(String) returns an instance of the type IElement or of subclasses thereof. The code in the Main class invokes the getX(String) method. The compiler allows to store the return value to a variable of the type Integer (which obviously is not in the hierarchy of IElement ). public interface IElement extends CharSequence { <T extends IElement> T getX(String value); } public class Main { public void example(IElement element) { Integer x = element.getX("x"); } } Shouldn't the return type still be an instance of IElement - even after the