-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
make "annotation constructors" actual constructors #1428
Comments
This is actually a recurring theme. For example, the definition of the syntax final class \Ifoo {
shared new foo {}
}
\Ifoo foo => \Ifoo.foo; //yew! Similarly, I would love to rewrite shared final class Boolean
of true | false {
shared actual String string;
shared new true { string="true"; }
shared new false { string="false"; }
} And shared final class Null
of null
extends Anything() {
shared new null {}
} In both cases, and in the case of the annotation constructor proposal above, I run into the problem that a constructor isn't a toplevel. Hrm, what if there were a syntax like this: shared final class Boolean of true | false {
shared actual String string;
shared new package.true { string="true"; }
shared new package.false { string="false"; }
} That would kill three birds with one stone. |
Then this: object foo {} would just mean this: final class \Ifoo {
shared new package.foo {}
} Which is just what I want, frankly. |
More generic perhaps would be final class \Ifoo {
shared new outer.foo {}
} Because |
@quintesse hrm I suppose you're right. Except |
Well perhaps not, I guess you could just say: "it would be like writing blah blah package blah blah" for a toplevel object and "blah blah outer blah blah" for member and local objects. It's not like you'd ever want to use that syntax over the normal |
So I implemented support for the Still to do:
I find the syntax pretty natural and convenient. And it really does the work of nailing down what |
Well, OK, wait, let me walk that back a little. We can have nested But local objects remain an outlier: we currently have no keyword that identifies the immediately-containing scope. ( |
A couple of options: Using |
Ah: And |
We could use |
Or, the other obvious option is to just use an annotation: |
Actually there is a use for this that has nothing to do with constructors, I suppose. According to §4.2.2, we can also import members of toplevel It might be nice if you could write: import ceylon.language { milliseconds } As an alternative to: import ceylon.language { system { milliseconds } } Just by annotating |
Would this also be allowed? shared class C() {
shared object o {
shared promoted String s => "s";
}
}
shared void run() => print(C().s); |
@lucaswerkmeister well currently we don't support this: import pack { C { o { s } } } So, no. |
Not that I'm agreeing with this whole constructor thingie, but since they're not going away (:sob:), I think constructors should simply naturally be accessible from the same scope as its class... Also, @gavinking, I'm confused by your |
Yes of course. Copy/paste error. |
An good alternative to function joinWithCommas => ", ".join; And: class Strings => Array<String>; Which we currently force you to write using a much more verbose syntax, for example: function joinWithCommas({Object*} objects) => ", ".join(objects); Then, we could rewrite my annotation example above like this: shared final annotation class Doc {
shared String text;
shared new doc(String text) {
this.text = text;
}
}
shared function doc => Doc.doc; Or, more simply, as: shared final annotation class Doc(text) {
shared String text;
}
shared function doc => Doc; But shared final class Boolean
of true | false {
shared actual String string;
shared new true { string="true"; }
shared new false { string="false"; }
}
shared value true = Boolean.true;
shared value false = Boolean.false; |
Well, currently you can do that: value joinWithCommas => ", ".join; and: function joinWithCommas({Object*} objs);
joinWithCommas = ", ".join; The first is cleaner, but the last allows you to override methods. I really like the idea of the simplified class aliases, but honestly I feel like the function aliases looks a lot like value declarations with a small semantic difference that may makes sense implementation-wise on the jvm, but conceptually, it just feels like a clunky, and unnecessary difference... |
But it's not quite the same: you lose the parameter names, which are very important in this case. |
Now that we have the notion of a constructor, and they happen to have lowercase names, it would make more sense if annotation constructors were constructors.
The only thing that doesn't quite fit here is that it looks like you would have to import the
doc
annotation like this:That would be quite inconvenient, and not backward compatible. So we would need to have a special rule that says you can import an annotation constructor like this:
I don't think that's too offensive a notion.
The text was updated successfully, but these errors were encountered: