Readit News logoReadit News
cletus · 6 years ago
So some immediate thoughts:

1. Why not just call these "structs"? Everyone knows what a struct is. Also making "struct" a reserved keyword is less likely to cause issues than "record";

2. I get that these are trying to be simple but why can't I compose records from other records? I guess I can have a record member of a record?

3. I couldn't see if records can be passed entirely on the stack like primitive values can. This would be huge. I seem to recall there was some effort to add this to Java but I'm not sure whatever happened to that. It probably added a ton of complexity.

kasperni · 6 years ago
1. Records are reference types (heap allocated), structs are not. C# have structs and will also be getting records with 9.0

2. Yes, you can make records of other records.

3. Not yet, but Project Valhalla will be bringing inline types to Java https://wiki.openjdk.java.net/display/valhalla/Main

buckminster · 6 years ago
C# records:

https://github.com/dotnet/roslyn/blob/features/records/docs/...

It's a struct or class but you only define the data members and the compiler auto-generates boiler plate like constructors, equality tests, etc.

xxs · 6 years ago
Personally I have been wanting to use headerless objects for a long time (pre java7). The best available option is DirectBuffer, still.
skywhopper · 6 years ago
What do you mean, "structs are not"? As far as I can tell, structs do not exist in Java?
BubRoss · 6 years ago
Aren't classes already allocated on the heap? Why would C# need another type like that?
Sharlin · 6 years ago
1. C popularized the `struct` keyword, but "record" is a more general, language-agnostic term for nominal tuple types in the literature.

2. Yes, it is widely accepted that a has-a relationship is the proper way to compose data. Using inheritance for composition is bad practice.

3. The article mentions inline classes, which is what the proposed value type feature is called these days. Presumably many or most record types will be inline if and when they're both implemented in some future Java version.

namelosw · 6 years ago
It's not structs in C, it's more like records in Haskell.

If you have two records {a: 1, b: 2}, those are equal because they are the same.

If you have two structs {a: 1, b: 2}, those are also equal but they are separate instances that consume more space. The comparison is recursive.

It's more like how string works in Java and most modern languages with constant pools - behaves like values but only takes constant space.

Deleted Comment

pvg · 6 years ago
There's a detailed Brian Goetz writeup about much of this at:

https://cr.openjdk.java.net/~briangoetz/amber/datum.html

fmjrey · 6 years ago
Disclaimer: it's been a long time since I coded in Java, and I have no knowledge of this new record feature, but coming from clojure I would guess the answers to be:

1. You can't change a record after the fact because if you do it's no longer reliable, it's been tampered with :)

2. Yes, existing records only. You can always create a new record pointing to the old or any other record.

3. Hope not, would make it unpractical to handle verbose facts.

I guess clojure really twisted my mind ;)

stewbrew · 6 years ago
Ad 1: Maybe they like pascal, scheme etc. more than c?
vmchale · 6 years ago
"record" is used in the Haskell world, for example. Also ATS.
Polyisoprene · 6 years ago
They are not that similar to a struct in C in that they are not just about being a data carrier. So that might be confusing if that is what you expect.

Future work is intending to solve the third point and add deconstructing the record for pattern matching.

“We can declare a simple x-y point abstraction as follows: record Point(int x, int y) { } which will declare a final class called Point, with immutable components for x and y and appropriate accessors, constructors, equals, hashCode, and toString implementations.”

dwohnitmok · 6 years ago
Records are great. If sealed classes become a thing (https://openjdk.java.net/jeps/360) I'll be absolutely elated. And it would be just the cherry on top if the two could be used together (along with a private constructor). Scala has this, i.e. `sealed abstract case class`, Kotlin does not, i.e. `sealed data class` is not valid.

I am thrilled that algebraic datatypes (effectively the combination of records and sealed classes) are finally entering mainstream consciousness and that statically typed languages all over the place are starting to adopt them.

jillesvangurp · 6 years ago
Kotlin data classes cannot be extended and they can only extend sealed classes. So they are effectively sealed already.
dwohnitmok · 6 years ago
I perhaps should have been more explicit about the goal rather than the method, since sealing records is only one part of this.

This is all in service of private constructors. You effectively cannot have private constructors for data classes in Kotlin because the copy method remains public and cannot be "turned off." `sealed abstract case class` in Scala gets rid of that.

Private constructors for record types is a fantastic light-weight way of incorporating runtime checks into compile-time types.

AzzieElbab · 6 years ago
Cool. Can't wait until java people start going crazy about profunctor optics
augusto-moura · 6 years ago
Guess everyone can dream
fctorial · 6 years ago
At first I thought it was project valhalla merging into mainline java. Apparently this is a java language feature. Java is starting to move away from being the C of jvm. Java constructs are no longer direct mappings of jvm constructs.
tristanstcyr · 6 years ago
I think that this ship has sailed quite some time ago. For example, inner classes which were introduced in Java 4 (if my memory serves right).
lokedhs · 6 years ago
I'm pretty sure it was in Java 1.2. That's when Swing was implemented, and I don't recall ever having used Swing without inner classes.
th0ma5 · 6 years ago
This sounds a lot like Clojure records and it may be some time before I can mentally distinguish them just based on the name alone.
abraxas · 6 years ago
Will arrays of records be contiguous in memory or are we still chasing pointers just as with all other arrays of objects in Java?
chris_overseas · 6 years ago
You'll still be chasing pointers with these. What you (and myself and many others) are looking for are inline types[0][1] that are part of Project Valhalla[2].

[0] https://openjdk.java.net/jeps/169

[1] https://dzone.com/articles/valhalla-lw2-progress-inline-type...

[2] https://wiki.openjdk.java.net/display/valhalla/Main

dajohnson89 · 6 years ago
I always thought array elements were contiguous in memory? Is it that the objects are contiguous, but the pointers are not? If so, this does seem pretty expensive and I wonder if there's any workarounds.
BubRoss · 6 years ago
Does it strike anyone as odd that something trivial in toy languages from the 70s is a separate project 30 years in and named for the the heaven of the gods in the world of Java?
abraxas · 6 years ago
Thanks, you're right on. That's what I'm after and that's exactly what Java needs to be useful for doing high performance computations in a sane fashion.
kasperni · 6 years ago
Records can be combined with inline types when they get there with project Valhalla.
dangerlibrary · 6 years ago
These seem extremely similar to Scala's case classes. In my experience, case classes are often the feature that sells people on moving over to Scala from Java (the other being exhaustive pattern matching warnings from the compiler).

Fingers crossed Java never implements the `implicit` keyword...

McDev · 6 years ago
q3k · 6 years ago
... all of these are likely inspired by the extremely powerful namesake of Java Records: Haskell Records [1].

[1] - http://learnyouahaskell.com/making-our-own-types-and-typecla...

harryh · 6 years ago
The implicit keyword is extremely powerful when used appropriately and not something that should just be shunned completely.

My favorite usage is probably in a typesafe Query engine that several people[1] built at Foursquare that is a lot better than what you can find in the java world.

  Venue.where(_.closed eqs false)
       .orderAsc(_.popularity)
       .limit(10)
       .modify(_.closed setTo true)
       .updateMulti
It allows for all sorts of typesafe queries that will fail at compile time if things are malformed including stuff like failing to query on an index.

https://github.com/foursquare/rogue

1. Hi Jorge, Liszka & Neil!

dangerlibrary · 6 years ago
I've never shown a use of "implicit" to an experienced developer unfamiliar with Scala and seen them get excited about the language.

The best case scenario is that they aren't instantly horrified / confused / repulsed.

LordHeini · 6 years ago
I would say there are tons of other features as well.

More often than not Java programming just consists of throwing gang signs on your keyboard for your Ide to create tons of boilerplate.

Well case classes can implement traits, might have a companion object and can (poorly) replace enums.

Scala sometimes seems to not need standard classes.

Other languages like go, rust or c can do just fine without classes at all.

halfmatthalfcat · 6 years ago
Implicits are very powerful when used tastefully but are being replaced/deprecated in Scala 3 regardless.
virtualwhys · 6 years ago
Actually, implicits aren't going anywhere in Scala 3.

There will be new syntax, but otherwise the underlying semantics will remain the same; it's just the implicit keyword itself and the kitchen sink of functionality that it provides in Scala 2 will be split up into new (soft) keywords like "given foo(using: Bar) = ..." rather than "implicit def foo(implicit bar: Bar) = ...", along with "extension listOps of" replacing "implicit class ListOps ...".

The community isn't united over the proposed changes (mostly due to migration concerns it seems), but overall it looks like a nice improvement over the status quo, particularly around typeclass definitions.

Joeri · 6 years ago
I only see a superficial similarity. Case classes are much more powerful. Copy constructor, private constructor, pattern matching, overrideable apply/equals/hashcode. Granted, some of that stuff is esoteric, but it does make case classes a very different beast.
jfengel · 6 years ago
Doesn't adding a "record" keyword break a lot of existing code? It wasn't a keyword as recently as JDK 8 (https://docs.oracle.com/javase/tutorial/java/nutsandbolts/_k...).
kasperni · 6 years ago
The keywords you refer to in your link are all "classic" keywords. The last one that was added was "assert" with Java 1.4, which broke a lot of code. And made people realize that adding new "classic" keywords was a bad idea.

Now, Java only adds contextual keywords. Which are basically a sequence of Java letters that are tokenized as a keyword in certain contexts but as an identifier in all other contexts. var, record, inline, module, ect. are examples hereof.

You can read more about it in this JEP Draft about "keyword management for the Java Language": https://openjdk.java.net/jeps/8223002

chrisseaton · 6 years ago
Can you give an example of some code it'd break?

At what points in a valid legal Java program could you have previously written record, where it would now conflict with this keyword in the locations where it is valid?

Macha · 6 years ago
Unless the rules of keywords have changed:

Map<String, String> record = getSomeData();

This should no longer be valid if record is a keyword?

fctorial · 6 years ago
It shouldn't be that big of a deal. Renaming is easy as pie in java codebases.