If you're interested in functional programming, you might also want to checkout my second blog which i'm actively working on!!

Wednesday, March 5, 2014

Ceylon: Comparable interface and Comparison Class

/**
You can use the comparison operators
<
<=
>
>=
for all Classes satisfying the Comparable interface.
But Ceylon provides a nifty fourth operator, <=> which is the compare operator.
This results in one of the singleton instances (equal, smaller or larger) of the class Comparison.
**/
void comparison_demo() {
Boolean isFiveLargerThan2 = 5 > 2;
print("isFiveLargerThan2 is ``isFiveLargerThan2``");
Integer age = 37;
switch(age <=> 50)
case (smaller) { print("age is smaller than 50");}
case (equal) {print("age is equal to 50");}
case (larger) {print("age is larger than 50");}
//just to double check !!
Comparison comparison = age <=> 20; //compiles fine :)
}

No comments:

Post a Comment