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
/** | |
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