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
class Movie(String title, Director director, {Actor+} actors) { | |
shared actual String string => "Movie(title=``title``, Director=``director``, actors=``actors``)"; | |
} | |
class Director(String name) { | |
shared actual String string => "Director(``name``)"; | |
} | |
class Actor(String name) { | |
shared actual String string => "Actor(``name``)"; | |
} | |
/** | |
demo prints | |
Movie(title=Avatar, Director=Director(James Cameron), | |
actors=[Actor(Sam Worthington), Actor(Sigourney Weaver)]) | |
**/ | |
void demo() { | |
Movie movie1 = | |
Movie( | |
"Avatar", | |
Director("James Cameron"), | |
{Actor("Sam Worthington"), Actor("Sigourney Weaver")} | |
); | |
//now we are going to construct a movie using declarative object instantiation | |
//As you can see this looks like a DSL language to construct objects whereas | |
//in fact you're just making good use of named arguments | |
Movie movie2 = Movie { | |
title = "Avatar"; | |
director = Director("James Cameron"); | |
actors = {Actor("Sam Worthington"), Actor("Sigourney Weaver")}; | |
}; | |
print(movie2); | |
} | |
No comments:
Post a Comment