Live data from Hacker News

Raw String Literals Removed From Java 12 as Feature Set Frozen

infoq.com

11–20 of 60 posts

Re: Raw String Literals Removed From Java 12 as Feature Set Frozen

#11
If Java were to support to scale/fp-like match statements with case objects and unapply methods, it would probably be good enough to obviate Scala (It would still be distinctly worse though).

Pattern matching is such a powerful feature. I wish more languages (like python) would realize it’s value and add it.

Re: Raw String Literals Removed From Java 12 as Feature Set Frozen

#12
post #11

If Java were to support to scale/fp-like match statements with case objects and unapply methods, it would probably be good enough to obviate Scala (It would still be distinctly worse though). Pattern matching is such a powerful feature. I wish more languages (like python) would realize it’s value and add it.

Is it?

I've used Scala before.

There are some cases, like compilers where I could see matching and extracting complex and nested patterns to be useful.

But in most cases monadic operations/functors plus if/else seems pretty much as good.

Re: Raw String Literals Removed From Java 12 as Feature Set Frozen

#13
post #11

If Java were to support to scale/fp-like match statements with case objects and unapply methods, it would probably be good enough to obviate Scala (It would still be distinctly worse though). Pattern matching is such a powerful feature. I wish more languages (like python) would realize it’s value and add it.

I suspect trying to combine pattern matching with java’s container classes would be an exercise in frustration. What struck me about scala was how much nicer its standard library is, even when using only things java has equivalents for.

Re: Raw String Literals Removed From Java 12 as Feature Set Frozen

#14
post #11

If Java were to support to scale/fp-like match statements with case objects and unapply methods, it would probably be good enough to obviate Scala (It would still be distinctly worse though). Pattern matching is such a powerful feature. I wish more languages (like python) would realize it’s value and add it.

This is on the roadmap.

Re: Raw String Literals Removed From Java 12 as Feature Set Frozen

#15
Adding new syntax to Java is tricky (unless certain other JVM languages starting with C ;) but I was really disappointed in the string proposal.

At the very least, have a default, optional string substitution feature. It ain't that hard and it's something users of raw strings will NEED anyways. What good is it to have snippets of raw strings that then need to be parsed again and split and re-glued again?

JavaScript backticks are much more useful out of the box. But that isn't the Java way... In Java, everyone gets to write their own string substitution library, littering the heap with completely unnecessary substrings.

After the disasters with generics and enums, do it right, for once, please?

Re: Raw String Literals Removed From Java 12 as Feature Set Frozen

#16
post #6
post #2

JEP325 seems like a nice little improvement. Just a linguistic trick but still lovely. int numLetters = switch (day) { case MONDAY, FRIDAY, SUNDAY -> 6; case TUESDAY -> 7; case THURSDAY, SATURDAY -> 8; case WEDNESDAY -> 9; }; Switches can return values, and can have multiple cases on the same line. I'm sure in 2 years when my organization gets around to switching to Java 12, I'll enjoy using it.

What happens if the value is not found? I would love it if you could throw an exception from a default block.

You need a default clause when your cases aren't exhaustive otherwise. The compiler will insert a default clause transparently if you switch over an enum: https://openjdk.java.net/jeps/325

The cases of a switch expression must be exhaustive; for any possible value there must be a matching switch label. In practice this normally means simply that a default clause is required; however, in the case of an enum switch expression that covers all known cases (and eventually, switch expressions over sealed types), a default clause can be inserted by the compiler that indicates that the enum definition has changed between compile-time and runtime. (This is what developers do by hand today, but having the compiler insert it is both less intrusive and likely to have a more descriptive error message than the ones written by hand.)

This is pretty much the behavior you'd expect if you're familiar with languages that support pattern-matching.

Re: Raw String Literals Removed From Java 12 as Feature Set Frozen

#17
post #5

Java Unicode escapes are kind of a mess, and the raw string literals proposal only makes it worse. Unicode escapes are processed in Java not just inside string literals, but everywhere in the source code. So, for example, the following program prints "Hello, world!" even though that line of code seems to be commented (\u000a is new line, so it ends the comment): public class Test { public static void main(String[] ar…

Dear God

Why did they do it like this

Just why

Re: Raw String Literals Removed From Java 12 as Feature Set Frozen

#18
post #8

Earlier quoted context omitted.

I don't think I've ever wanted SQL embedded in my Java source over loading it as a resource. Loading it as a resource allows me to edit the SQL in a sql-aware text editor.

IntelliJ is aware of SQL in text strings, and even validates it against the database schema. I like my SQL right next to the code that sets parameters, processes results, etc - mismatches are apparent (and also validated by IntelliJ).

Not just SQL, but any language. It's one of my favourite (and IMO very under-appreciated) features of the Jetbrains editors.

Re: Raw String Literals Removed From Java 12 as Feature Set Frozen

#19
post #5

Java Unicode escapes are kind of a mess, and the raw string literals proposal only makes it worse. Unicode escapes are processed in Java not just inside string literals, but everywhere in the source code. So, for example, the following program prints "Hello, world!" even though that line of code seems to be commented (\u000a is new line, so it ends the comment): public class Test { public static void main(String[] ar…

What is the reason for Unicode escapes? It is something legacy from when Unicode was not universally accepted in all systems? Do other programming languages have this?

Re: Raw String Literals Removed From Java 12 as Feature Set Frozen

#20
post #11

If Java were to support to scale/fp-like match statements with case objects and unapply methods, it would probably be good enough to obviate Scala (It would still be distinctly worse though). Pattern matching is such a powerful feature. I wish more languages (like python) would realize it’s value and add it.

Is it? I've used Scala before. There are some cases, like compilers where I could see matching and extracting complex and nested patterns to be useful. But in most cases monadic operations/functors plus if/else seems pretty much as good.

Unfortunately monads aren’t in base scala (as an explicit class at least), so often you need to do some ad-hoc pattern matching on tuples or data structures and pattern matching really helps in de-structuring and binding relevant variables.

I probably use monadic operations (map, fold, etc) at least 5-10 times a day in my job (I’m a scala dev), and match expressions at least 3-5 times a day.

So pretty useful I’d say. Though then again I’m dealing with some pretty baroque case classes that contain dozens of values of financial information.

Post reply on HN