Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Not 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)


Many 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>)


Not 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.


For example in C++:

  template<template<class> class F, class T>
  using apply = F<T>;

  static_assert(is_same_v<apply<list, string>, list<string> >);




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: