Covariant overrides on the Buffer Hierachy (original) (raw)
Peter Levart peter.levart at gmail.com
Tue Apr 22 07:05:21 UTC 2014
- Previous message: Covariant overrides on the Buffer Hierachy
- Next message: Covariant overrides on the Buffer Hierachy
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On 04/22/2014 04:30 AM, Zhong Yu wrote:
On Mon, Apr 21, 2014 at 7:07 AM, Ulf Zibis <Ulf.Zibis at cosoco.de> wrote:
So to me, this is clearly a language matter, not to solve by tricky footprint and/or performance lasting code on APIs: People want a smart syntax for consecutive method invocations on the same object, that's all! Amen to that! However I'm a little concerned with the mental effort a reader needs to figure out the implicit method receiver. I'd like to see more explicitness in establishing the implicit method receiver, something like Pascal's with statement. expr "." { // expr is the implicit receiver for this block foo(); // interpreted as expr.foo() } so that we could write CharBuffer.allocate(26).{position(2);put("C");position(25);put("Z");} string.{ mySub=subString(indexOf("C"), indexOf("Q")); } new HashMap<>().{ put(n1,v1); put(n2,v2); } new Button().{ color(red); text("hello"); onClick(event->color(blue)); }
Some questions about this syntax:
What is the type of :
new HashMap<>().{ put(n1,v1); put(n2,v2); }
is it an expression or a statement? It should be an expression, otherwise it's not of much use. It should be possible to do:
Map<Integer, String> map = new HashMap<>().{ put(n1,v1); put(n2,v2); };
...but that looks very similar to an anonymous class syntax, doesn't it? It actually behaves similarly to, for example:
Map<Integer, String> map = new HashMap<>() {{ put(n1,v1);
put(n2,v2); }};
I don't know if this is good or bad.
It certainly spawns all kinds of questions that were on the agenda for lambda expressions, like:
- what's the meaning of "this" inside the .{ ... } block?
- what's the meaning of "break", "continue", "next", "return", .... (I guess it should be the same as with the ordinary lexical block: { ... }, although it's rather unusual to be able to for example "break" out of a loop inside the evaluation of an expression)
Regards, Peter
Zhong Yu
- Previous message: Covariant overrides on the Buffer Hierachy
- Next message: Covariant overrides on the Buffer Hierachy
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]