covariance

Why is void not covariant in Java?

可紊 提交于 2019-12-21 09:14:55
问题 If I have this interface: public interface Foo { void bar(); } Why can't I implement it like this? public class FooImpl implements Foo { @Override public Object bar() { return new Object(); } } It seems like void should be covariant with everything. Am I missing something? Edit: I should have been clearer that I'm looking for the design justification, not the technical reason that it won't compile. Are there negative consequences to making void covariant to everything? 回答1: Technically void

Shouldn't ILookup<TKey, TElement> be (declared) covariant in TElement?

心已入冬 提交于 2019-12-21 03:20:08
问题 The definition System.Linq.ILookUp reads interface ILookup<TKey, TElement> : IEnumerable<IGrouping<TKey, TElement>>, IEnumerable { int Count { get; } IEnumerable<TElement> this[TKey key] { get; } bool Contains(TKey key); } Since IEnumerable is covariant in IGrouping<TKey, TElement>, IGrouping<TKey, TElement> is covariant in TElement and the interface only exposes TElement as a return type, I would assume that ILookup is also covariant in TElement. Indeed, the definition interface IMyLookup

Why do I need an Interface for Covariance (out Type)?

时光总嘲笑我的痴心妄想 提交于 2019-12-20 02:59:16
问题 I just need to use the covariant out generic type modifier again. I had a class with a generic type and wanted to add an out but VS told me that this is only possible on interfaces. But why can I use the out modifier only on an interface? I helped myself in creating an interface copy of my class but this seems a little bit strange to me to only have an interface so I can use this modifier. 回答1: It's extremely difficult to ensure that the class's definition is in fact covariant. It is much

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

白昼怎懂夜的黑 提交于 2019-12-19 17:44:11
问题 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 {

How can i cast into a ObservableCollection<object>

醉酒当歌 提交于 2019-12-19 13:46:15
问题 How can i cast from ObservableCollection<TabItem> into ObservableCollection<object> this doesnt work for me (ObservableCollection<object>)myTabItemObservableCollection 回答1: you should copy like this return new ObservableCollection<object>(myTabItemObservableCollection); 回答2: 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>

method return value covariance for primitives. Is it works?

对着背影说爱祢 提交于 2019-12-19 11:59:14
问题 **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? 回答1: 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

Why are arrays covariant but generics are invariant?

*爱你&永不变心* 提交于 2019-12-19 11:57:29
问题 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

Numpy Cholesky decomposition LinAlgError

孤街醉人 提交于 2019-12-19 06:42:50
问题 In my attempt to perform cholesky decomposition on a variance-covariance matrix for a 2D array of periodic boundary condition, under certain parameter combinations, I always get LinAlgError: Matrix is not positive definite - Cholesky decomposition cannot be computed . Not sure if it's a numpy.linalg or implementation issue, as the script is straightforward: sigma = 3. U = 4 def FromListToGrid(l_): i = np.floor(l_/U) j = l_ - i*U return np.array((i,j)) Ulist = range(U**2) Cov = [] for l in

Numpy Cholesky decomposition LinAlgError

痴心易碎 提交于 2019-12-19 06:42:01
问题 In my attempt to perform cholesky decomposition on a variance-covariance matrix for a 2D array of periodic boundary condition, under certain parameter combinations, I always get LinAlgError: Matrix is not positive definite - Cholesky decomposition cannot be computed . Not sure if it's a numpy.linalg or implementation issue, as the script is straightforward: sigma = 3. U = 4 def FromListToGrid(l_): i = np.floor(l_/U) j = l_ - i*U return np.array((i,j)) Ulist = range(U**2) Cov = [] for l in

PyMC - variance-covariance matrix estimation

断了今生、忘了曾经 提交于 2019-12-19 04:59:51
问题 This question was migrated from Cross Validated because it can be answered on Stack Overflow. Migrated 5 years ago . I read the following paper(http://www3.stat.sinica.edu.tw/statistica/oldpdf/A10n416.pdf) where they model the variance-covariance matrix Σ as: Σ = diag(S)*R*diag(S) (Equation 1 in the paper) S is the k×1 vector of standard deviations, diag(S) is the diagonal matrix with diagonal elements S, and R is the k×k correlation matrix. How can I implement this using PyMC ? Here is some