user3464741
user3464741

Reputation:

Shortcut syntax when directly implementing an interface

Is it possible to implement the ceylon typechecker in such a way, that a class, that satisfies an interface directly (types in members signatures are the same as in the interface being satisfied), can omit types in its own members signatures?

This would help to reduce visual clutter at implementation site by moving all meta information (types, annotations) to the interface. And it helps to focus on implementation details.

This would be close to a signature file in ocaml.

And it could help to say more, more clearly.

Edited after the answer was given by Lukas Werkmeister:

What I'd like to have is a shortcut syntax that works not only for attributes but also for methods.

Look at "name(x)" in class Person:

interface Named {
    shared formal String name(String n);
}
class Person(shared String  firstName, shared String lastName) satisfies Named {

    name(x) => firstName + x +" " + lastName;

}

Named named = Person("Lucas", "Werkmeister");
print(named.name);

Upvotes: 0

Views: 85

Answers (3)

Gavin King
Gavin King

Reputation: 4273

This isn't allowed because it would be a bit too syntactically ambiguous. That is, it's impossible for the parser to distinguish name(x, y, z) from an invocation expression, until it finally hits the =>. This would result in some pretty undesirable behavior, for example, in the IDE when you start typing in a shortcut refinement, the parser would treat it as an invocation expression, and display errors on x, y, and on z.

In general we're very careful to make sure that we don't have any syntactic constructs that require too much lookahead to make sense of.

Upvotes: 2

gdejohn
gdejohn

Reputation: 7579

Shortcut refinement does work with methods too, you just need to specify the parameter types:

interface Named {
    shared formal String name(String n);
}

class Person(shared String  firstName, shared String lastName) satisfies Named {
    name(String x) => firstName + x +" " + lastName;
}

Upvotes: 0

Lucas Werkmeister
Lucas Werkmeister

Reputation: 2742

Yes, with the shortcut refinement syntax: you can simply write thing => ...; instead of shared actual Type thing => ...;. For exampled

interface Named {
    shared formal String name;
}
class Person(firstName, lastName) satisfies Named {
    shared String firstName;
    shared String lastName;

    name => firstName + " " + lastName;
    // shortcut for
    // shared actual String name => …;
}

Named named = Person("Lucas", "Werkmeister");
print(named.name);

Try online

Upvotes: 0

Related Questions