generics

Constructing a n-ary product with all the values of a simple sum type

六月ゝ 毕业季﹏ 提交于 2021-01-27 21:14:10
问题 I'm working with the generics-sop library. I want to write a value with the following type: values :: forall r. IsEnumType r => NP (K r) (Code r) That is, for sum types whose constructors don't have any arguments (IsEnumType) I want to produce an n-ary product (NP) which contains the corresponding constructor value at each point. For example, for the type {-# LANGUAGE DeriveGeneric #-} import qualified GHC.Generics as GHC import Generics.SOP data Foo = Bar | Baz deriving (GHC.Generic)

F# Generic constraint to have one generic type inherit from another

半世苍凉 提交于 2021-01-27 20:10:04
问题 In C# it is straightforward to define a generic class when one generic parameter inherits from another, e.g.: public class MyClass<TClass, TInterface> where TClass : class, TInterface { } This is used to "force" the class TClass to implement the interface TInterface . I want to do the same in F# and surprisingly it does not seem to work. For example, the following code: type Startup<'S, 'I when 'I : not struct and 'S : not struct and 'S :> 'I>() = member _.x = 0 results in FS0663 - This type

ClassCastException using Generics and Varargs

落爺英雄遲暮 提交于 2021-01-27 19:54:43
问题 I got into Java Generics just recently and wanted to replicate the JavaScript map function you can use with Arrays. Now I can't figure out what's going wrong in my code: public class Test { public interface Function<T> { public T call(T... vals); } public <E> E[] map(E[] array, Function<E> func) { for (int i = 0; i < array.length; i++) { array[i] = func.call(array[i]); <--- Exception } return array; } public Test() { Integer foo[] = {3, 3, 4, 9}; foo = map(foo, new Function<Integer>() {

How to allow a generic collection to perform under the hood conversion in swift

血红的双手。 提交于 2021-01-27 19:20:08
问题 I am implementing a generic collection and I would like to make its usage with types/protocol hierarchy more convenient. I stumbled on the following question. How one does implement a collection class SpecialSet<ObjectType> { Such that given two types B : A one can perform implicite conversion of SpecialSet<B> into SpecialSet<A> ? I am suprised but I wasn't able to find any documentation explaining this simple concept that indeed exist for the Array collection. This swift code is indeed valid

Is this a Java generic type inference compiler bug? [duplicate]

北城以北 提交于 2021-01-27 17:38:37
问题 This question already has answers here : String gets assigned to a List without a compilation error [duplicate] (1 answer) Why does this generic code compile in java 8? (3 answers) Generic return type upper bound - interface vs. class - surprisingly valid code (2 answers) Closed 1 year ago . I am thinking about constrained type inference at the call site of a function. In the following snippet I get a compile error for the first inference call in the main function as expected ( String is not

Selecting objects from an ArrayList based on value of certain fields

青春壹個敷衍的年華 提交于 2021-01-27 16:46:00
问题 I have a Course class, which has fields of various types, with their respective getters and setters, such as: float percentage char courseLevel String courseName boolean takingNow I then have an ArrayList of course objects and a method: public <T> ArrayList<Course> getCourses(int parameterId, T value) { ArrayList<Course> res = new ArrayList<Course>(); switch(parameterId) { case COURSE_NAME: for(Course c: courseList) { if(c.getCourseName().equals(value) { res.add(c) } } break; .... //rest of

How do you call GetMethod for a generic function that takes a generic parameter (without using GetMethods)?

半腔热情 提交于 2021-01-27 13:45:16
问题 I know I can fetch the method info using GetMethods , but I want to know how to do it properly without GetMethods . I have read other SO questions and answers that suggest this is not possible, or suggest just using LINQ instead, but that isn't really an answer to the question. Consider at the most basic level, a static generic function that takes a single generic parameter. private static void Test<T>(T val) { } To fetch this method info we can just call Type.GetMethod("Test", BindingFlags

C# Generics with concrete implementation

隐身守侯 提交于 2021-01-27 12:12:05
问题 Is it possible in C# to create a generic method and add also a concrete implementation for given type? For example: void Foo<T>(T value) { //add generic implementation } void Foo<int>(int value) { //add implementation specific to int type } 回答1: In your specific example you wouldn't need to do this. Instead, you'd just implement a non-generic overload, since the compiler will prefer using that to the generic version. The compile time type is used to dispatch the object: void Foo<T>(T value) {

How can I use internal mutability with generic type in Rust?

家住魔仙堡 提交于 2021-01-27 11:43:11
问题 I would like to design a struct in Rust that can be constructed with an object that implements the Digest trait, and abstract the behavior of the hash behind a method. Here's a simple example that doesn't compile: use digest::Digest; struct Crypto<D: Digest> { digest: D, } impl<D> Crypto<D> where D: Digest, { pub fn hash(&self, data: &[u8]) -> Vec<u8> { self.digest.chain(&data).finalize_reset().to_vec() } } This fails to compile because self is immutably borrowed in the method signature, so

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,