D and Java [was Re: The DUB package manager]

Russel Winder russel at winder.org.uk
Sun Feb 24 02:34:27 PST 2013


On Sat, 2013-02-23 at 23:22 +0400, Dmitry Olshansky wrote:
[…]
> Would be nice to know if there is something that can represent this C 
> snippet without the usual heaps of verbosity:
> enum {
> STATUS_OK = 0,
> STATUS_FAIL_REASON_XYZ,
> ... ad infinitum (but presently dozens)
> };

        enum StatusCodes {
          STATUS_OK,
          STATUS_FAIL_REASON_XYZ,
        }

Is perfectly valid Java.

> There are lots of other cases, but this one is prime.

Job done then, Java is fine for your purposes!

> You might argue for using classes and inheritance + visitor pattern 
> instead of plain switch/state machines. The heaps of boilerplate to 
> write/generate all of "state"/visitor etc. classes argues the other way 
> around.

That used to be the right way of doing it in Java, but as you say too
much boilerplate, so the syntax was added to make it all work correctly.
There is much more to the Safe Enum pattern implementation in Java. For
example see the Planets enum example in the Java documentation.

> (and I suspect the execution speed too)

No point in philosophizing about performance, measurement is the only
valid way of making any form of performance comparison.

> > The G1 GC is a significant improvements to Java.
> 
> I guess this depends on the platform you (have to) use?
> BTW I agree that Java's GC is nice, more so the automatic 
> de-heapification of modern VMs.

No G1 is just nice.  I think it is standard in JavaSE and JavaEE from
7u6. Not sure about JavaSE Embedded.  Although JavaME is due a
re-launch, I think it is dead in the water before being launched.

> A trivial example is storage of pairs or tuples. That plus using Java's 
> containers makes memory footprint explode. At 100K+ hash-map I notice 
> quite high factor of waste. It's some double digits compared to plain C 
> arrays. I probably shouldn't care that much since servers are having 
> tons of RAM these days, and customers throw money on equipment without 
> second thought. Thus for the moment I just can't help but notice. Thank 
> god we are mostly I/O bound.

Java is a big memory user, anyone trying to claim it isn't is living in
cloud cuckoo land. But the memory does get used efficiently in modern
JVMs especially with the G1 GC. 

I can't see D ever really competing with Java or any other JVM-based
language as JVM-based shops won't even consider D (or C or C++), except
in very few circumstances.

> Another example. I had to sort some objects by rating for a bit of 
> processing with ranking. Yet this rating is computed and really doesn't 
> belong with them at all (and needs locking beforehand) and I can't 
> reorder them in place. What I'd do is create an array of tuple 
> link-rating and sort these.
> 
> In Java I have to create a full-blown object and allocate each array 
> slot separately, cool. Not to mention the complete lack of concise tuples.

Without the code it is difficult to comment other than to say, are you
using the right data structures for the job? Creating small short-lived
objects is now highly efficient on the JVM, especially immutable ones.
Support for tuples is interesting: Kotlin just removed them, Ceylon just
added them. Java is nowhere!

> Writing functors is a royal pain in the ass too. Anonymous classes 
> doesn't quite cut it on the scale of verbosity/benefit. If there any 
> library that doesn't use reflection like crazy to achieve simple, 
> concise functors, I'd be glad to use it but so far I found none.

Java 8. You'll love it. Lambda expressions aplenty. Interestingly, no
need for anonymous classes since it is possible to do call site type
inferencing and use method handles and invokedynamic to get it all in
place without an object in sight.

> Sorry can't elaborate on exact details here, the work is not open-source 
> by any measure.

In which case if you want help I'm available for consultancy!

> > Far too few programmers use the final keyword properly/effectively.
> 
> In Java programs it can be used very often. I'd say too often since I 
> find it rare to have a need to e.g. rebind an object. As for "a few 
> programmers", well you have to type it everywhere, guess why it's not 
> popular?!

Indeed, we all agree on verbose. Java variables should be single
assignment by default and variable only by annotation, but that isn't
going to happen now. So final should be typed first and only remove if
rebinding is needed.

> 
> Mmm. I report my experience not argument much of anything excpet that 
> it's far from pleasant.

Different people like and hate different languages, this is only
natural. I am surprised though how many people stay working in places
using languages they do not and cannot begin to like. 

> If anything I might convince the team to switch off to Scala, someday.
> If not the somewhat alien syntax it would be far easier.

Scala has many pluses over Java, but also some minuses, as do the other
languages such as Kotlin and Ceylon. There is also Groovy which can be
both a dynamic and a static language these days and is closely tied to
Java (but without all the verbosity), unlike Scala, Kotlin and Ceylon
which have significant impedance mismatch in places.


-- 
Russel.
=============================================================================
Dr Russel Winder      t: +44 20 7585 2200   voip: sip:russel.winder at ekiga.net
41 Buckmaster Road    m: +44 7770 465 077   xmpp: russel at winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20130224/9caadf94/attachment.pgp>


More information about the Digitalmars-d mailing list