开发者

What are the benefits of types being first-class objects?

Does anybody here have good examples where types as first-class objects come in hand?

I guess it helps to straightforwardly implement some math concep开发者_开发百科ts, 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.


Take a look at Agda2, ats-lang.org and other languages with dependent types. Not quite what you asked, but related.


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')
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜