generic-programming

Method with generic return type but not generic input. Is this possible?

五迷三道 提交于 2019-12-10 18:45:44
问题 Suppose we have a NodeData class: public class NodeData<T> { public string Name; public T Value; public NodeData(string name, T value) { this.Name = name; this.Value = value; } } And a base Node class and child classes that have several properties with type NodaData : public class Node { public List<NodeData<T>> listOutputs<T>() { var fieldInfos = GetType().GetFields(); var list = new List<NodeData<T>>(); foreach (var item in fieldInfos) { Type t = item.FieldType; string name = item.Name; if

C++ Why does error “no matching function” appear when it 100% looks like they match?

妖精的绣舞 提交于 2019-12-10 17:14:39
问题 I don't understand why I'm getting an error that states my function doesn't match my defined template function. To me, they look exactly the same. Here is the error in my debug: error: no matching function for call to 'mergesort' newVec = mergesort(vec.begin(),vec.end()); So I can learn and write better generic functions and templates, what do I need to change to make that error go away? (Just to be clear, I'm not asking for help with my mergesort algorithm - I know it has problems, but I'll

Generic OpenCL stencil kernel and host

喜夏-厌秋 提交于 2019-12-10 15:14:25
问题 I am new to OpenCL. I would like to write a generic kernel so later I can extend its use to other memory non-coalescing patterns and pairing this with Rectangular stencil pattern for simplicity (also avoiding out-of-bound access). This kernel controls the use of local memory ( __local float ∗lmem ). As of now, I have structures my .cl file as bellow: __kernel void kmain ( __global float ∗in , __global float ∗out , __global float ∗in2 , __local float ∗lmem) { int wg_x = get group id(0); int wg

How to convert generic potentially nested map Map[String, Any] to case class using any library in Scala?

喜夏-厌秋 提交于 2019-12-10 12:38:17
问题 I've not had much joy with reflection, this answer using shapeless works for some cases (but seems to have many edge cases) Shapeless code to convert Map[String, Any] to case class cannot handle optional substructures Does anyone know of a nice library that does this in just a few LOCs? 回答1: Using jackson: libraryDependencies += "com.fasterxml.jackson.core" % "jackson-databind" % "2.9.8" libraryDependencies += "com.fasterxml.jackson.module" %% "jackson-module-scala" % "2.9.8" case class Foo(a

How to write below logic in Generic way?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-10 09:49:39
问题 I have a model like below public sealed class Person { public string MobileNo { get; set; } public string Firstname { get; set; } public string Lastname { get; set; } } In my implmentation class, I have a method which takes an IEnumerable as a parameter public string PersonList(string listName, IEnumerable<Person> persons) { dictionary.Add("name", new String[1] { listname }); dictionary.Add("list", persons.ToArray()); PrivateMethod("personList", dictionary); } I have another private method

Is it possible to partially apply nth parameter in Haskell?

大兔子大兔子 提交于 2019-12-10 02:46:56
问题 I am curious if it is possible to write a function apply_nth that takes a function, the number of a parameter, and that parameter's value and then returns a new, partially-applied function. The feeling I get is that this is impossible due to the type system, but I can't come up with a satisfying answer. I also can't come up with a working type signature. If the language were more loosely-typed, I imagine the code might look like this. apply_nth f 0 x = f x apply_nth f n x = \a -> apply_nth (f

Generic Type Argument checked by Class Parameter can be hacked, any better ways?

给你一囗甜甜゛ 提交于 2019-12-09 03:21:50
问题 Consider the class: class OnlyIntegerTypeAllowed<T> { OnlyIntegerTypeAllowed(Class<T> clazz) { System.out.println(clazz); if (clazz != Integer.class) throw new RuntimeException(); } } It is designed to accept only Type Arguments of Integer . We have added a if-throw check in its constructor. This is a very common way to check the Type Arguments. However, this checking can be bypassed (hacked, fooled) by: OnlyIntegerTypeAllowed<Integer> normalWay = new OnlyIntegerTypeAllowed<Integer>(Integer

Deep Copy of a Generic Type in Java

南笙酒味 提交于 2019-12-08 19:46:37
问题 How does deep copies (clones) of generic types T, E work in Java? Is it possible? E oldItem; E newItem = olditem.clone(); // does not work 回答1: The answer is no. Cause there is no way to find out which class will replace your generic type E during compile time, unless you Bind it to a type. Java way of cloning is shallow, for deep cloning, we need to provide our own implementation The work-around for it, is to create a contract like this public interface DeepCloneable { Object deepClone(); }

Any suggestion for doing an arbitrary operation using given arguments of arbitrary types?

怎甘沉沦 提交于 2019-12-08 19:46:27
问题 Basically i just want to do an arbitrary operation using given arguments of arbitrary types. Argument type base class is Var, and Operation is base class of the operation that will executed for given arguments. I have Evaluator class, that hold a collection of operators which mapped using opId. Evaluator will do operation based on opId argument given in evaluate() member function, then evaluate() function will do search for supported operator that will accept argument type and opId. what I

Template specialization for fundamental types

て烟熏妆下的殇ゞ 提交于 2019-12-08 17:45:23
问题 Is there any way to make a template specialization for fundamental types only? I have tried to do the following: template<typename T, typename = typename std::enable_if<!std::is_fundamental<T>::value>::type> class foo { } template<typename T, typename = typename std::enable_if<std::is_fundamental<T>::value>::type> class foo { } But I'm getting an error that the template is already defined. 回答1: Here you are creating two templated classes with the same name, not specializations. You need to