This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"sets": [ | |
{ | |
"title": "Try out a sample:", | |
"items": [ | |
{ | |
"title": "Hello World", | |
"gist": "1b5a174b7fda6eb0e3a6" | |
}, | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//$webrun_wrapped | |
shared void run() { | |
// The classic "hello world" in Ceylon | |
print("Hello world!"); | |
// Click 'Run' above to run the program. | |
// It is compiled to JavaScript and then runs directly in your browser! | |
// You can try the more examples from the list on the right of this page |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//$webrun_wrapped | |
shared void run() { | |
// Any object can be converted to a string using ".string". | |
// There are no special primitive types, even numbers are objects. | |
Integer i = 3; | |
String s = i.string; | |
"Declarations can be documented using the `doc` annotation. | |
Note that string literals can span several lines." | |
Boolean b = (i == 3); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//$webrun_wrapped | |
shared void run() { | |
// The following is an error because null is not a String. | |
//String s = null; //ERROR! | |
// We have to write "String?" instead: | |
String? s1 = null; | |
String? s2 = "This is a String."; | |
print(s1); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//$webrun_wrapped | |
shared void run() { | |
//You can use normal conditions as in any other language | |
value nums = 1..10; | |
if (nums.size == 10) { | |
print("ok, normal condition"); | |
} | |
if (nums.size == 10 && nums.first == 1) { | |
print("another normal condition"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//$webrun_wrapped | |
shared void run() { | |
// Functions use the familiar C/Java syntax: | |
Float mean(Float x, Float y) { | |
return ((x*x + y*y) / 2) ^ 0.5; | |
} | |
print("mean(11.0, 307.0) = ``mean(11.0, 307.0)``"); | |
// Short form for functions consisting of a single expression | |
Float mean2(Float x, Float y) => ((x*x + y*y) / 2) ^ 0.5; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//$webrun_wrapped | |
shared void run() { | |
"This interface represents a customer of the shop." | |
interface Customer { | |
"The name of the customer." | |
shared formal String name; | |
// Interfaces may define concrete implementations of methods | |
// and getters/setters. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//$webrun_wrapped | |
shared void run() { | |
// We can use a reference to a function as a value: | |
Float average(Float x, Float y) => (x + y) * 0.5; | |
value f1 = average; | |
print("f1(1.0, 7.0) = ``f1(1.0, 7.0)``"); | |
// The type of f1 is Float(Float, Float): | |
Float(Float, Float) f2 = f1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//$webrun_wrapped | |
shared void run() { | |
// A constant, read-only sequence of values: | |
String[] animals = [ "elephant", "parrot", "giraffe" ]; | |
String[] none = []; // empty sequence | |
// Accessing the sequence returns an optional value (String?) | |
print("First: ``animals.first else "none"``"); | |
print("Second: ``animals[1] else "none"``"); | |
print("Fourth: ``animals[3] else "none"``"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//$webrun_wrapped | |
shared void run() { | |
void order(String product, Integer count=1, Float discount=0.0, | |
{String*} comments={}) { | |
String commentStr = ", ".join { for (c in comments) "'``c``'" }; | |
print("Order '``product``', quantity ``count``, discount `` | |
discount``, comments: ``commentStr``"); | |
} | |
// Ceylon has an alternative syntax for invoking functions or |
OlderNewer