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

Monday, March 10, 2014

Ceylon: named arguments and declarative syntax for object creation

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