generic-programming

Why does 'Func<IBase>' compile while 'Func<TGeneric> where TGeneric : IBase' doesn't?

旧时模样 提交于 2019-12-08 09:54:04
问题 Why is the following bloc wrong? public interface IBase { } public class ClassX : IBase { } public class ClassY { public static ClassX FunctionReturnX() { return new ClassX(); } } public class ClassZ<TGeneric> where TGeneric : IBase { Func<IBase> funcInterface = ClassY.FunctionReturnX; //Right Func<TGeneric> funcGeneric = ClassY.FunctionReturnX; //Wrong } 回答1: In summary, you cannot cast ClassX to any class that implement IBase . You are only guaranteed to be able to cast it to IBase itself.

Template accepts const but not literal

萝らか妹 提交于 2019-12-08 04:11:08
问题 When writing a template, class T may be substituted by a const type. Consider: template<class T> T& min(T& a, T& b) { return a < b ? a : b; } This will work in the following cases: int a = 1, b = 5; const int c = 1, d = 5; min(a, b); // T is int min(c, d); // T is const int But will throw a compilation error when called with a literal (like so): min(1, 5); // T is const int literal invalid initialization of non-const reference of type ‘int&’ from an rvalue of type ‘int’ Why? Isn't an int

Scala: “Static values” in traits?

烈酒焚心 提交于 2019-12-08 02:43:10
问题 Let's say I have: trait X { val x: String } Using mix-in, I can define a trait such as trait XPrinter { self: X => def printX: String = "X is: " + x } such that a value/object implementing XPrinter implements x and give its methods such as printX access to the values specified in X such as x . So far, so good. I want to know if there is a way of having a trait in the form of: trait XDependent[T <: X] { def printX: String = ??? } So that XDependent instances have access to the value of T.x ,

Structural Programming in Scala with Shapeless: How to use the SYB implementation correctly?

五迷三道 提交于 2019-12-08 01:30:16
问题 I want to use the SYB implementation in the Shapeless library to write the following generic traversal function: class Data // Perform the desired manipulation on the given data object manipulate extends ->((data: Data) => data) def traverseAndManipulate[B](expr: B): B = { everywhere(manipulate)(expr) } Unfortunately, this code produces the following type error (using Shapeless 2.0.0-M1 and Scala 2.10.2): type mismatch; [error] found : shapeless.EverywhereAux[SYB.manipulate.type] [error]

How do I express generic map and set containers in Rust?

不羁岁月 提交于 2019-12-08 00:25:41
问题 I'm learning Rust coming from a C++ background and I'm writing a topological sort. The input is a dependency map with type Map<Key, Set<Key>> , where every node (key) is mapped to its dependency (a set of keys). Map and Set can be any Map and Set implementation. The output is a vector with sorted topological order. In C++, I would use a "template template parameter" for both Map and Set : template< class K, template<class...> class Map, template<class...> class Set > std::vector<K>

Trying to make templates in C

元气小坏坏 提交于 2019-12-07 23:23:31
问题 I made a generic vector in C using macros. Is the concept viable or do I get a one-way trip to the bonfire for even thinking about it? #ifndef VECTOR_H #define VECTOR_H #define vector_at(vector, pos) ((vector).data[pos]) #define VECTOR_DEFINITION(type)\ typedef struct {\ size_t size;\ size_t capacity;\ type *data;\ } vector_ ## type ## _t;\ void vector_ ## type ## _reserve(vector_ ## type ## _t *vector, size_t size) {\ size_t capacity = 1;\ while (capacity < size) capacity *= 2;\ if (size ==

Compiler cannot find right implicits for shapeless LabelledGeneric

ぐ巨炮叔叔 提交于 2019-12-07 14:11:47
问题 Continuing the discussion about converting case classes and Map[String,Any] , I faced some problems when I wanted to use it in a generic way, Here is the main discussion. And when I want to use a generic method like following, compiler complains about the implicits: import MapConvertor._ def convert[A](cc: A)= { cc.toMapRec } Here is the whole code that provides toMapRec : import shapeless._, labelled.FieldType, record._ trait ToMapRec[L <: HList] { def apply(l: L): Map[String, Any] } trait

What kinds of types does qsort not work for in C++?

守給你的承諾、 提交于 2019-12-07 05:24:58
问题 std::sort swaps elements by using std::swap , which in turn uses the copy constructor and assignment operators, guaranteeing that you get correct semantics when exchanging the values. qsort swaps elements by simply swapping the elements' underlying bits, ignoring any semantics associated with the types you are swapping. Even though qsort is ignorant of the semantics of the types you are sorting, it still works remarkably well with non-trivial types. If I'm not mistaken, it will work with all

Template accepts const but not literal

荒凉一梦 提交于 2019-12-06 16:41:30
When writing a template, class T may be substituted by a const type. Consider: template<class T> T& min(T& a, T& b) { return a < b ? a : b; } This will work in the following cases: int a = 1, b = 5; const int c = 1, d = 5; min(a, b); // T is int min(c, d); // T is const int But will throw a compilation error when called with a literal (like so): min(1, 5); // T is const int literal invalid initialization of non-const reference of type ‘int&’ from an rvalue of type ‘int’ Why? Isn't an int literal a const int ? And how can the template be modified to allow working with literals? (consistent with

Extracting an Id with GHC.Generics

↘锁芯ラ 提交于 2019-12-06 13:58:12
问题 How to extract an identifier (in this case an integer) from a structure using GHC.Generics ? I have an Id type: newtype Id = Id { _id :: Int } and a lot of types that use that type: data VarId = VarId Name Id SortId data FuncId = FuncId Name Id [SortId] SortId data SortId = Name Id -- ... and 20 more of these things On top of that there are other types that wrap the types above, for instance, identifiers: data Identifier = IdVar VarId | IdFunc FuncId | IdSort SortId -- ... and 20 more of