Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any object-oriented static typed language with variables with few types?

I like reading about programming theories, so could you tell me if there is any object-oriented static typed language that allow variables to have a few types? Example in pesudocode:

var value: BigInteger | Double | Nil

I think about way of calling methods on this object. If object value have type BigInteger | Double language could allow user to call only shared methods (lake plus, minus) but when the type is BigInteger | Double | Nil then object of Nil hasn't methods plus and minus, so we can't do anything usefull with this object, because it has only few shared methods (like toString).

So is there any idea how should work calling methods on variable with few types in static typed object-oriented language?

like image 998
guest Avatar asked Sep 11 '10 21:09

guest


People also ask

Is Python a statically or dynamically typed language?

Python is both a strongly typed and a dynamically typed language. Strong typing means that variables do have a type and that the type matters when performing operations on a variable. Dynamic typing means that the type of the variable is determined only during runtime.

Are all statically typed languages compiled?

Statically typed languages are generally compiled languages, thus, the compilers check the types (make perfect sense right? as types are not allowed to be changed later on at run time). Dynamically typed languages are generally interpreted, thus type checking (if any) happens at run time when they are used.

What languages are statically typed?

A statically-typed language is a language (such as Java, C, or C++) where variable types are known at compile time. In most of these languages, types must be expressly indicated by the programmer; in other cases (such as OCaml), type inference allows the programmer to not indicate their variable types.

Is Java static or dynamic typed?

Java is statically-typed, so it expects its variables to be declared before they can be assigned values.


1 Answers

What you are describing is an intersection type. They do exist in Java, for example, but they only arise within the type-checker as the result of capture conversion and type-inference. You cannot write one yourself.

I don't know of any language which uses them directly, but they are often used to describe or analyze type systems of languages, espececially languages which don't actually have a type system. For example, Diamondback Ruby, which is a static type system and type-inferencer for the dynamically typed Ruby programming language, uses both union and intersection types.

Note that the syntax you are using is generally used to denote union types, which are the dual of intersection types. Intersection types are generally written A & B & C.

like image 123
Jörg W Mittag Avatar answered Sep 28 '22 14:09

Jörg W Mittag