4 ms·
ConcurrentHashMap contains multiple volatile fields. These are kept in sync (i.e. their invariants are preserved) by the ConcurrentHashMap implementation. Concu
by mahkoh 7y ago
ConcurrentHashMap contains multiple volatile fields. These are kept in sync (i.e. their invariants are preserved) by the ConcurrentHashMap implementation. Concurrent programming being hard is a property of concurrent programming. It being hard does not require pattern matching.
Most mutable objects are easy because they don't support concurrent modifications. Therefore pattern matching would not make them harder to implement. Pattern matching such an object while it is being concurrently modified is simply a programmer error just like any other use of the object.
Not allowing pattern matching of mutable objects would be similar to not allowing equals and hashCode methods on such objects and for similar reasons.
- pron 7y agoI'm not talking about not allowing. I merely explained, because you asked, why pattern matching does not play well with mutation, and it doesn't. equals and hashCode also don't play well with mutation, and indeed they are not automatically generated for mutable objects, but they are for records. As the author of a class you can provide a reasonable implementation of them, as well as of a deconstructor for pattern-matching, but that requires knowing more about the particulars of the class. There are other reasons for not allowing mutable records, but most -- including pattern matching -- can be summarized as "that's not what records are about, which is being 'dumb' data aggregates". You can read a more detailed discussion of the subject here: https://cr.openjdk.java.net/~briangoetz/amber/datum.html https://cr.openjdk.java.net/~briangoetz/amber/datum.html
- mahkoh 7y ago>I merely explained, because you asked, why pattern matching does not play well with mutation, and it doesn't. What you explained so far is that it doesn't play well with concurrent modification. And then I said that the "not playing well" comes from the concurrent part, not from the modification part. I'm less interested in the philosophy of records and more in the use cases they support.
- pron 7y ago> What you explained so far is that it doesn't play well with concurrent modification Fair enough. > I'm less interested in the philosophy of records and more in the use cases they support. The use cases and motivation, as well as why mutation is wrong for records, are explained in the links I provided. Here they are again: * https://openjdk.java.net/jeps/359 https://openjdk.java.net/jeps/359 * https://cr.openjdk.java.net/~briangoetz/amber/datum.html https://cr.openjdk.java.net/~briangoetz/amber/datum.html
- smarks 7y agoMutation without concurrency is still a problem for pattern matching. To return to your earlier example, suppose you've matched Point(3, y) and you're now in some code that assumes that the x-value of the Point is 3. Now, you call some method that has some reference to the point you're working on, and it mutates the x-value to 4. The assumption in that code after the match is now broken.
- deleted 7y ago[deleted]
- deleted 7y ago[deleted]