Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (480)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (546)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1] 2 3 ... 5
  ignore  |  Print  
  Java Lambdas Finalised!  (Read 13394 times)
0 Members and 1 Guest are viewing this topic.
Offline kappa
« League of Dukes »

JGO Kernel


Medals: 75
Projects: 15


★★★★★


« Posted 2011-09-10 00:25:04 »

For those that missed it, it was announced that the final syntax for Java Closures has been agreed (will be part of Java 8 ).

http://mail.openjdk.java.net/pipermail/lambda-dev/2011-September/003936.html

After all the years of debate, flamewars and articles, they just decided to use the same syntax as Scala and C#.
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #1 - Posted 2011-09-10 02:07:30 »

Can someone tell me why closures are so awesome? They cause me so many bugs in javascript.

See my work:
OTC Software
Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #2 - Posted 2011-09-10 03:08:50 »

Thank god!

@Eli
I have never used closures before but I can't see anything about it that could cause bugs. Since JavaScript is very different from Java, closures may have some error-prone syntax. However, I see closures as a way to type less and to write more dynamic code.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #3 - Posted 2011-09-10 03:25:48 »

1  
2  
3  
4  
5  
6  
7  
for (var i = 0; i < array.length; i++)
{
    someFunctionThatHasACallback(function()
    {
        doSomeCrap(array[i]);
    });
}

That's an oversimplified example, but basically doSomeCrap will get called on array[array.length-1] array.length times. That's because the way a js closure works is that the most recent assignment to the value it finds will be used. The correct way of doing the above is binding i into the function as a parameter - thereby not using closures. The above is pretty obvious, but if you have a complicated group of callbacks that happen through like 3 different classes and two of them just happen to both define a variable of the same name, you can get the wrong variable being used.

Just pass around the variable you want, it's so much better.

See my work:
OTC Software
Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #4 - Posted 2011-09-10 03:33:22 »

Oh well if there are multiple variables of the same name in different classes and the coder gets them confused, it is the coder's fault Tongue
I do understand your point but since I have yet to try to closures, I still assume I would be easily able to use them without being confused about the order of execution.

Offline zammbi

JGO Coder


Medals: 4



« Reply #5 - Posted 2011-09-10 03:53:09 »

Sounds like I used Closures quite a bit in C# when it came to collection filtering and sorting. So can't wait till its in Java.

Current project - Rename and Sort
Offline Addictman

Senior Member


Medals: 3
Projects: 1


Java games rock!


« Reply #6 - Posted 2011-09-10 09:01:07 »

I work with C# in my dayjob, and I for one will definitely welcome closures with open arms Smiley Miss my lambdas.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 781
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #7 - Posted 2011-09-10 13:30:29 »

Can someone tell me why closures are so awesome? They cause me so many bugs in javascript.
Relax Smiley they aren't closures. They are lambda expressions and they are an entire different species. You won't run into the javascript/closure issues you mentioned earlier.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #8 - Posted 2011-09-10 17:22:34 »

Can someone tell me why closures are so awesome? They cause me so many bugs in javascript.
Relax Smiley they aren't closures. They are lambda expressions and they are an entire different species. You won't run into the javascript/closure issues you mentioned earlier.
Happy days are here again. Smiley

See my work:
OTC Software
Offline JL235

JGO Coder


Medals: 10



« Reply #9 - Posted 2011-09-13 03:45:17 »

YAY! Java is getting a feature which is now common in most mainstream programming languages.

Hopefully we won't have to wait 5 years for the next language feature.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline gouessej
« Reply #10 - Posted 2011-09-13 08:55:19 »

Hi!

I disagree with a lot of people here. Java is going to become so complicated and remember what happened to C++ in the past... A modern language cannot get all "cool" features, choices have to be made and using several programming languages in a single project should not be a problem. Some languages are better to express of some kinds of things. If a single language tries to express at best anything, it can only become extremely complicated to use. Closures give a (contestable) comfort to some people but Java can live without it.

In my humble opinion, the syntax of Java should be frozen, the efforts should be concentrated on the APIs (repairing the existing APIs and adding new ones), the support of other languages on the same VM (not only scripting languages), the improvements of the deployment (applets and Java Web Start, plugin 3 with AWT-free fast launcher) and on the modularization (project JigSaw). I would prefer Oracle to help me to convince several Internet service providers to use Java in their ADSL TV boxes. We need Java everywhere, on consoles, on all smartphones including IPhones and Android (a DVM is not a real JVM).

Some C# programmers asked for closures in Java but I'm not sure that they are the people who use the most Java.

Can someone tell me why closures are so awesome? They cause me so many bugs in javascript.
I agree with you even though I rarely use JavaScript. The "awesomeness" of this feature is not obvious for me.

Online princec

JGO Kernel


Medals: 361
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #11 - Posted 2011-09-13 10:28:45 »

I've managed 30 years without needing closures. Now they're going to co-opt a few more ascii characters to do something that makes not a lot of sense and does even more trickery under the hood. Oh well, not much we can do about it.

Cas Smiley

Online princec

JGO Kernel


Medals: 361
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #12 - Posted 2011-09-13 10:33:44 »

I need to go and sit down and have a glass of water, I just agreed with Julien!

Cas Smiley

Offline kappa
« League of Dukes »

JGO Kernel


Medals: 75
Projects: 15


★★★★★


« Reply #13 - Posted 2011-09-13 10:39:48 »

I agree too and also prefer that the java syntax be kept simple and basic (as some here like to call it as a 'baby language' Smiley). If they need all these extra things they should just pick up Scala and push it as an official jvm langauge. Scala has all the bells and whistles that one could hope for and is pretty compatible with normal Java too.
Offline Spasi
« Reply #14 - Posted 2011-09-13 10:59:15 »

I think Scala is complex and inefficient. Something in between Java and Scala would be better. Kotlin sounds promising.
Offline nsigma
« Reply #15 - Posted 2011-09-13 11:04:05 »

@gouessej - I'd generally agree with your list of priorities there and not adding stuff in for the sake of it.  However, I'm coming around to this one, possibly for the same reason that Oracle did post-Sun, and in one word that is "concurrency".  It's not that there are things impossible with Java's current syntax, but the amount of boilerplate code that can be removed is sometimes staggering.  Java has always included (for the time) good multi-threading support, and I guess this helps further that.

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Online princec

JGO Kernel


Medals: 361
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #16 - Posted 2011-09-13 12:33:37 »

The crappy thing about all this concurrency support is that the whole premise it appears to be based on - that is, unified memory architectures and multiple cores - is kind of crap compared to the infinitely more sensible Transputery way of doing things. IMHO it's going to set software engineering and hardware design back a decade Sad

Cas Smiley

Offline gouessej
« Reply #17 - Posted 2011-09-13 13:22:51 »

@gouessej - I'd generally agree with your list of priorities there and not adding stuff in for the sake of it.  However, I'm coming around to this one, possibly for the same reason that Oracle did post-Sun, and in one word that is "concurrency".  It's not that there are things impossible with Java's current syntax, but the amount of boilerplate code that can be removed is sometimes staggering.  Java has always included (for the time) good multi-threading support, and I guess this helps further that.
What is the "cost" of closures on the "readability" of the source code? Do you have a precise example to illustrate your opinion? How are closures helpful in concurrency? The benefit of closures is very subjective, I can go on using anonymous classes containing a single method, complicating the syntax of the whole language can allow to avoid such boilerplate code but it can drive it more difficult to read.

It's a matter of taste, I find closures worse than anonymous classes containing a single method and do you know the secret of very long projects? When I know I'm going to see my girlfriend, I'm happy because she's pleasant and beautiful. It is exactly the same thing for my source code. I can work on the same project for years if I find the source code "pleasant and beautiful". nsigma finds closures better than anonymous classes containing a single method; in his case, closures are the way to go.

I need to go and sit down and have a glass of water, I just agreed with Julien!

Cas Smiley
Lol. I'm not surprised. I already knew that you agreed with me about this aspect. I'm even throwing AWT out of my game Smiley

Offline nsigma
« Reply #18 - Posted 2011-09-13 13:44:19 »

What is the "cost" of closures on the "readability" of the source code? Do you have a precise example to illustrate your opinion? How are closures helpful in concurrency? The benefit of closures is very subjective, I can go on using anonymous classes containing a single method, complicating the syntax of the whole language can allow to avoid such boilerplate code but it can drive it more difficult to read.

I did have a precise example, I just can't bloody find it now!  Roll Eyes  Essentially it's to do with both fork/join (in 7) and also ParallelArray (in 8 I think).  The example used 80% less code and was infinitely more understandable with closures.  Parallelizing loop operations across data structures can benefit from closures, and seem much easier to understand without the boilerplate.

My absolute benchmark is less code and more legible - this example won me over! (and if I can ever find it again, I'll post a link).

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Offline JL235

JGO Coder


Medals: 10



« Reply #19 - Posted 2011-09-13 15:36:04 »

I write a lot of JavaScript, and closures are easily one of it's best features. It is not that they will allow you to do anything new; it is that you can do the same in a more natural way. This means libraries will use closures more often, as they are much easier to wield then anonymous classes. Once your leveraging closures, there are lots of advantages.

The main one is that it allows libraries and frameworks to have more control over how they are used. It is very common to build a library, that manages some code, but has to be used in a specific way. As a result you can be expected to always call specific methods before performing certain tasks, or calling items in a specific order. Closures allow you to wrap up whatever task it is you are performing, then just hand it over, and have the library call it for you (at the right time).

For example asynchronously downloading multiple files, and running a task when they are all complete. Another example is being able to repopulate a cache, on the fly, when missing items are queried for. The task to generate those missing values can be stored inside the cache, using a closure. These are simpler to build if you can easily wrap up tasks to be passed in.

It can also allow you to build more efficient architectures. For example I once built a grid based data structure that partitioned sprites based on their class. It used custom data structures, but for simplicity, it was essentially a: Map<Integer, Map<Integer, Map<Class, List<Sprite>>>>. As you can imagine, iterating over that is not trivial, and a naive implementation requires using lots of iterator objects nested inside of each other. This iteration loop would build a list of Sprites to return, which would then be iterated over a second time by the caller. To improve this I built a version which used anonymous classes, and this allowed me to remove all of the iterator objects, the returned list, and the second bout of iteration. What is really important, is that is was simpler to optimize this using anonymous objects, then using a complex iterator solution. Closures make it more natural to build these solutions.

Finally writing a Swing UI is far too verbose. One of the issues is the large number of action listeners, and and closures would greatly reduce the amount of noise in your code.

Offline Cero
« Reply #20 - Posted 2011-09-13 15:58:52 »

Quote
I've managed 30 years without needing closures.
I guess enough said for everyone who didn't need closures before wonders if he/she should care about this addition.

Also: It just seems to me that closures just allow stuff to be able to be compiled, but can very well fail at runtime. A thing java usually tries to avoid, and is praised for, in comparison with C++ for example.

Offline Chromanoid

Junior Member


Medals: 3



« Reply #21 - Posted 2011-09-13 16:01:17 »

When using libs they may force you to use these new featues.

I think I agree with gouessej. Java sourcecode is something I can read like a book. For me Lambda expressions are visually like nested ?-:-expressions. I hope I can get used to it.

PS: Smiley first post, hello there Smiley
Offline Cero
« Reply #22 - Posted 2011-09-13 16:03:34 »

When using libs they may force you to use these new featues.

I think I agree with gouessej. Java sourcecode is something I can read like a book. For me Lambda expressions are visually like nested ?-:-expressions. I hope I can get used to it.

PS: Smiley first post, hello there Smiley

Yeah I don't use  ? :  just because it's hard to read.

Offline JL235

JGO Coder


Medals: 10



« Reply #23 - Posted 2011-09-13 16:10:47 »

Quote
I've managed 30 years without needing closures.
I guess enough said for everyone who didn't need closures before wonders if he/she should care about this addition.
Most people have lived for decades without the internet, does that mean we should get rid of it?

Offline nsigma
« Reply #24 - Posted 2011-09-13 16:16:54 »

Quote
I've managed 30 years without needing closures.
I guess enough said for everyone who didn't need closures before wonders if he/she should care about this addition.

The last 30 years does not give a good guide to the next 30 years!  Cool  Moore's Law is coming to an end, etc., etc. http://www.ordina.nl/nl/~/media/Files/Markten/Technologie/Masterclass%20Brian%20Goetz%20%20Project%20Lambda.ashx?forcedownload=1 is interesting.

I have to say I'm at least in partial agreement with Cas' comment earlier - we may just be papering over the cracks.

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Offline JL235

JGO Coder


Medals: 10



« Reply #25 - Posted 2011-09-13 16:49:19 »

Quote
I've managed 30 years without needing closures.
I guess enough said for everyone who didn't need closures before wonders if he/she should care about this addition.

The last 30 years does not give a good guide to the next 30 years!  Cool  Moore's Law is coming to an end, etc., etc.
People have stated Moore's Law has been coming to an end, repeatedly, for the last 30 years. We have at least another 10 years to go until we reach the physical barriers for miniaturizing transistors. By then we should have new methods for yielding larger, or layered chips, which will allow Moore's Law to continue for much longer.

Offline nsigma
« Reply #26 - Posted 2011-09-13 17:02:00 »

Quote
I've managed 30 years without needing closures.
I guess enough said for everyone who didn't need closures before wonders if he/she should care about this addition.

The last 30 years does not give a good guide to the next 30 years!  Cool  Moore's Law is coming to an end, etc., etc.
People have stated Moore's Law has been coming to an end, repeatedly, for the last 30 years. We have at least another 10 years to go until we reach the physical barriers for miniaturizing transistors. By then we should have new methods for yielding larger, or layered chips, which will allow Moore's Law to continue for much longer.

To an end was perhaps too apocalyptic a way to phrase it  Grin , but I think the concept of what it means for us is changing.  To quote that Oracle PDF I linked to
Quote
Chip designers have nowhere to go but parallel – Moore’s Law gives more cores, not faster cores
which, right or wrong, seems to be a key reason behind Oracle's decision to include closures in Java now.

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Offline Chromanoid

Junior Member


Medals: 3



« Reply #27 - Posted 2011-09-13 17:12:52 »

Thank you, after reading this I feel confident that closures will be a convenient feature. BTW I like the syntax in this presentation more Smiley...
Offline Nate

JGO Kernel


Medals: 145
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #28 - Posted 2011-09-13 19:48:17 »

I love how they show anonymous inner classes with no highlighting, then lambdas with highlighted text.  Roll Eyes

Lambdas murder readability when used extensively for loops. I curse every time I have to dig into C# where someone went balls out with lambdas unnecessarily. They probably do improve readability in other cases. I guess it will become just another thing to add to coding standards documents.

Is the method reference stuff going in Java?

Online princec

JGO Kernel


Medals: 361
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #29 - Posted 2011-09-13 20:21:42 »

That article from Oracle does explain things quite neatly, and I can see that it is quite neat. Wonder how I'll be applying it to the sort of noddy coding I do for games. Probably won't Smiley

Cas Smiley

Pages: [1] 2 3 ... 5
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

atombrot (20 views)
2014-08-19 09:29:53

Tekkerue (21 views)
2014-08-16 06:45:27

Tekkerue (21 views)
2014-08-16 06:22:17

Tekkerue (12 views)
2014-08-16 06:20:21

Tekkerue (19 views)
2014-08-16 06:12:11

Rayexar (55 views)
2014-08-11 02:49:23

BurntPizza (37 views)
2014-08-09 21:09:32

BurntPizza (28 views)
2014-08-08 02:01:56

Norakomi (35 views)
2014-08-06 19:49:38

BurntPizza (65 views)
2014-08-03 02:57:17
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!