That's my main worry about it. Perhaps we should instead steal something
from Delphi:
BufferedWriter out;
with (out) {
write("foo");
newLine();
write("bar");
newLine();
}
Which basically redefines the implicit target of invocations inside its
scope from 'this' to something else. It's a little wordier.
the above syntax I think exists in several other languages, such as
JavaScript and C# IIRC.
in my thinking, access rights should be preserved, so "with()" should
still not give access to any private or protected fields, ... (but, then
again I guess a proper JVM would enforce this restriction anyways).
admittedly, I would also like get/set support and structs, ... (if/when
I get around to finishing my Java compiler, I may support these as
language extensions...). at present it is a lower priority due to both
using standard compilers, and also having several other languages I also
make use of (my VM core also handles C and BGBScript at present).
get/set (AKA: properties) would just be syntax sugar, and probably with
a defined mapping to actual methods (probably "getX()"/"setX()" or
"get_x()"/"set_x()").
structs are still under some debate, mostly WRT how exactly to represent
and handle them in the VM (several related mechanisms were implemented).
I am now leaning towards handling them similar to normal Objects, with
them differing mostly in that they will use pass-by-value semantics.
likely implementation (current idle thinking):
they will be implemented as classes, which will be marked final and
likely inherit from "bgb/vm/Struct" (or "java/lang/struct", but this is
non-standard and "java/lang" is Sun/Oracle turf...);
potentially, semantics could be handled explicitly in the bytecode
(allowing potential standard-JVM compatibility).
hence, Struct would contain several special/magic methods:
"Z_clone()", copy struct;
"Z_set()", assign struct (copies all fields into a target struct of same
type);
"Z_free()", called when struct goes out of scope (likely no-op in
standard JVM).
(or "_Clone()", "_Set()", ... it is an open issue).
issues:
the above design would allow plain Java code (IOW: a standard compiler)
to access structs (possibly good), but plain Java would not necessarily
preserve correct semantics (bad).
a general-purpose "Z_clone()" and "Z_set()" would be expensive in a
standard JVM (would likely need to use reflection to implement them),
but are the most efficient for my VM (which would handle them
internally). struct types could overload these methods (to allow more
efficient handling by a standard JVM), but mine would likely ignore them
in this case (extending "Struct" will tell the VM that they are structs).
likely methods will be passed copies of structs (copy-in-caller), and
the called method will themselves be responsible for freeing them.
....
Going back to the original idea, perhaps you could mark the receiver:
StreamHolder holder;
[holder.getBufferedWriter()].write("foo"); .newLine(); .write("bar");
.newLine();
Although if the rule is otherwise that it's the leftmost term of the
top-level expression (not sure i'm expressing that right), then you
could just use normal parentheses there.
I don't like the above so much...
True. The 'with' form avoids that too. But then i think this notation is
intended for small runs of tightly-coupled operations, where you
probably wouldn't want to log in between them. Still, it would be bound
to trip someone up sooner rather than later.
yeah...
I like with() more...
A construction which i hate with a burning passion. Why on earth does
the append method of a StringBuffer return a StringBuffer? In what sense
does that method evaluate to a StringBuffer? How does that have any
semantic meaning? It's a perversion of the normal rules of method design
to accomodate a particular usage, which is unacceptably awful. The
syntaxes were kicking around are ways of enabling that compact style
without needing to pervert the design of code.
possibly, but it is not that terrible/unusual...
basically, this sort of thing at least allows some vague approximation
of FP coding styles, where not every task is ideally suited to an
OO-based design.
admittedly, I would rather also be able to do some more C-like stuff in
Java as well, but alas...
for my uses, I was actually partly considering (as an extension) the
possibility package-scoped functions (technically, they would likely
exist inside special "hidden" classes), as well as idle thoughts about
handling partial classes (which are possible in my VM core, but lack any
good way to express them either in Java or in the way packages/classes
are managed).
technically, as implemented, "java/lang/String" is implemented using the
same basic mechanism I would use for partial classes, since the class
needs to exist before running the classloader, which is needed to load
String, the result being that a stub version of the class is created (by
the VM), and then the proper class is loaded and forced in on top of it
(basically, the classes merge at this point).
or such...