News for hackers. Stuff that matters.
Re:Underscore is a keyword in Java 9, use this script to che... by virtualwhys on Saturday January 14, 2017 @11:30AM (#13399115)
Wonder what they'll do with `_`?

At last year's JLS the keynote mentioned that reserving underscore did not mean that Java is becoming Scala. And yet, what else are they going to with it other than to slowly integrate Scala idioms into the language?

If alternative JVM languages gain enough traction perhaps archaic boilerplate like having to define Java getters/setters, equals, hashCode and toString on data classes will replaced sooner than later with terse "modern" equivalents found in C#, Scala, etc.

Re:Underscore is a keyword in Java 9, use this script to che... by alblue on Saturday January 14, 2017 @11:43AM (#13399188)
The use of _ as a discardable placeholder has been present in many languages in the past, such as Haskell and Prolog. These aren't necessarily Scala-isms that Java is adopting; they have a much richer history.
Re:Underscore is a keyword in Java 9, use this script to che... by nilved on Saturday January 14, 2017 @11:50AM (#13399231)
The way Scala uses them is very different from Haskell or Prolog. They are used to create lambdas, not discardable placeholders. E.g. `List(1, 2, 3) map (_ + 1)`.
Re:Underscore is a keyword in Java 9, use this script to che... by pharrington on Saturday January 14, 2017 @02:48PM (#13400198)
_ has a lot uses in Scala, one of them being to create lambdas, another as placeholders when pattern matching. for example val List(a, b, _, d) = List(1, 2, 3, 4)
Re:Underscore is a keyword in Java 9, use this script to che... by cs02rm0 on Saturday January 14, 2017 @01:41PM (#13399829)
...we are certainly NOT going to do Scala's "wunderbar". However, things it might be used for include things like "I don't want to give this variable a name" (such as catch parameters that are never used.)

http://git.net/ml/lambda-development-java/2013-07/msg00350.h...

Re:Underscore is a keyword in Java 9, use this script to che... by chvid on Saturday January 14, 2017 @12:20PM (#13399393)
Good question; to me _ has no obvious meaning. And in general Java is verbose - ie. the keyword for private is "private" rather than some shorthand or symbol. Maybe they should just not use it.
Re:Underscore is a keyword in Java 9, use this script to che... by ivan_gammel on Saturday January 14, 2017 @11:16AM (#13399034)
Re:Underscore is a keyword in Java 9, use this script to che... by Sharlin on Saturday January 14, 2017 @11:22AM (#13399072)
Java 8 already warns about the use of _.
Re:Underscore is a keyword in Java 9, use this script to che... by jakub_g on Saturday January 14, 2017 @02:07PM (#13399962)
Looking forward to have the same feature in JavaScript!

/sees the horror on the faces of all the lodash users/

Ok that was not a good idea

Re:Underscore is a keyword in Java 9, use this script to che... by sratner on Saturday January 14, 2017 @02:46PM (#13400180)
Interestingly, _ is already reserved in the node.js console, so you have to import lodash under a different name.
Re:Underscore is a keyword in Java 9, use this script to che... by mattnewton on Saturday January 14, 2017 @04:07PM (#13400556)
It's actually a magic variable that refers to the last expresion https://nodejs.org/api/repl.html#repl_assignment_of_the_unde...
Re:Underscore is a keyword in Java 9, use this script to che... by Nullabillity on Saturday January 14, 2017 @03:55PM (#13400508)
Seems to work fine for me.

    teo  ~  node -v                                                                                                                                                                                                                                 
    v7.3.0
    teo  ~  node                                                                                                                                                                                                                                    
    > _ = "hi"
    Expression assignment to _ now disabled.
    'hi'
    > _
    'hi'
Re:Underscore is a keyword in Java 9, use this script to che... by sratner on Sunday January 15, 2017 @07:10PM (#13407350)
That's a fluke ;)

    $ node
    > _ = "hi"
    'hi'
    > _
    'hi'
    > foo = "bar"
    'bar'
    > _
    'bar'
(See mattnewton's reply for explanation.)
Re:Underscore is a keyword in Java 9, use this script to che... by Nullabillity on Sunday January 15, 2017 @09:16PM (#13407827)

    > _ = "hi"
    Expression assignment to _ now disabled.
    'hi'
    > "bar"
    'bar'
    > _
    'hi'
    > foo = "bar"
    'bar'
    > _
    'hi'
Re:Underscore is a keyword in Java 9, use this script to che... by sratner on Monday January 16, 2017 @04:56PM (#13413379)
Neat, looks like newer versions detect and correct for it. TIL.
Re:Underscore is a keyword in Java 9, use this script to che... by gsylvie on Saturday January 14, 2017 @12:30PM (#13399436)
Please give us goto and const back!
Re:Underscore is a keyword in Java 9, use this script to che... by forinti on Saturday January 14, 2017 @12:18PM (#13399377)
Will $_ still be valid? You could use that instead.
Re:Underscore is a keyword in Java 9, use this script to che... by chvid on Saturday January 14, 2017 @12:37PM (#13399473)
You have (almost) the full range of unicode at your disposal - have you considered that maybe މ is more meaningful variable name?
Re:Underscore is a keyword in Java 9, use this script to che... by yjftsjthsd-h on Saturday January 14, 2017 @12:54PM (#13399570)
That's a bit harder to type.
Re:Underscore is a keyword in Java 9, use this script to che... by ci5er on Saturday January 14, 2017 @01:33PM (#13399785)
Not if you have a Thaana keyboard!
Re:Underscore is a keyword in Java 9, use this script to che... by kps on Saturday January 14, 2017 @01:53PM (#13399887)
Mixing RTL and LTR is a nuisance, though; that's why ℵ is a good variable name and א is not.
Re:Underscore is a keyword in Java 9, use this script to che... by ci5er on Saturday January 14, 2017 @02:08PM (#13399977)
"consistency is the hobgoblin of little minds", said a well known and frequently quoted man[0].

I can't speak to the pros and cons of ℵ-vs-א, but in other geographies I have found mixing RTL and LTR to be a real PITA. Especially if one is touch-typing and not eyeballing the screen from time-to-time. Of course, before Unicode, we got to tell developers: "Just use ASCII for variable names. That's why God gave us 127 keys."

[0] Some call him arrogant. Others say that I intentionally truncated the quote for my own evil purposes. Who is to judge?

[dead]
Re:Underscore is a keyword in Java 9, use this script to che... by nilved on Saturday January 14, 2017 @01:23PM (#13399732)
This is not acceptable HN material.
Re:Underscore is a keyword in Java 9, use this script to che... by nrjdhsbsid on Saturday January 14, 2017 @01:27PM (#13399752)
For a language I find as reasonably designed as Java, this is stupid.

Add a new key"word", don't deprecate a whole fucking character in a language that's been around for twenty years.

It pains me to consider the horrific amount of code this will break. The lifetimes of hours spent deleting _ that would have been writing whimsical poetry in a moonlit garden or curing cancer.

I'm not surprised I guess given that Java is "owned" by Oracle now. Hopefully they kill it off quickly so we can get the migrations to openjdk overwith.

Re:Underscore is a keyword in Java 9, use this script to che... by ivan_gammel on Saturday January 14, 2017 @02:06PM (#13399961)
Never seen single underscore as variable, field or method name and never seen any naming convention that allowed that (and I've read a lot of open source code too). Conclusion: it's a pain only for those who didn't care about readability of their code and it's definitely not the end of the world.
Re:Underscore is a keyword in Java 9, use this script to che... by sharpercoder on Saturday January 14, 2017 @02:22PM (#13400045)
I've seen it popping up a lot lately. It's used as an identifier for lambda functions, indicating "i must use a variable identifier here, but I don't use it"

someObject.doSomething(_ -> isDerp());

Re:Underscore is a keyword in Java 9, use this script to che... by thechewanater on Saturday January 14, 2017 @02:34PM (#13400112)
No it's not, it's illegal to use _ as a lambda parameter.
Re:Underscore is a keyword in Java 9, use this script to che... by ivan_gammel on Saturday January 14, 2017 @02:34PM (#13400111)
Exactly the case when you get warning in Java 8. Bad code written by lazy devs and now it's time for them to pay.
Re:Underscore is a keyword in Java 9, use this script to che... by edblarney on Saturday January 14, 2017 @02:43PM (#13400167)
"It pains me to consider the horrific amount of code this will break"

Almost none.

_ was never encouraged, promoted, it wouldn't really fit with any sort of Java coding practice at all.

So I don't think it will be a huge issue.

Re:Underscore is a keyword in Java 9, use this script to che... by geofft on Saturday January 14, 2017 @02:16PM (#13400016)
> Add a new key"word"

That's exactly what they're doing. And exactly why many languages are hesitant to add new keywords ever, since you could be using anything as a variable name. (Hence the practice of reserved words which might become keywords in the future.)

> I'm not surprised I guess given that Java is "owned" by Oracle now. Hopefully they kill it off quickly so we can get the migrations to openjdk overwith.

What makes you think that OpenJDK isn't owned by Oracle, or that OpenJDK won't add the _ keyword, given that it's part of the Java 9 spec?

Re:Underscore is a keyword in Java 9, use this script to che... by nrjdhsbsid on Saturday January 14, 2017 @02:29PM (#13400083)
I'll admit the last part was just using a good opportunity to diss Oracle. The longer the keyword the less name collisions there will be in general, using a single character is absolute worst case if you want to avoid breaking code
Re:Underscore is a keyword in Java 9, use this script to che... by Denvercoder9 on Saturday January 14, 2017 @03:09PM (#13400287)
I highly doubt that, seems like you'd get a lot more name collisions if you make something commonly used as a variable (such as index or length) a keyword instead of the seldomly-used underscore.
Re:Underscore is a keyword in Java 9, use this script to che... by chvid on Saturday January 14, 2017 @01:39PM (#13399816)
It is the string '_' that turns into a keyword. Names like _buh or BUH_HUH will still be valid.
Re:Underscore is a keyword in Java 9, use this script to che... by abiox on Saturday January 14, 2017 @01:33PM (#13399786)
> The lifetimes of hours spent deleting _

that's... quite a lot of _

Re:Underscore is a keyword in Java 9, use this script to che... by nrjdhsbsid on Saturday January 14, 2017 @01:44PM (#13399848)
The sheer amount of Java code written is nearly unfathomable. Maybe 3 million developers each churning out 20000 lines a year. That's about 60 billion lines of Java code per year. There could easily be over a trillion lines of Java out there.
Re:Underscore is a keyword in Java 9, use this script to che... by nom on Saturday January 14, 2017 @02:20PM (#13400025)
If that is true, it's stuff for nightmares.

Here is something I made to lighten the mood: https://i.imgur.com/qzOPn85.png

.. or maybe this is even scarier. I'm not sure.

edit: downvotes from java devs incoming :D

Re:Underscore is a keyword in Java 9, use this script to che... by nrjdhsbsid on Saturday January 14, 2017 @02:26PM (#13400065)
Hahahaha

Edit: I'm a Java guy too, don't get why some people get so butthurt over things :)

At least it's not JavaScript with infinity,null,undefined, and NaN. Oh and null!=null which is hilarious

Re:Underscore is a keyword in Java 9, use this script to che... by crazy2be on Saturday January 14, 2017 @03:31PM (#13400398)
You mean NaN != NaN. null == null is true, and null != null is false.

NaN != NaN is surprising, but is not unique to JavaScript. As far as I know, this is the case in every language, and is mandated by the IEEE 754 specification.

However, it does come up in JavaScript more often than other places, since NaN is really easy to get (+{} => NaN, 1 - '' => NaN, etc, etc).

Re:Underscore is a keyword in Java 9, use this script to che... by nom on Saturday January 14, 2017 @02:58PM (#13400239)
LOL yeah.. don't get me started about JS :D.

I more of a C++ guy, but I like Java very much and have used it extensively in the past. I personally like a good C/C++ joke and can't understand why the Java devs are so serious. Apparently nobody is allowed to joke about Java, it's serious business :D

Re:Underscore is a keyword in Java 9, use this script to che... by mhink on Saturday January 14, 2017 @03:15PM (#13400321)
For what it's worth, you should _always_ be using triple-equals in Javascript unless you have an overwhelmingly good reason not to. Many of the weird Javascript-isms people like to complain about are due to surprising type coercions, which don't happen with triple-equals (which behaves as you would expect- value equality without type coercion for primitive types and reference equality otherwise.)
Re:Underscore is a keyword in Java 9, use this script to che... by echelon on Saturday January 14, 2017 @03:29PM (#13400386)
For someone who uses a more than one language on a regular basis, triple equals is an annoying break with convention. It shouldn't be that easy to make a mistake by simply missing an extra character. This is one of many reasons I've taken to using TypeScript for all of my web frontend projects.
Re:Underscore is a keyword in Java 9, use this script to che... by robocat on Saturday January 14, 2017 @05:37PM (#13400993)
However you still run into the same problems with > , < , <= , and =>. So you need to really really understand the corner cases (same as with C++).
Re:Underscore is a keyword in Java 9, use this script to che... by grkvlt on Saturday January 14, 2017 @10:19PM (#13402104)
In SQL, 'NULL <> NULL' is true as well, you have to use 'NULL IS NULL' instead, so the concept has a long and somewhat respectable pedigree...
Re:Underscore is a keyword in Java 9, use this script to che... by revelation on Saturday January 14, 2017 @12:45PM (#13399520)
So I've looked for an explanation beyond "it's done", but save your time, there is none.

This is Java, starting tomorrow it's a keyword because why not, but let's keep in type erasure or the software on our Java Ring (TM) breaks.