문제

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.

도움이 되었습니까?

해결책

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')
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top