Content deleted Content added
add influences and references |
→Type System: improve and expand |
||
Line 63:
One of the most novel aspects of Ceylon is its [[type system]]. Ceylon foregoes java's primitive types<ref>{{cite web|last1=King|first1=Gavin|title=Ceylon: Language Design FAQ|url=http://ceylon-lang.org/documentation/faq/language-design/}}</ref> and [[Object type|boxing]] in favor of a type system composed entirely of first-class objects.
More uniquely, Ceylon allows for union and intersection types.
Union types, The <source lang="ceylon">
Line 75 ⟶ 77:
</source>
Intersection types, written <code>A&B</code>, are the theoretical foundation of flow-based typing:
<source lang="ceylon">
shared void integerOrString(Integer|String input) {
Integer added = input + 6; //
if (is Integer input) {
Integer added = input + 6; //
print("Got the integer ``input``");
} else {
Line 90 ⟶ 92:
</source>
The
which [[Distributive property|distributes]] to <code>Integer&Integer | String&Integer</code>,
which, as <code>String</code> and <code>Integer</code> are disjoint types, is equivalent to <code>Integer&Integer | Nothing</code> (<code>Nothing</code> is the empty bottom type),
which simplifies to just <code>Integer</code>.
====
Union and intersection types are used to provide null safety.
Ceylon is strongly and statically typed, but also has support for type inference. For example, in most strong, statically typed languages, one would create an Integer variable with value 3 like so:▼
The top type of the Ceylon type hierarchy is the class <code>Anything</code>,
which has two subclasses: <code>Object</code>, the superclass of all normal classes and all interfaces, and <code>Null</code>, with the only instance [[Null pointer|<code>null</code>]].
Since <code>Object</code> and <code>Null</code> are disjoint types, most regular types like <code>Integer</code> or <code>List<String></code> are not nullable;
a [[nullable type]] is the union <code>Integer|Null</code>, abbreviated <code>Integer?</code>.<ref>{{cite web
| url=http://ceylon-lang.org/documentation/1.2/spec/html_single/#compiletimesafety
| title=The Ceylon Language: §1.4.3 Compile-time safety for null values and flow-sensitive typing
| accessdate=2015-12-04
| last=King | first=Gavin}}</ref>
Intersection types can be used to get a non-optional type out of a possibly-optional type, such as a type parameter.
For example, the signature of a function that removes <code>null</code> elements from a stream of values could be:
<source lang="ceylon">
Iterable<Element&Object> removeNulls<Element>(Iterable<Element> stream);
</source>
When <code>removeNulls</code> is called with a stream of <code>Integer|Null</code> elements,
the result will be a stream of <code><Integer|Null> & Object</code> elements,
which simplifies to <code>Integer</code>.
==== Type Inference ====
▲Ceylon is strongly and statically typed, but also has support for type inference.
The <code>value</code> keyword is used to infer the type of a variable,
and the <code>function</code> keyword is used to infer the type of a function.
The following two definition pairs are each equivalent:
<source lang="ceylon">
Integer i = 3;
value i = 3;
Integer add(Integer i1, Integer i2) {
return i1 + i2;
}
function add(Integer i1, Integer i2) {
return i1 + i2;
}
</source>
However, to make single-pass type inference possible, type inference is only allowed for non-toplevel and unshared declarations.<ref>{{cite web
| url=http://ceylon-lang.org/documentation/1.2/spec/html_single/#typeinference
| title=The Ceylon Language: §3.2.9 Type inference
| accessdate=2015-12-04
| last=King | first=Gavin}}</ref>
== License ==
|