Content deleted Content added
Lukedegruchy (talk | contribs) Briefly describe Ceylon functions including syntax. |
Lukedegruchy (talk | contribs) No edit summary |
||
Line 153:
// Any example where an anonymous function - (text) => text+text - is provided to the higher-order function above.
String reversed2 = process("one", (text) => text+text);
</source>
==== Enumerated Types ====
Similar to Java and many other languages, and with a similar mechanism as algebraic types, Ceylon supports enumerated types, otherwise known as enums. This is implemented in Ceylon with a pattern of limiting the instances of an abstract class at declaration to a limited set of objects (in this case, singleton instances). Another way to implement this pattern is with the new constructor feature in Ceylon 1.2 where the objects are implemented as different named constructor declarations. <ref>{{cite web|last=King|first=Gavin|title=The Ceylon Language: 4.5.8 Enumerated classes |url=http://ceylon-lang.org/documentation/1.2/spec/html/declarations.html#classeswithcases|accessdate=6 December 2015}}</ref>
<source lang="ceylon">
// Traditional syntax for enumerated type, in this case, limiting the instances to three objects(for this purpose: Singletons)
abstract class Vehicle(shared String name) of plane | train | automobile {}
object plane extends Vehicle("plane") {}
object train extends Vehicle("train") {}
object automobile extends Vehicle("automobile") {}
// Compile error: type is not a subtype of any case of enumerated supertype: 'boat' inherits 'Vehicle'
//object boat extends Vehicle("boat") {}
// New (as of Ceylon 1.2.0) constructor-based syntax
class Vehicle of plane | train | automobile {
String name;
abstract new named(String pName) {
name = pName;
}
shared new plane extends named("plane") {}
shared new train extends named("train") {}
shared new automobile extends named("automobile") {}
// Compile error: value constructor does not occur in of clause of non-abstract enumerated class: 'boat' is not listed in the of clause of 'Vehicle'
//shared new boat extends named("boat") {}
}
</source>
|