3 ms·
So monad is used to construct a new Type dynamically instead of writing it? And functor is also the same?
by SNosTrAnDbLe 5y ago
So monad is used to construct a new Type dynamically instead of writing it? And functor is also the same?
- lmm 5y agoNot exactly dynamically - both a monad and a functor have to be a kind of "function" from type to type. The most common kind is a generic type with one type parameter. (e.g. you can see "List" as the "constructor" that takes "String" to "List<String>", takes "Int" to "List<Int>" and so on)
- SNosTrAnDbLe 5y agoMany thanks!! So is it something like Class<List<String>> thisistypeconstructor(Class<String> strClass) And if I generify the types above, maybe something like <OutputType> thisistypeconstructor(<InputType>)
- lmm 5y agoNot quite - the type constructor is the thing that does the same thing with the types themselves, not with Class (which is sort-of-but-not-really a way to represent types as values). In Java or C# there's no way to actually represent a type constructor (e.g. you can't have a class that takes a type parameter F that could be List or Set, and then you use that type to form F<String> within the class itself and it will be List<String> or Set<String> depending on whether you do new MyClass<List> or new MyClass<Set>), but other languages are more powerful in what they let you do with types. Look up "higher-kinded types" for the general concept.
- gpderetta 5y agoFor example in C++: template<template<class> class F, class T> using apply = F<T>; static_assert(is_same_v<apply<list, string>, list<string> >);