1

I have the following toplevel class:

class Example(shared String first = "default one", shared String second = "default two") {
}

Now, I want to instantiate this class using an explicit value for first, but the default value for second.

I know how to do this via explicitly compiled code, by using named arguments:

void instantiateExample(String firstValue) {
    Example ex = Example { first = firstValue; };
    assert(ex.first == firstValue);
    assert(ex.second == "default two");
}

Now, I would like to do the same thing as the above code, but by using the Class<Example, []|[String,String=]> object.

Jean Hominal
  • 16,518
  • 5
  • 56
  • 90

1 Answers1

2

with class model it is ...

Class<Example,[]|[String, String=]> exampleModel = `Example`;
value e1 = exampleModel();
value e2 = exampleModel("foo");
value e3 = exampleModel("foo", "bar");

or with class declaration it is ...

ClassDeclaration exampleDeclaration = `class Example`;
assert(is Example e1 = exampleDeclaration.instantiate());
assert(is Example e2 = exampleDeclaration.instantiate([], "foo"));
assert(is Example e3 = exampleDeclaration.instantiate([], "foo", "bar"));

HTH

thradec
  • 106
  • 2
  • Oh crap, I have inverted my first and second arguments when writing the question! My bad :( I will just wait to return home to check that my code works when the order is inverted. Also, great work on Ceylon. ;) – Jean Hominal Apr 25 '14 at 09:29
  • Hello, I have edited the question to switch the order of the two arguments - meaning that the call with ordered arguments does not work anymore. I would be glad if there was an answer to the modified question. Thank you in advance. – Jean Hominal Apr 25 '14 at 21:45
  • I reedited the question to its original form as I found an answer for that on the ceylon-users list (by using namedApply from ceylon 1.1). Thank you. – Jean Hominal Apr 26 '14 at 23:40