4

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.

Yrogirg
  • 2,301
  • 3
  • 22
  • 33
  • "Does anybody here have good examples where types as first-class objects come in hand?" -- Do you have any examples, even if it's a "bad" one? I guess it's not quite clear to me, what *exactly* you mean by "types as first-class objects". Are you talking about compile-time reflective metaprogramming à la Template Haskell or C++, parametric polymprhism à la Haskell / Java Generics / .NET Generics, higher-kinded parametric polymorphism (type constructor polymorphism) à la Haskell / Scala, some kind of runtime reflection over types, or something else entirely? – Jörg W Mittag Jul 24 '11 at 11:26
  • +1, BTW, regardless of my earlier comment. This looks to be a super-interesting question! – Jörg W Mittag Jul 24 '11 at 11:28

3 Answers3

2

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

nponeccop
  • 13,527
  • 1
  • 44
  • 106
1

Reflection

If types are first-class objects is that you can do reflection.

Peter K.
  • 8,028
  • 4
  • 48
  • 73
1

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')
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358