Created
September 26, 2015 18:31
-
-
Save ceylonwebide/fbc0950ca55eb5c39c0a to your computer and use it in GitHub Desktop.
EXAMPLE: Operator polymorphism
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() { | |
// Operators are associated to specific interfaces. A type that | |
// satisfies such an interface can be used with the respective | |
// operators. | |
class Product(String name, Float price) | |
satisfies Comparable<Product> { | |
shared actual String string = name; | |
shared actual Comparison compare(Product other) | |
=> price <=> other.price; | |
shared actual Boolean equals(Object other) { | |
if (is Product other) { return price==other.price; } | |
return false; | |
} | |
shared actual Integer hash => price.hash; | |
} | |
// Now we can use comparison operators with Products: | |
value frog = Product("Frog", 16.0); | |
value bee = Product("Bee", 0.50); | |
void cmp(Product x, Product y) { | |
if (x < y) { print("``x`` < ``y``"); } | |
else if (x > y) { print("``x`` > ``y``"); } | |
else { print("``x`` == ``y``"); } | |
} | |
cmp(frog, bee); | |
// alternative version: | |
void cmp2(Product x, Product y) { | |
switch (x <=> y) | |
case (smaller) { print("``x`` < ``y``"); } | |
case (larger) { print("``x`` > ``y``"); } | |
case (equal) { print("``x`` == ``y``"); } | |
} | |
cmp(bee, frog); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment