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 define a dynamic interface, so you can use | |
conformant instances outside of dynamic blocks" | |
dynamic DataHolder { | |
"This interface defines a dynamic member, | |
which can only be used inside dynamic blocks" | |
shared formal dynamic data; | |
} |
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() { | |
// Using JavaScript's XMLHttpRequest to connect | |
// to a simple web service that returns the time | |
// on the server and displays it in the console | |
dynamic { | |
print("Sending request..."); | |
dynamic req = XMLHttpRequest(); | |
req.onreadystatechange = void() { | |
if (req.readyState==4) { |
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 JVM backend of the Ceylon compiler provides good | |
// interoperability with Java, so that programs can use the JDK. | |
// Interoperability with JavaScript is also provided, mainly | |
// through "dynamic" blocks. Compile-time checks are relaxed in | |
// these blocks (of course this can result in erros at runtime). | |
dynamic { | |
dynamic doc = window.parent.document; | |
dynamic nodes = doc.getElementsByTagName("A"); |
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
// Interfaces and abstract classes can specify a list of | |
// implementing types. | |
abstract class Node() of Leaf | Branch {} | |
class Leaf(item) extends Node() { | |
shared Float item; | |
} | |
class Branch(left, right) extends Node() { | |
shared Node left; | |
shared Node right; | |
} |
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() { | |
// Ceylon allows generic types and functions, i.e. they may have | |
// type parameters: | |
class Generic<Item>(item) { | |
shared variable Item item; | |
shared Item f(Item other) => other; | |
} | |
Item genericFunc<Item>(Item i) => i; |
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 |
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() { | |
// 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() { | |
"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() { | |
// 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; |