What are the benefits of types being first-class objects?
-
25-10-2019 - |
Question
Does anybody here have good examples where types as first-class objects come in hand?
I guess it helps to straightforwardly implement some math concepts, indeed that is the kind of examples I'm looking for.
UPD To clarify the question, what can be done if one can make functions accepting types and returning types, or store types in variables?
I'm studying Aldor, though due to license issue it is a bit dead. There types are said to be first-class objects, at least in the sence above.
Solution
Take a look at Agda2, ats-lang.org and other languages with dependent types. Not quite what you asked, but related.
OTHER TIPS
Reflection
If types are first-class objects is that you can do reflection.
Dynamic factory.
_types = {}
register_type(typ, iden):
_types[iden] = typ
def factory(iden):
typ = _types.get(iden)
if not typ:
raise ValueError('Type not registered for %r' % iden)
return typ()
register_type(SomeClass, 'class1')
register_type(SomeOtherClass, 'class2')
print factory('class1')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow