covariance

java cast from List<B> to List<A> where B extends A

风格不统一 提交于 2019-12-01 18:47:42
is this possible? if not, why isn't this possible in Java? interface B extends A {} public List<B> getList(); List<A> = getList(); // Type mismatch: cannot convert from List<B> to List<A> I think the topic I'm looking for is "covariant types" as here and here , but its murky and it doesn't solve my problem. Here is an intuitive example of how this can make things go horribly wrong: interface B extends A {} List<B> blist=new List<B>(); List<A> alist=blist; alist.add(new A()); //should be ok, right? B b = blist.get(0); //fail: even though blist is a List<B>, it now has an A in it Try List<?

Conversion from Func<object,string> to Func<string,string> works but to Func<int,string> fails

跟風遠走 提交于 2019-12-01 18:43:16
I have the following code: static Func<object, string> s_objToString = (x) => x.ToString(); static Func<string, string> s_stringToString = s_objToString; //compiles static Func<int, string> s_intToString = s_objToString; //error The second line compiles but the third line fails to compile with error: Cannot implicitly convert type ' System.Func<object,string> ' to ' System.Func<int,string> ' Why is that? I understand that with genetics although string is derived from object a List<string> does not derive from List<object> , but here object to string works and object to int fails, why? OK let's

Fill lower matrix with vector by row, not column

孤街醉人 提交于 2019-12-01 18:32:44
I am trying to read in a variance-covariance matrix written out by LISREL in the following format in a plain text, whitespace separated file: 0.23675E+01 0.86752E+00 0.28675E+01 -0.36190E+00 -0.36190E+00 0.25381E+01 -0.32571E+00 -0.32571E+00 0.84425E+00 0.25598E+01 -0.37680E+00 -0.37680E+00 0.53136E+00 0.47822E+00 0.21120E+01 -0.37680E+00 -0.37680E+00 0.53136E+00 0.47822E+00 0.91200E+00 0.21120E+01 This is actually a lower diagonal matrix (including diagonal): 0.23675E+01 0.86752E+00 0.28675E+01 -0.36190E+00 -0.36190E+00 0.25381E+01 -0.32571E+00 -0.32571E+00 0.84425E+00 0.25598E+01 -0.37680E

Cannot implicitly convert MyType<Foo> to MyType<IFoo>

回眸只為那壹抹淺笑 提交于 2019-12-01 17:33:49
I am not sure if this is a Covariance and Contravariance issue but I cannot get this working. Here is the code: public interface IDto { } public class PaginatedDto<TDto> where TDto : IDto { public int PageIndex { get; set; } public int PageSize { get; set; } public int TotalCount { get; set; } public int TotalPageCount { get; set; } public bool HasNextPage { get; set; } public bool HasPreviousPage { get; set; } public IEnumerable<TDto> Dtos { get; set; } } public class PersonDto : IDto { public int Id { get; set; } public string Name { get; set; } public string Surname { get; set; } public int

How can i cast into a ObservableCollection<object>

天涯浪子 提交于 2019-12-01 15:28:25
How can i cast from ObservableCollection<TabItem> into ObservableCollection<object> this doesnt work for me (ObservableCollection<object>)myTabItemObservableCollection you should copy like this return new ObservableCollection<object>(myTabItemObservableCollection); Basically, you can't. Not now, and not in .NET 4.0 . What is the context here? What do you need? LINQ has Cast<T> which can get you the data as a sequence , or there are some tricks with generic methods (i.e. Foo<T>(ObservalbleCollection<T> col) etc). Or you can just use the non-generic IList ? IList untyped = myTypedCollection;

Why are arrays covariant but generics are invariant?

懵懂的女人 提交于 2019-12-01 14:47:54
From Effective Java by Joshua Bloch, Arrays differ from generic type in two important ways. First arrays are covariant. Generics are invariant. Covariant simply means if X is subtype of Y then X[] will also be sub type of Y[]. Arrays are covariant As string is subtype of Object So String[] is subtype of Object[] Invariant simply means irrespective of X being subtype of Y or not , List<X> will not be subType of List<Y>. My question is why the decision to make arrays covariant in Java? There are other SO posts such as Why are Arrays invariant, but Lists covariant? , but they seem to be focussed

method return value covariance for primitives. Is it works?

↘锁芯ラ 提交于 2019-12-01 14:24:09
**since java 5; I know that if in the base class I write: public Number doSomething(){ ... } in the child class I can write something like this @Override public Integer doSomething(){ ... } But I have a question. If in base class method returns - primitive - array - or Collection. How can I use covariant at this case? There's no covariance between primitives. No primitive type is a sub type of any other. So you can't do this class Parent { public int method() { return 0; } } class Child extends Parent { public short method() { // compilation error return 0; } } For the same reason,

Possible to convert IQueryable<Derived> to IQueryable<Base>?

大兔子大兔子 提交于 2019-12-01 11:22:24
I know about covariance, and I know that in general it will not be possible in C# until v4.0. However I am wondering about a specific case. Is there some way of getting converting IQueryable<Derived> to IQueryable<Base> by somehow creating a wrapper class that does not actually perform a query, but can actually "pass through" a .Where<>() call? My use case is that I am trying to deal with a database schema that has many similar tables. Most of the fields are in common, and many of the common fields need to be queried on each table. I'm using LinqToSql. I was hoping to avoid duplicating all the

Parameter must be input-safe error

情到浓时终转凉″ 提交于 2019-12-01 11:00:35
Here is a piece of my code: public interface IA<in TInput> { void Method(IB<TInput> entities); } public interface IB<in T> { } I can't figure out why I get following compile error: "Parameter must be input-safe. Invalid variance: The type parameter |TInput| must be contravariantly valid on "IB< in T>". Any help will be appreciated. dasblinkenlight The designator of contravariance in C# (i.e. in ) is intuitive only at the immediate level, when you make a method that "takes in" a parameter of generic type. Internally, however, contravariance means an inversion of a relation ( Q&A with an

Parameter must be input-safe error

旧城冷巷雨未停 提交于 2019-12-01 09:40:21
问题 Here is a piece of my code: public interface IA<in TInput> { void Method(IB<TInput> entities); } public interface IB<in T> { } I can't figure out why I get following compile error: "Parameter must be input-safe. Invalid variance: The type parameter |TInput| must be contravariantly valid on "IB< in T>". Any help will be appreciated. 回答1: The designator of contravariance in C# (i.e. in ) is intuitive only at the immediate level, when you make a method that "takes in" a parameter of generic type