Ceylon (programming language): Difference between revisions

Content deleted Content added
No edit summary
m top: replaced: primitively-defined → primitively defined
(88 intermediate revisions by 55 users not shown)
Line 1:
{{Short description|None}}
{{primary sources|date=January 2017}}
 
{{Infobox programming language
| name = Ceylon
| paradigmlogo = [[Object-orientedCeylon (programming|Object-oriented]] language) logo.png
| logo paradigm = [[image:CeylonObject-oriented (programming language) logo.png|Object-oriented]]
| releaseddesigner = 2011Gavin King
| designerdeveloper = Gavin King, [[RedEclipse HatFoundation]]
| released = {{Start date and age|2011}}
| latest release version = 1.2.0<ref>{{cite web|last=King|first=Gavin|title=Ceylon 1.2.0 is now available |url=http://ceylon-lang.org/blog/2015/10/29/ceylon-1-2-0/|accessdate=29 October 2015}}</ref>
| latest release date version = {{Start date and age|2015|10|29}}1.3.3
| latest release date = {{Start date and age|2017|08|21}}
| typing = [[Type system|Static, strong, safe]]
| typing = [[Static type system|Static]], [[Strong and weak typing|strong]], [[Type safety|safe]]
| dialects =
| scope =
| implementations =
| influenced by = [[Java (programming language)|Java]], [[Scala (programming language)|Scala]],= [[Smalltalk]]
| platform = [[Java virtual machine]], [[JavaScript]]
| file ext = .ceylon
| operating system =
| wikibooks =
| license = [[Apache License|Apache v22.0]]
| website = {{URL|ceylon-lang.org}}
| file ext = .ceylon<ref>{{cite web
}}
| url=http://ceylon-lang.org/documentation/1.2/spec/html_single/#compilationunitstructure
| title=The Ceylon Language: §4.1 Compilation unit structure
| quote=A ''compilation unit'' is a text file, with the filename extension <code>.ceylon</code>.
| access-date=2015-12-04
| last=King | first=Gavin}}</ref>
| implementations =
| dialects =
| influenced by = [[Java (programming language)|Java]],<ref>{{cite web
| url=http://ceylon-lang.org/documentation/1.2/faq/#what_is_ceylon
| title=Frequently Asked Questions: What is Ceylon?
| quote=Ceylon is a new programming language that's deeply influenced by Java
| access-date=2015-12-04}}</ref> [[Scala (programming language)|Scala]], [[Smalltalk]], [[ML (programming language)|ML]],<ref>{{cite web
| url=https://gitter.im/ceylon/user?at=5660a7242cbea1d7054de9d9
| title=ceylon/user - Gitter
| access-date=2015-12-04}}</ref> [[Lisp (programming language)|Lisp]],<ref>{{cite web
| url=https://gitter.im/ceylon/user?at=5660a90e5057376520db6f8b
| title=ceylon/user - Gitter
| access-date=2015-12-04}}</ref> [[Whiley (programming language)|Whiley]]<ref>{{cite web
| url=https://jaxenter.com/top-10-ceylon-language-features-java-wishes-it-had-108003.html
| access-date=2019-11-29
| title=Top 10 Ceylon language features Java wishes it had}}</ref>
| influenced =
| wikibooks =
}}
 
'''Ceylon''' iswas an [[Object-oriented programming|object-oriented]], [[Strong typing|strongly]] [[Type system|statically typed]] programming language with an emphasis on [[Immutable object|immutability]], created by [[Red Hat]]. It is based on the [[Java (programming language)|Java programming language]]. Ceylon programs run on the [[Java Virtualvirtual Machinemachine]] (JVM), and cancould be compiled to [[JavaScript]].<ref>{{cite web
| url=http://ceylon-lang.org/blog/2013/09/22/ceylon-1/
| title=Ceylon 1.0 beta
| accessdateaccess-date=2013-09-26}}</ref><ref>{{cite web
| url=httphttps://www.theregister.co.uk/2011/04/13/red_hat_unveils_project_ceylon
| title=Project Ceylon – Red Hat builds Java replacement
| date=2011-04-13
| publisher=[[The Register]]
| accessdateaccess-date=2011-11-27}}</ref>
The language design focuses on [[Computer programming#Readability of source code|source code readability]], [[Predictability#Predictability in human–computer interaction|predictability]], [[Programming tool|toolability]], [[Modular programming|modularity]], and [[Metaprogramming|metaprogrammability]].<ref>{{cite web
| url=http://ceylon-lang.org/blog/2012/01/10/goals/
| title=Principles that guide this project
| date=2012-01-10
| last=King | first=Gavin
| access-date=2015-12-04}}</ref>
 
Important features of Ceylon include:<ref>{{cite web
Ceylon aims at solving the following problems its developers experienced with Java:<ref>[http://devstonez.wordpress.com/2011/04/13/introducing-the-ceylon-project-gavin-king-presentation-at-qcon-beijing-2011/ Introducing the Ceylon Project – Gavin King presentations at QCon Beijing 2011]</ref><ref>{{cite web
| url=http://inceylon-lang.relationorg/documentation/1.to2/faq/Bloggerslanguage-design/Ceylon#goals
| title=FAQ about language design: Goals
| title=Ceylon
| access-date=20112015-0412-1304}}</ref>
* A type system enforcing [[null safety]] and list element existence at compile time
| author=Gavin King
* Regular syntax and semantics, avoiding special cases and primitively defined constructs in favor of [[syntactic sugar]]
| accessdate=2011-11-27}}</ref><ref name="qaInfoQ">{{cite web
* Support for generic programming and metaprogramming, with [[Reification (computer science)|reified]] [[Parametric polymorphism|generics]]
| url=http://www.infoq.com/news/2011/04/ceylon
* Modularity built into the language, based on [[WildFly|JBoss modules]], interoperable with [[OSGi]]<ref>{{cite web
| title=Ceylon JVM Language
| url=http://ceylon-lang.org/blog/2014/10/10/ceylon-osgi-jee/
| publisher=infoq.com
| title=Write in Ceylon, deploy as OSGI, use in Java EE
| date=2011-04-13
| date=2014-10-10
| quote=''First, I never billed this as a Java Killer or the next generation of the Java language. Not my words. Ceylon isn't Java, it's a new language that's deeply influenced by Java, designed by people who are unapologetic fans of Java. Java's not dying anytime soon, so nothing's killing it''
| last=Festal | first=David
| accessdate=2011-11-27}}</ref>
| access-date=2015-12-04}}</ref> and [[Apache Maven|Maven]]<ref>{{cite web
# improved structured data and user interfaces
| url=http://ceylon-lang.org/documentation/1.2/reference/repository/maven/#maven_repositories
# language level modularity
| title=Maven repositories
# support for [[first-class function|first-class]] and [[higher-order function]]s
| access-date=2015-12-04}}</ref>
# do away with clumsy [[metaprogramming]]
* powerful tools, including an Eclipse-based IDE<ref>{{cite web
| url=http://ceylon-lang.org/documentation/1.2/ide/features/
| title=Features of Ceylon IDE
| access-date=2015-12-04}}</ref>
 
The name "Ceylon" is an oblique reference to Java, in that [[Java]] and [[Sri Lanka]], formerly known as Ceylon, are islands known for growth and export of [[coffee]] and [[tea]].
 
In August 2017, Ceylon was donated to the [[Eclipse Foundation]]. Development slowed down and finally stopped in 2020.<ref>{{cite web
The name "Ceylon" is an oblique reference to Java, in that [[Java]] and [[Sri Lanka]], formerly known as Ceylon, are islands known for growth and export of [[tea]] and [[coffee]].
|date=2020-05-25
|title= ceylon / ceylon
|publisher=GitHub, Inc.
|url=https://github.com/eclipse-archived/ceylon
|access-date=2024-01-22
|archive-url=https://web.archive.org/web/20231003075404/https://github.com/eclipse-archived/ceylon
|archive-date=2023-10-03
|url-status=live}}
</ref> In April 2023, [[Eclipse Foundation]] declared the termination of the transition.<ref>{{cite web
|date=2023-04-05
|title= Eclipse Ceylon™ Termination Review
|publisher=Eclipse Foundation
|url=https://projects.eclipse.org/projects/technology.ceylon/reviews/termination-review
|access-date=2023-04-23
|archive-url=https://web.archive.org/web/20230423134055/https://projects.eclipse.org/projects/technology.ceylon/reviews/termination-review
|archive-date=2023-04-23
|url-status=live}}</ref>
 
== Language features ==
Ceylon is heavily- influenced by [[Java (programming language)|Java]]'s syntax, but adds many new features.
 
=== Type Systemsystem ===
One of the most novel aspects of Ceylon compared to Java is its [[type system]]. Ceylon foregoes javaJava'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. While this may cause boxing overhead in some situations, it makes the type system more uniform.
 
More uniquely, Ceylon allows for union and [[intersection typestype]]s, wherein a variablesimilar canfashion haveto more than one type. For example[[TypeScript]], here is a Ceylon function which may take either an [[IntegerWhiley (computerprogramming sciencelanguage)|integerWhiley]] orand aFlow, [[Stringwhich (computerin science)|string]]:fact, took the idea from Ceylon.
 
Union types, written <code>A|B</code>, allow a variable to have more than one type. The following example shows a Ceylon function which may take either an [[Integer (computer science)|integer]] or a [[String (computer science)|string]]:
<source lang="ceylon">
 
<syntaxhighlight lang="ceylon">
shared void integerOrString(Integer|String input) {
if (is Integer input) {
Line 62 ⟶ 120:
}
}
</syntaxhighlight>
</source>
 
Intersection types, written <code>A&B</code>, are the theoretical foundation of [[flow-sensitive typing]]:
An important component of this feature is flow-based typing. For example, the following code is invalid:
 
<sourcesyntaxhighlight 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 77 ⟶ 135:
}
}
</syntaxhighlight>
</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
| access-date=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:
<syntaxhighlight lang=ceylon>Integer foo = 3;</syntaxhighlight>
 
<syntaxhighlight lang="ceylon">
This is perfectly valid in Ceylon, however Ceylon also allows the following:
Iterable<Element&Object> removeNulls<Element>(Iterable<Element> stream);
</syntaxhighlight>
 
When <code>removeNulls</code> is called with a stream of <code>Integer|Null</code> elements, the result will be a stream of <code>&lt;Integer|Null&gt; & Object</code> elements, which simplifies to <code>Integer</code>.
<syntaxhighlight lang=ceylon>value foo = 3;</syntaxhighlight>
 
==== Functions ====
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.
Similarly to many modern languages, Ceylon supports [[first class function]]s and [[higher order function]]s, including function types and [[anonymous function]]s<ref>{{cite web|last=King|first=Gavin|title=The Ceylon Language: 4.7 Functions |url=http://ceylon-lang.org/documentation/1.2/spec/html/declarations.html#functions|access-date=5 December 2015}}</ref>
 
<syntaxhighlight lang="ceylon">
// A top-level higher-order function using block syntax (not associated with any user-created classes)
String process(String text, String transformString(String toChange)) {
return transformString(text);
}
 
// A top-level function calling String.reverse in expression form.
String reverse(String text) => text.reversed;
 
// A function reference to String.reversed but mostly equivalent to the function above.
String(String) reverseFunctionReference = String.reversed;
 
// An example where the top-level function above is provided as an argument to the higher-order function above
String reversed1 = process("one", reverse);
 
// An example where an anonymous function - (text) => text+text - is provided to the higher-order function above.
String reversed2 = process("one", (text) => text+text);
</syntaxhighlight>
 
==== Enumerated types ====
Similar to Java and many other languages, and with a similar mechanism as [[Algebraic data type|algebraic types]], Ceylon supports [[enumerated type]]s, 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|access-date=6 December 2015}}</ref>
 
<syntaxhighlight 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") {}
}
</syntaxhighlight>
 
==== 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:
 
<syntaxhighlight 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;
}
</syntaxhighlight>
 
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
| access-date=2015-12-04
| last=King | first=Gavin}}</ref>
 
=== Entry point with names===
By default the starter (<code>ceylon run</code>) runs the shared run() function of a module:
<syntaxhighlight lang="ceylon">
/* The classic Hello World program */
shared void run() {
print("Hello, World!");
}
</syntaxhighlight>
but any other shared function without parameters can be used as main calling the program with the ''run'' parameter, like this:
 
<code>ceylon run --compile=force --run hello default</code>
 
== Versions ==
Versions of Ceylon released:<ref>https://ceylon-lang.org/download-archive/ Ceylon: Download Previous Ceylon versions</ref>
<!-- Extracted by entering this [[JavaScript]] code in the browser console of the referenced page: Array.apply(null, $('h3 a').map(function(x,y) { return "* " + $(y).text().trim().replace(/\s*\(Latest release\)/i,"").replace(/Ceylon /,"") + " (" + $(y.parentElement.previousSibling.previousSibling).text().trim().replace(/Released on /,"") + ")"; })).reverse().join("\n")`) -->
* M1 0.1 "Newton" (Dec 20 2011)
* M2 0.2 "Minitel" (Mar 2 2012)
* M3 0.3 "V2000" (Jun 21 2012)
* M3.1 0.3.1 "V2000" (Jul 6 2012)
* M4 0.4 "Analytical Engine" (Oct 29 2012)
* M5 0.5 "Nesa Pong" (Mar 13 2013)
* M6 0.6 "Virtual Boy" (Sep 23 2013)
* 1.0 beta "Virtual Boy" (Sep 24 2013)
* 1.0.0 "No More Mr Nice Guy" (Nov 13 2013)
* 1.1.0 "Ultimate Ship The Second" (Oct 09 2014)
* 1.2.0 "A Series of Unlikely Explanations" (Oct 28 2015)
* 1.2.1 "Irregular Apocalypse" (Feb 11 2016)
* 1.2.2 "Charming But Irrational" (Mar 11 2016)
* 1.3.0 "Total Internal Reflection" (Sep 19 2016)
* 1.3.1 "Now We Try It My Way" (Nov 22 2016)
* 1.3.2 "Smile Tolerantly" (Mar 02 2017)
* 1.3.3 "Contents May Differ" (Aug 21 2017)
 
== License ==
All parts of Ceylon are available as [[free software]], mostly the [[Apache License]].<ref>{{cite web
All the work, including the website, the language specification, and Ceylon Herd, is freely available under open source licenses.<ref>[http://ceylon-lang.org/code/licenses licences, official website]</ref>
| url=http://ceylon-lang.org/code/licenses
| title=Ceylon: Licenses
| access-date=2015-12-04}}</ref> Part of the source code is licensed under LGPL.
 
== See also ==
{{Portal|Free and open-source software}}
* [[Dart (programming language)]], has its own VM, compiles to JS., Typetype system not very strict, supports [[mixin]]s.
* [[Fantom (programming language)]], compiles to JVM., Typetype system not very strict, supports [[mixin]]s.
 
== References ==
Line 105 ⟶ 285:
== External links ==
* {{Official website}}
 
{{Programming languages}}
 
[[Category:Java programming language family]]
[[Category:JVM programming languages]]
[[Category:Red Hat]]
[[Category:Software using the Apache license]]
[[Category:Programming languages created in 2011]]
[[Category:Red Hat software]]
[[Category:Software using the Apache license]]
[[Category:2011 software]]
[[Category:High-level programming languages]]
[[Category:Source-to-source compilers]]