Skip to content

Instantly share code, notes, and snippets.

@ceylonwebide
ceylonwebide / dynints.ceylon
Created September 26, 2015 18:30
EXAMPLE: Dynamic interfaces
//$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;
}
@ceylonwebide
ceylonwebide / request.ceylon
Created September 26, 2015 18:29
EXAMPLE: Interoperability 2
//$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) {
@ceylonwebide
ceylonwebide / interop.ceylon
Created September 26, 2015 18:28
EXAMPLE: Interoperability
//$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");
@ceylonwebide
ceylonwebide / switch.ceylon
Created September 26, 2015 18:24
EXAMPLE: Enumerations and the switch statement
// 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;
}
@ceylonwebide
ceylonwebide / generics.ceylon
Created September 26, 2015 18:23
EXAMPLE: Type parameters
//$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;
@ceylonwebide
ceylonwebide / named_arguments.ceylon
Created September 26, 2015 18:22
EXAMPLE: Named argument syntax
//$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
@ceylonwebide
ceylonwebide / collections.ceylon
Created September 26, 2015 18:21
EXAMPLE: Collections and sequence comprehensions
//$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"``");
@ceylonwebide
ceylonwebide / classes_and_functions2.ceylon
Created September 26, 2015 18:20
EXAMPLE: Classes and functions 2
//$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;
@ceylonwebide
ceylonwebide / interfaces.ceylon
Created September 26, 2015 18:19
EXAMPLE: Interfaces and mixin inheritance
//$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.
@ceylonwebide
ceylonwebide / classes_and_functions.ceylon
Created September 26, 2015 18:18
EXAMPLE: Classes and functions 1
//$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;