Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a language with something like the following construct?

var_type(TYPE_1, TYPE_2,..., TYPE_N) obj;

and now its possible to:

TYPE_17 staticTypeVariable17;
obj = staticTypeVariable17;

TYPE_3 staticTypeVariable3;
obj = staticTypeVariable3;

without obj being a pointer of some base class and with no need for casting if the types used are from the predefined set. So basically an object that can switch it's type amongst a predefined set of types.

share|improve this question
Something like python? python variables are not typed, only values are. – mouviciel Jun 12 '13 at 7:48
If you need that, then you are doing something wrong and you should learn something about OOP, generics and abstractions – Euphoric Jun 12 '13 at 7:50
up vote 3 down vote accepted

The construct you are referring to is called a discriminated union and is part of quite a few languages and can be created in more.

Among the languages that support discriminated unions are C++, Visual Basic, ML and Haskell and those are just the languages that popped up in the wikipedia article.

share|improve this answer
Exactly what i was looking for. Thank you. – SmokyGotSmoked Jun 12 '13 at 7:59

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.