Ceylon (programming language): Difference between revisions

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, wherewritten <code>A|B</code>, allow a variable canto have more than one type.
The Forfollowing example, here isshows a Ceylon function which may take either an [[Integer (computer science)|integer]] or a [[String (computer science)|string]]:
 
<source lang="ceylon">
Line 75 ⟶ 77:
</source>
 
Intersection types, written <code>A&B</code>, are the theoretical foundation of flow-based typing:
An important component of this feature is flow-based typing. For example, the following code is invalid:
 
<source lang="ceylon">
shared void integerOrString(Integer|String input) {
Integer added = input + 6; // Illegal,illegal; wethe don't+ knowoperator thatis input isnot definitelydefined anon integer.Integer|String
 
if (is Integer input) {
Integer added = input + 6; // Thislegal; input is fine.now inputknown can onlyto be an integer here.Integer
print("Got the integer ``input``");
} else {
Line 90 ⟶ 92:
</source>
 
The first example is wrong because thecondition <code>+</code> operator is not defined for <code>Integer|String</code>, only for <code>Integerinput</code>, but, within the if block, we have checkednarrows the type of <code>input</code> and know it to be simply an <code>&lt;Integer</code>,|String&gt; so Ceylon ''narrows'' the type of the variable&amp; <code>inputInteger</code> and we can treat it like an integer.,
which [[Distributive property|distributes]] to <code>Integer&amp;Integer | String&amp;Integer</code>,
which, as <code>String</code> and <code>Integer</code> are disjoint types, is equivalent to <code>Integer&amp;Integer | Nothing</code> (<code>Nothing</code> is the empty bottom type),
which simplifies to just <code>Integer</code>.
 
==== TypeNull Inferencesafety ====
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&lt;String&gt;</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.
<syntaxhighlight lang=ceylon>Integer foo = 3;</syntaxhighlight>
For example, the signature of a function that removes <code>null</code> elements from a stream of values could be:
 
<source lang="ceylon">
This is perfectly valid in Ceylon, however Ceylon also allows the following:
Iterable<Element&Object> removeNulls<Element>(Iterable<Element> stream);
</source>
 
When <code>removeNulls</code> is called with a stream of <code>Integer|Null</code> elements,
<syntaxhighlight lang=ceylon>value foo = 3;</syntaxhighlight>
the result will be a stream of <code>&lt;Integer|Null&gt; & Object</code> elements,
which simplifies to <code>Integer</code>.
 
==== Type Inference ====
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 <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
Here, the <code>value</code> keyword indicates we are declaring a variable, but does not state its type. Instead, the type is inferred to be <code>Integer</code> from our initial value of 3.
| 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 ==