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: Singletons

/**
A singleton is the one and only possible instance of a Class. In languages like Java you have to use
all kinds of crazy patterns to accomplish this.
See http://en.wikipedia.org/wiki/Singleton_pattern
Not so in Ceylon however :) You just need to use an algebraic type with a single subtype (object)
**/
shared abstract class Earth() of earth {}
shared object earth extends Earth(){}
//WON'T COMPILE ==> not a subtype of any case of enumerated supertype: BabyEarth is a subtype of Earth
//class BabyEarth() extends Earth() {}
//WON'T COMPILE ==> not a subtype of any case of enumerated supertype: BabyEarth is a subtype of Earth
//shared object babyEarth extends Earth() {}

No comments:

Post a Comment