assert-like debug { /* code */ } feature?

  • Thread starter Andreas Leitgeb
  • Start date
A

Andreas Leitgeb

If `assert' statements are used, the class is *always*
"compiled with assertions." They can be enabled or disabled
at run-time, forty-two weeks after the compiler ran.

I apologize for having once again made a fool of myself, for
not sufficiently researching how assertions really work, before
making wild claims.

While my reasoning went far off the facts, I think that the
result is still valid.

It boils down to providing access to that hidden boolean field
$assertionsDisabled, either directly, or through a getter or
wrapped as a "debug"-block.

I also had a look at Class.desiredAssertionStatus() and it does a couple
of things including calling a native method(whose implementation
I didn't care to look up). Obviously it must be "expensive"
enough, so the compiler creates the above-mentioned field as
a cache.
 
A

Andreas Leitgeb

Stefan Ram said:
Andreas Leitgeb said:
debug {
int check;
// some costly code to determine some supposed invariant
assert check==42;
}

public class Main
{ public static void main( final java.lang.String[] args )
{ assert new java.util.concurrent.Callable<java.lang.Boolean>()
{ public java.lang.Boolean call()
{ java.lang.System.out.println( "alpha" ); return true; }}.call(); }}

Heck, I didn't think it could get that much worse than Eric's
assert-misuse :)

Anyway, it still can only read final local vars from the context
that way.
 
T

Tom Anderson

The others are right. You are a troll. *Good bye*. Plonk!

I think he's a crank rather than a troll, but the appropriate response is
much the same.

tom
 
T

Tom Anderson

It's useful as a form of currying. And it's purely up to a language
designer or a library designer's choice. And it's purely subjective and
depends on individual's taste.

For example, even method calls can be curried.

Suppose you have a File object, which has a "write(String)" method. Now,
the library designer could have implemented it without currying:

void write(String x) { for(char c: x) this.writeChar(c); }
Then to write 3 strings, you would write:
file.write("A");
file.write("B");
file.write("C");

Or the library designer could have implemented it with currying (an
example of this is the StringBuilder class in Java standard library)

File write(String x) { for(char c: x) this.writeChar(c); return
this; }
Then to write 3 strings you can write:
file.write("A").write("B").write("C);

Okay, firstly, this example has nothing at all to do with assignment being
an expression. But never mind.

Secondly, may i ask where you learned to call this 'currying'? I'm aware
of the term, but its established meaning in computer science is something
rather different. Admittedly, something that if you look at it from a
distance, in the right light, looks strangely related, but different
nonetheless. I've heard the style you're describing referred to as
'chaining', or 'invocation chaining' before, but never 'currying'.

Personally, i'm not a big fan of it. In fact, i really, really dislike it
- it means abusing a method's return value to return something that isn't
in any sense a result of the method's execution. This is purely an
aesthetic concern, and a matter of taste, though.

I will mention that Smalltalk has something called method cascading.
Smalltalk statements are normally terminated by a full stop, but if you
use a semicolon, then you can omit a receiver from the next statement, and
it will use the receiver of the previous one. Which is clear as mud, so
here's an example:

file write: 'A'.
file write: 'B'.
file write: 'C'.

Becomes:

file write: 'A';
write: 'B';
write: 'C'.

Or, all on one line:

file write: 'A'; write: 'B'; write: 'C'.

It's a terribly simple bit of syntactic sugar which gives you all the
benefits of chaining without having to (a) design your classes to support
it or (b) pervert your design, depending on how you look at it. It's
particularly useful in Smalltalk, where the style is to write lots of
little methods, rather than a few big ones.

It's another thing that would (arguably) be nice to have in java, although
i don't know what punctuation we've got left for it! Comma, perhaps?

file.write("A"), write("B"), write("C") ;

Or perhaps indicate it by just omitting the receiver:

file.write("A"); .write("B"); .write("C") ;

tom
 
J

John B. Matthews

Tom Anderson said:
Cranks are allergic to facts.
Ed editor textorum probatissimus est -- Cicero, De officiis IV.7

[OT] I'm pretty sure he switched to vi in A.U.C. 711... Hey, wait a
minute, three's only _three_ books in De officiis!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top