Ceylon: real progress or just another language?

Source: Internet
Author: User
Keywords Functions Java provided some so

The road to computer science is littered with things that will become "the next big thing". Although many niche languages do find some place in scripts or specific applications, C (and its derivatives) and Java languages are hard to replace. But Red Hat's Ceylon seems to be an interesting combination of some language features, using the well-known C-style syntax, but it also provides object-oriented and some useful functional support in addition to simplicity. Study Ceylon to see if this future VM language can find its place in enterprise-level software development.

Linux and open source usually have a certain relationship with the forefront of language design, it may be used to support language development tools, or platform open to promote language design progress. Or it could be that open languages based on open source technology (such as the GNU Compiler Collection series, Ruby, Python, and Perl) are excellent because they welcome and encourage experimentation (not to mention red Hat is the company behind Ceylon. For whatever reason, Linux developers can use a wide variety of languages, ranging from being less used to having a certain number of years of language to the latest and most advanced products.

But in a world of C + +, java™, Scala, Ruby, Python, Perl, Erlang, Lua, scheme, and many other languages, is there a need to be concerned about the announcement of a new language dedicated to business-oriented enterprise software development? In many cases, the answer is in the negative. But let's look at Red Hat's future-named Ceylon language, and see if it can rise to the ranks of today's most popular languages.

Ceylon is not Java
"Ceylon is not Java, it is a new language affected by Java, is designed by some Java enthusiasts, they do not feel that there is nothing wrong, Java is not immediately too angry, so there is no one to kill it." "--gavin King

Introduction to Ceylon

Ceylon is a new project from Red Hat, led by Gavin King. King is the founder of the Hibernate project, a persistent solution within the Java language. Although King is a fan of Java technology-the first language to be used for large-scale development-the language, in his words, has some frustrating places (including generic types of language complexity, a rash and obscure standard Edition SDk, Clumsy annotation syntax, broken block structure, XML dependencies, and so on.

So King raises the question of what form the language should have after learning some lessons from the strengths and weaknesses of the Java language and SDK. His answer is Ceylon, a static type language that retains some of the best functional features of the Java language (also running on the JVM), but improves the readability of the language, the built-in modularity, and the incorporation of functional language features such as higher-order functions. Ceylon also uses some of the features of C and Smalltalk, but it is more like the Java language, a new language focused on business computing, but it is also flexible enough to be used in other areas.

Some people call Ceylon "Java Killer" (probably because of the future of the Java language), but in fact Ceylon is running on the JVM, so it is an extension rather than an alternative to Java technology. Using the JVM to support Ceylon execution is an ideal model because it means that Ceylon (like Java) can be ported across a variety of architectures that currently support the JVM.

Language characteristics of Ceylon

Most of today's languages are no longer subject to a simple classification, which embodies a variety of programming styles, and Ceylon is no different. Ceylon is a static type language (statically typed language) (that is, type checking is done at compile time, and corresponds to a dynamic type language such as Lisp, where type checking is done at run time). Ceylon is an object-oriented language, similar to the Java language, that uses a typical C syntax style to support higher-order functions (Higher-order functions) (that is, a function can use other functions as input and output). The Java language does not directly support higher-order functions, so this functional feature represents a unique difference between the two languages.

Sometimes, however, improvement refers to what language cuts rather than what it adds. Ceylon simplifies and deletes some elements of the Java language and replaces them with simpler alternatives. An example of simplification is the removal of public, protected, and Private keywords, and Ceylon's alternative is to include only a shared annotation that defines which element of the class is externally visible. Ceylon also removes overloaded functionality, but uses simpler syntax to provide some workarounds for this functionality (for example, default and ordered parameters).

Ceylon contains support for inheritance, sequences (array) or list construction, generics, named parameters, and so on, and it contains some of the features of Run-time type management (we'll look at an example in the next section). The language is currently in a positive development phase, so its final function is still in an open state.

Ceylon's example illustrates

Although a publicly available compiler does not exist at the time of writing this article, the structure of the Ceylon language has been defined so that it can be studied and considered for its usage and readability by developing simple applications. This section looks at some sample applications written with Ceylon to illustrate its structure.

Hello World

I'll use a "Hello World" program to illustrate the creation of a simple program that gives a simple string of text in the display interface. The example given in Listing 1 shows a top-level named Hello method that uses the WriteLine method to emit a string to the standard output.

Listing 1. Hello World program written using Ceylon

Doc "Hello World"
By "Gavin King"
void Hello () {
WriteLine ("Hello world.");

It is important to note that annotations are also used in API documentation (similar to Doxygen tools) that allow you to specify methods and authors (doc and by annotations, respectively).

Type of Ceylon

Ceylon employs a set of traditional types that are implemented as ordinary classes, including:

1. Natural: unsigned integers, including 0
2. Integer: Signed integer
3. Float: Floating-point number
4. Whole: signed integer with arbitrary precision
5. Decimal: arbitrary precision and decimal digits of any decimal

By default, the Natural, Integer, and float types are 64-bit, but you can annotate them with Sgt to indicate 32-bit precision.

Ceylon Class

Ceylon is an object-oriented language, you use the concept of class to write code. Class (Class) is a type in Ceylon that encapsulates a set of actions (called methods) and States (state), and defines how the state is initialized when the object of the class is initialized (class initializer), Similar to a constructor).

A simple example will help you understand the Ceylon approach. Listing 2 provides a simple class for a counter class, listing 2 uses an optional value to define the class, which means that the user can provide the value or not, and it uses type? This pattern is marked. A class body contains a class initializer instead of a constructor, which defines a private variable (invisible unless the annotation is shared), and then defines the initialization logic. You start by looking at whether the start variable exists, and if it exists, it is used as the initial value for the count. Your first method, annotated as shared, is visible from the outside of the class, and it defines the extender. When called, this method simply increments your counter.

Finally, define a getter method to return the current counter value to the user and define a setter method to set the current counter value for the caller-supplied value. Note that the Assign keyword is used to create a variable property for the setting of counter values. In addition to the processing of a constructor (which is embedded within the class), the class does not have a destructor, nor does it provide a way to implement multiple constructors (this is just one of the different Java languages).

Listing 2. A simple class written using Ceylon

Doc "Simple Counting Class"
Class Counter (Natural. Start) {
Doc "Class initializer"
Variable Natural count: = 0;
exists start {
Count: = start;
Doc "The Incrementer"
The shared void increment () {
Doc "The Getter"
Shared Natural CurrentValue {
return count;
Doc "The Setter"
Shared Assign CurrentValue {
Count: = CurrentValue;

After defining this simple class, let's look at how to use the class in Ceylon. Listing 3 provides a section of code that uses the Counter class. It begins by instantiating a CNT object of the class, noting that there is no new keyword in Ceylon. After the new counter object is defined, the increment method is called and the Getter method is used to output the counter value. It should be noted that in Ceylon, the = and: = operators are different: = This qualifier can only be used for immutable values, and variable assignment is performed using the: = operator.

Listing 3. Using the Counter class

Counter cnt = Counter (1);
Cnt.increment ();
WriteLine (C.currentvalue);

Ceylon encourages the use of invariant properties whenever possible, which means that an object is initialized with a value and will not be assigned again. To indicate that a named variable is mutable (which can be changed after initialization), it must use the variable annotation, as shown in line 5th of Listing 2.

One of the final items to be studied is the main difference between the Ceylon control structure. You should have noticed. In many languages, the curly braces ({}) that follow the conditional expression can be omitted, for example if only a single statement appears:

if (CNT >) statement ();

Ceylon does not allow this syntax, it requires curly braces, that is, the example code shown above is written in Ceylon:

if (cnt > MB) {statement ();}

Because this represents the most common mistake in C, it is acceptable to specifically enforce this correct style.

Higher order functions

Ceylon includes a functional programming style called first-order functions (First-order function), which simply means that a function is treated as a class one object, which can be used as an argument to a function, and can be returned from a function. Take King as a demonstration example provided by the definition of the Repeat method (see listing 4). In this case, the method uses two parameters: a natural as a repeat number, and a method parameter for a function to invoke. The method body of the repeat method simply creates a for loop (using a range operation) and then invokes a method that is passed as a function parameter.

Listing 4. Higher order functions in Ceylon

void repeat (Natural times, void Hfunction ()) {
to (Natural N in 1..times) {
Hfunction ();

The use of this method is simple, as shown in line 7th of listing 5, using a method name with no parameters.

Listing 5. Using higher-order functions in Ceylon

void SayHello () {
WriteLine ("Hello World.")
Repeat (SayHello);

Unlike function support provided in other languages, Ceylon does not support anonymous functions (nameless functions that appear directly in expressions), but it supports closures (closure), which are essentially functions that can refer to States in another function.

Type narrowing

The Ceylon does not contain the instanceof operator that appears in the Java language; it does not contain the type conversions that can be found in C, and the Ceylon alternative is to implement what is called type narrowing (type narrowing). This practice is used to test and narrow the type of object reference in one step. Consider the code snippet in listing 6 below, which uses a special (is ...) code. Constructs to test whether an object reference is a given type, and if the type is determined, the type-specific method is then invoked. This structure is similar to what you saw in Listing 2, which describes optional parameters (exists ...). ) This structure.

Listing 6. Type narrowing in Ceylon

Object obj =;
Switch (obj)
Case (is Counter) {
Obj.increment ()
Case (is Complexcounter) {
Obj.incrementby (1);
One else {
Stream.writeline ("Unknown object");

Ceylon contains another similar definition (nonempty ...). , you can apply the construct to a sequence (array or list) to determine whether the sequence contains elements, so that you do not have to perform operations on them.

The last thing to note is that the syntax of the Ceylon switch statement differs from both the C and the Java languages. This statement in both languages is prone to error, Ceylon enforces block structure on case, and removes default, substituting else block. Ceylon also (at compile time) ensures that the switch statement contains an exhaustive list of instance tests, or at least one else statement that provides a complete overlay. The compiler automatically checks these switch statements and generates an error if an instance is not overwritten.

Other control structures

Ceylon implements the traditional If...else statement, as you would expect, it also implements the Java language exception handling (try, catch, finally). Ceylon also creates so-called fail blocks that are used with the For loop to identify when the loop is interrupted early. Consider the example shown in Listing 7.

Listing 7. Description of the fail block of Ceylon

Instrument I in limited {
if (i.failing ()) {
The break;
Fail {
Modified//Take some action ...

This is a common design pattern in both the C and the Java languages, and is therefore a useful complement to Ceylon.

The future of Ceylon

As King says, Ceylon is a community effort that requires software engineers and testers to help design, build, and validate languages and SDKs. This call encourages Java language users to make feedback, which helps support their migration from the language to the Ceylon. King remains properly silent on the current state of Ceylon, simply saying that the language specification and ANTLR (Another Tool for Language) syntax already exist.

Looking to the future

While some people question the need for a new language, another view of language is to use them as a series of tools to solve problems. Not every language is a suitable or ideal solution for any given problem, but some languages themselves are well suited to specific solution areas; Therefore, it is a blessing, not a scourge, to have multiple languages available. Because Ceylon is still in the development phase, it is still unknown whether it can find a location in the mainstream language that is currently being used. But the language captures enough points of interest, so it would be interesting to further study it when it finally emerges.


Learning materials

1. Gavin King originally introduced the Ceylon language to watch this 30-minute video titled Introducing the Ceylon Project. There are two Ceylon demos from King, including introductory speeches and a demo titled The Ceylon Type System, which covers some of the more advanced features.

2. Gavin King provides a 11-part article series describing the Ceylon language. As you will see from the comments on each element of the series, there is a considerable amount of feedback and a dynamic clipping of the language to make up for the problems found. You can find this series of articles in King's blog.

3. To focus on (and participate in) The Ceylon real-time build, view the mailing list of the Ceylon core team.

4. ANTLR is a language tool and framework that is used to build compilers, interpreters, translators, and recognizers from a syntax description. ANTLR from the University of San Francisco (University of San Francisco), based on Terence Parr work.

5. This article mentions the functional features of some of the functions that are implemented in Ceylon. The introduction of the first class function (first-class function), the High-order function (Higher-order function) and the closure (closure) can be found in Wikipediaz.

6. Hundreds of how-to articles and tutorials can be found on Developerworks Linux zone, as well as downloads of resources and discussion forums, and other rich resources available for Linux developers and administrators.

7. Keep an eye on Developerworks technical events and webcasts for a variety of IBM products and IT industry topics.

8. Add a free DeveloperWorks live! Briefing is quick to learn about IBM's products and tools, as well as the development trends in the IT industry.

9. Watch Developerworks On-demand demos, ranging from the installation and setup of products provided for beginners to the advanced features offered to experienced developers.

10. Pay attention to developerworks on Twitter or subscribe to the Linux tweets on Developerworks reader.

(Responsible editor: The good of the Legacy)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.