generics

Scala's Nothing vs partial unification

て烟熏妆下的殇ゞ 提交于 2021-02-08 06:38:34
问题 I would expect the following code to compile just fine: trait Widen[M[_]] { def widen[A, B >: A](ma: M[A]): M[B] } object Widen { implicit class Ops[M[_], A](ma: M[A]) { def widen[B >: A](implicit ev: Widen[M]): M[B] = ev.widen[A, B](ma) } // implicit class OpsNothing[M[_]](ma: M[Nothing]) { // def widen[B](implicit ev: Widen[M]): M[B] = ev.widen(ma) // } implicit val WidenList = new Widen[List] { def widen[A, B >: A](l: List[A]): List[B] = l } } import Widen._ List.empty[Some[Int]].widen

How can I implement a generic struct that manages key-value pairs for UserDefaults in Swift?

十年热恋 提交于 2021-02-08 04:44:30
问题 How would one implement a struct that manages UserDefaults mappings in Swift? Right now I have some computed properties a, b, c, d of different types and corresponding keys that look like this: enum UserDefaultsKeys { a_key b_key ... } var a: String { get { UserDefaults.standard.string(forKey: UserDefaultsKeys.a_key.rawValue) } set { UserDefaults.standard.set(newValue, forKey: UserDefaultsKeys.a_key.rawValue) } } var b: Int { get { UserDefaults.standard.integer(forKey: UserDefaultsKeys.b_key

Using Comparable to compare generic variables

懵懂的女人 提交于 2021-02-08 04:37:27
问题 For one of the Homeworks in my class, we have a collection of a class titled Pair and we need to sort it in ascending order based on the value of the key. I could apply this if the keys were strings or integers, but how do I write code that would compare my items when they're Generic as seen below? The professor in my class explained what to do for integers or strings but when my variables are generic I'm at a complete loss. Below are copies of the relevant parts of my code. import java.util.

Scala : parameterize a type with one of its inner types

谁说我不能喝 提交于 2021-02-08 04:18:31
问题 I would like to parameterize a type with one of its subclasses. Consider the following: class DataLoader { class Data { /* data specifics to this data loader */ } def getData : Data /* and so on */ } Now I want to make this loader able to asynchronously retrieve data from the network. One of the options is to have it subclass Callable. class DataLoader extends Callable[Data] { class Data { /* ... */ } def call : Data = { ... } } val futureData = executor.submit(new DataLoader) futureData.get

C# get the the type Generic<T> given T

橙三吉。 提交于 2021-02-07 22:16:08
问题 I have a generic class in C#, like this: public class GenericClass<T> { ... } Now, I have the Type object for an object, and would like to, through reflection or otherwise, to get the Type object for GenericClass<T> where T corresponds to that Type object I have my object. Like this: Type requiredT = myobject.GetType(); Type wantedType = typeof(GenericClass<requiredT>); Obviously this syntax doesn't work, but how do I do it? 回答1: Yes, you can: Type requiredT = ... Type genericType = typeof

C# get the the type Generic<T> given T

自古美人都是妖i 提交于 2021-02-07 22:12:09
问题 I have a generic class in C#, like this: public class GenericClass<T> { ... } Now, I have the Type object for an object, and would like to, through reflection or otherwise, to get the Type object for GenericClass<T> where T corresponds to that Type object I have my object. Like this: Type requiredT = myobject.GetType(); Type wantedType = typeof(GenericClass<requiredT>); Obviously this syntax doesn't work, but how do I do it? 回答1: Yes, you can: Type requiredT = ... Type genericType = typeof

ObjectMapper using TypeReference not working when passed type in generic method

≯℡__Kan透↙ 提交于 2021-02-07 20:53:14
问题 This is the method: protected <T> TestPageResult<T> getTestPageResutForRequest(MockHttpServletRequestBuilder request) throws Exception { String responseJson = mockMvc.perform(request).andReturn().getResponse() .getContentAsString(); TestPageResult<T> response = getObjectMapper().readValue(responseJson, new TypeReference<TestPageResult<T>>() { }); return response; } I call it like this: TestPageResult<SomeDto> pageResult = this.<SomeDto>getTestPageResutForRequest(getRequest()); TestPageResult

ObjectMapper using TypeReference not working when passed type in generic method

怎甘沉沦 提交于 2021-02-07 20:53:04
问题 This is the method: protected <T> TestPageResult<T> getTestPageResutForRequest(MockHttpServletRequestBuilder request) throws Exception { String responseJson = mockMvc.perform(request).andReturn().getResponse() .getContentAsString(); TestPageResult<T> response = getObjectMapper().readValue(responseJson, new TypeReference<TestPageResult<T>>() { }); return response; } I call it like this: TestPageResult<SomeDto> pageResult = this.<SomeDto>getTestPageResutForRequest(getRequest()); TestPageResult

ObjectMapper using TypeReference not working when passed type in generic method

雨燕双飞 提交于 2021-02-07 20:53:00
问题 This is the method: protected <T> TestPageResult<T> getTestPageResutForRequest(MockHttpServletRequestBuilder request) throws Exception { String responseJson = mockMvc.perform(request).andReturn().getResponse() .getContentAsString(); TestPageResult<T> response = getObjectMapper().readValue(responseJson, new TypeReference<TestPageResult<T>>() { }); return response; } I call it like this: TestPageResult<SomeDto> pageResult = this.<SomeDto>getTestPageResutForRequest(getRequest()); TestPageResult

How to cast object to Action<T>

六月ゝ 毕业季﹏ 提交于 2021-02-07 20:31:40
问题 I've created a simple message bus that queues and emits/publishes events. I'm using StructureMap to locate the registered handlers ( Action<T> ) of the event but am not sure how to cast it from the object that StructureMap returns into an invoke-able action. Since I can't cast to Action<object> I'm assuming that Action<T> is not covariant? Can this be done another way? public class Bus { private ConcurrentQueue<object> events = new ConcurrentQueue<object>(); public void Queue<TEvent>(TEvent e