generics

How to implicitly figure out the type at the head of a shapeless HList

空扰寡人 提交于 2021-02-05 12:04:49
问题 Lets say I have the following: case class TestField(value: String) case class TestField2(value: String) implicit class ProductExtensions[T <: Product](val value T) extends AnyVal { def mapTo[R <: Product](implicit tGen: Generic.Aux[T, String :: HNil], rGen: Generic.Aux[R, String :: HNil]: R = ??? } val testField2 = TestField("my value").mapTo[TestField2] // TestField2("my value") Can I "genersize" the mapTo function to work for types other than String without having to specify the type? Note

How to implicitly figure out the type at the head of a shapeless HList

佐手、 提交于 2021-02-05 12:03:40
问题 Lets say I have the following: case class TestField(value: String) case class TestField2(value: String) implicit class ProductExtensions[T <: Product](val value T) extends AnyVal { def mapTo[R <: Product](implicit tGen: Generic.Aux[T, String :: HNil], rGen: Generic.Aux[R, String :: HNil]: R = ??? } val testField2 = TestField("my value").mapTo[TestField2] // TestField2("my value") Can I "genersize" the mapTo function to work for types other than String without having to specify the type? Note

How do I use reflection to call a generic method?

好久不见. 提交于 2021-02-05 11:48:08
问题 What's the best way to call a generic method when the type parameter isn't known at compile time, but instead is obtained dynamically at runtime? Consider the following sample code - inside the Example() method, what's the most concise way to invoke GenericMethod<T>() using the Type stored in the myType variable? public class Sample { public void Example(string typeName) { Type myType = FindType(typeName); // What goes here to call GenericMethod<T>()? GenericMethod<myType>(); // This doesn't

How do I use reflection to call a generic method?

久未见 提交于 2021-02-05 11:47:15
问题 What's the best way to call a generic method when the type parameter isn't known at compile time, but instead is obtained dynamically at runtime? Consider the following sample code - inside the Example() method, what's the most concise way to invoke GenericMethod<T>() using the Type stored in the myType variable? public class Sample { public void Example(string typeName) { Type myType = FindType(typeName); // What goes here to call GenericMethod<T>()? GenericMethod<myType>(); // This doesn't

Java generics class cast exception

好久不见. 提交于 2021-02-05 11:38:51
问题 I am trying to create a class that processes comparables. I boiled down to the simplest code that gives an error when I try to instantiate the class. I get a couple of compile warnings (unchecked cast) but when I run this program it throws a classcast exception. I did look at some of the other questions on this topic but didnt come across something useful. public class GD<Item extends Comparable<Item>> { private Item[] data; private final int MAX_SIZE = 200; public GD() { data = (Item[]) new

T <: A, return T method

大憨熊 提交于 2021-02-05 11:33:58
问题 here is some sample code: trait A trait B extends A def test[T <: A](): T = { new B {} } but I get an compile error: type mismatch; found : B required: T new B {} how to make it working ? ( but without doing asInstanceOf[T] at the end ) thanks! 回答1: The signature of your method def test[T <: A](): T promises that for any type T that is a subtype of A you return a value of this type T . And then you returned a value of type B . You violated the signature (there are many subtypes of A , not

T <: A, return T method

痞子三分冷 提交于 2021-02-05 11:33:05
问题 here is some sample code: trait A trait B extends A def test[T <: A](): T = { new B {} } but I get an compile error: type mismatch; found : B required: T new B {} how to make it working ? ( but without doing asInstanceOf[T] at the end ) thanks! 回答1: The signature of your method def test[T <: A](): T promises that for any type T that is a subtype of A you return a value of this type T . And then you returned a value of type B . You violated the signature (there are many subtypes of A , not

Can't call extension method on generic Array class when element type is a Protocol [Swift]

折月煮酒 提交于 2021-02-05 11:21:07
问题 The following fails as per the error message quoted in the comment. It has been boiled down to the bare minimum, so the code below has no apparent practical value. I'm just trying to get a handle on the truly bizarre (in my opinion) error message. The reason I want to declare the array as [P] and not [S] is for the usual run-time polymorphism of the array contents. protocol P { func sp() } struct S: P { func sp() {} } extension Array where Element: P { func am() {} } func t() { let goodA = [S

Using static method from generic class

自古美人都是妖i 提交于 2021-02-05 11:14:26
问题 I have problem as above. My code: public abstract class BaseFactory<T> where T: class { protected static dbModelContainer context = new dbModelContainer(); public static int UpdateDataBase_static() { return context.SaveChanges(); } } and my question is how can I call BaseFactory.UpdateDataBase_static(); instead of: BaseFactory<SomeClass>.UpdateDataBase_static(); Any ideas? 回答1: You can't, because there is no such method. The closest is to have a non-generic base that the generic class

Using static method from generic class

丶灬走出姿态 提交于 2021-02-05 11:13:26
问题 I have problem as above. My code: public abstract class BaseFactory<T> where T: class { protected static dbModelContainer context = new dbModelContainer(); public static int UpdateDataBase_static() { return context.SaveChanges(); } } and my question is how can I call BaseFactory.UpdateDataBase_static(); instead of: BaseFactory<SomeClass>.UpdateDataBase_static(); Any ideas? 回答1: You can't, because there is no such method. The closest is to have a non-generic base that the generic class