covariance

Can I force a trait to be covariant?

跟風遠走 提交于 2021-01-27 16:57:11
问题 Thanks to @francis-gagné 's excellent answer to another question, I have a clearer view of how variance works. For example, a type containing a reference is covariant over its lifetime parameter, as demonstrated below. struct Foo<'a> (PhantomData<&'a str>); /// Foo is covariant over its lifetime parameter pub fn test_foo<'a:'b, 'b:'c, 'c>() { let fa: Foo<'a> = Foo(PhantomData); let fb: Foo<'b> = Foo(PhantomData); let fc: Foo<'c> = Foo(PhantomData); let v: Vec<Foo<'b>> = vec![fa, fb]; // fc is

Can I force a trait to be covariant?

女生的网名这么多〃 提交于 2021-01-27 16:51:27
问题 Thanks to @francis-gagné 's excellent answer to another question, I have a clearer view of how variance works. For example, a type containing a reference is covariant over its lifetime parameter, as demonstrated below. struct Foo<'a> (PhantomData<&'a str>); /// Foo is covariant over its lifetime parameter pub fn test_foo<'a:'b, 'b:'c, 'c>() { let fa: Foo<'a> = Foo(PhantomData); let fb: Foo<'b> = Foo(PhantomData); let fc: Foo<'c> = Foo(PhantomData); let v: Vec<Foo<'b>> = vec![fa, fb]; // fc is

The type cannot be used as type parameter 'T' in the generic type or method 'BaseController<T>'. There is no implicit reference

耗尽温柔 提交于 2021-01-27 07:51:32
问题 I'm trying to create a generic to simplify my codes (it's a web api project), but at somehow it's ended up becoming more complicated than I expected. What I'm trying to implement is something like this: To simplify my whole real code, this is what I've written: public interface IDatabaseTable { } public class ReceiptIndex: IDatabaseTable { } public interface IBackend<T> where T: IDatabaseTable { } public class Receipts : IBackend<ReceiptIndex> { } public class Generic<T> : SyncTwoWayXI,

The type cannot be used as type parameter 'T' in the generic type or method 'BaseController<T>'. There is no implicit reference

坚强是说给别人听的谎言 提交于 2021-01-27 07:51:03
问题 I'm trying to create a generic to simplify my codes (it's a web api project), but at somehow it's ended up becoming more complicated than I expected. What I'm trying to implement is something like this: To simplify my whole real code, this is what I've written: public interface IDatabaseTable { } public class ReceiptIndex: IDatabaseTable { } public interface IBackend<T> where T: IDatabaseTable { } public class Receipts : IBackend<ReceiptIndex> { } public class Generic<T> : SyncTwoWayXI,

The type cannot be used as type parameter 'T' in the generic type or method 'BaseController<T>'. There is no implicit reference

微笑、不失礼 提交于 2021-01-27 07:46:45
问题 I'm trying to create a generic to simplify my codes (it's a web api project), but at somehow it's ended up becoming more complicated than I expected. What I'm trying to implement is something like this: To simplify my whole real code, this is what I've written: public interface IDatabaseTable { } public class ReceiptIndex: IDatabaseTable { } public interface IBackend<T> where T: IDatabaseTable { } public class Receipts : IBackend<ReceiptIndex> { } public class Generic<T> : SyncTwoWayXI,

Calculate covariance matrix for complex data in two channels (no complex data type)

两盒软妹~` 提交于 2021-01-20 20:32:08
问题 I have complex-valued data given in 2 channels of a matrix (one is the real, one the imaginary part, so the matrix dimensions are (height, width, 2) , since Pytorch does not have native complex data types. I now want to calculate the covariance matrix. The stripped-down numpy calculation adapted for Pytorch is this: def cov(m, y=None): if m.ndimension() > 2: raise ValueError("m has more than 2 dimensions") if y.ndimension() > 2: raise ValueError('y has more than 2 dimensions') X = m if X

Calculate covariance matrix for complex data in two channels (no complex data type)

好久不见. 提交于 2021-01-20 20:31:23
问题 I have complex-valued data given in 2 channels of a matrix (one is the real, one the imaginary part, so the matrix dimensions are (height, width, 2) , since Pytorch does not have native complex data types. I now want to calculate the covariance matrix. The stripped-down numpy calculation adapted for Pytorch is this: def cov(m, y=None): if m.ndimension() > 2: raise ValueError("m has more than 2 dimensions") if y.ndimension() > 2: raise ValueError('y has more than 2 dimensions') X = m if X

Clarifying contravariance nature of the return type of a function as parameter a function of an outer convariant container

﹥>﹥吖頭↗ 提交于 2021-01-01 09:40:48
问题 In Option we have def getOrElse[B >: A](default: => B): B = this match { case None => default case Some(a) => a } def orElse[B >: A](obj: => Option[B]): Option[B] = this match { case None => obj case _ => this } In Either we have: def flatMap[EE >: E, B](f: A => Either[EE, B]): Either[EE, B] I understand what is going and why, a rather extended example could be this OrElse( { Option[B]}).map{....} If B is such that A :> B, then if Some(a) you get Some(a).map(f:B => ???) then Kaboom generally

Covariant return types, const-ness, and incomplete classes

吃可爱长大的小学妹 提交于 2020-12-29 05:13:24
问题 This code compiles successfully under g++ 6.1 but gives an error with clang 3.8: class C; class Base { public: virtual const C *getC(); }; class Derived : public Base { public: virtual C *getC(); }; The error from clang is as follows: $ dev/compilers/linux-x86_64-2.12.2/clang3.8/bin/clang++ -Wall -c testcovariantreturn.cxx testcovariantreturn.cxx:10:20: error: return type of virtual function 'getC' is not covariant with the return type of the function it overrides ('C' is incomplete) If class

Covariant return types, const-ness, and incomplete classes

杀马特。学长 韩版系。学妹 提交于 2020-12-29 05:12:08
问题 This code compiles successfully under g++ 6.1 but gives an error with clang 3.8: class C; class Base { public: virtual const C *getC(); }; class Derived : public Base { public: virtual C *getC(); }; The error from clang is as follows: $ dev/compilers/linux-x86_64-2.12.2/clang3.8/bin/clang++ -Wall -c testcovariantreturn.cxx testcovariantreturn.cxx:10:20: error: return type of virtual function 'getC' is not covariant with the return type of the function it overrides ('C' is incomplete) If class