5 ms·
>> not having ADTs (except maybe Rust) built-in Most of the common languages today have product types. Java[1], Rust, Haskell, etc. have sum types. I think i
by CraigJPerry 2y ago
>> not having ADTs (except maybe Rust) built-in
Most of the common languages today have product types.
Java[1], Rust, Haskell, etc. have sum types.
I think it gets a bit more escoteric beyond that though - i don't doubt that there's probably some haskell extension for quotient types[2] or some other category theory high-jinx.
Most languages have ADTs built in.
[1] https://blogs.oracle.com/javamagazine/post/inside-the-language-sealed-types https://blogs.oracle.com/javamagazine/post/inside-the-langua...
[2] https://en.wikipedia.org/wiki/Quotient_type https://en.wikipedia.org/wiki/Quotient_type
- speed_spread 2y agoJava sum types work but still need a bit of syntax sugar on the declaration side, IMHO.
- def_not_troll 2y ago[flagged]
- nextaccountic 2y agoDoes Java sealed classes enable something like an exhaustive pattern matching? (A form of pattern matching that will fail at compile time if you add a new class that extends the sealed class)
- thewakalix 2y ago> The intent is to introduce a more-advanced construction called pattern matching in a later release.
- brabel 2y agoYou read that in a blog post from 2019. Java has had comprehensive pattern matching since Java 21, like one year ago (current Java version is 22). I posted an answer to the same parent comment with the C example written in Java... You can read more about it here: https://www.baeldung.com/java-lts-21-new-features https://www.baeldung.com/java-lts-21-new-features
- brabel 2y agoYes, since Java 21. Example: sealed interface BinaryTree { record Leaf(int value) implements BinaryTree {} record Node(BinaryTree lhs, BinaryTree rhs, int value) implements BinaryTree {} } public class Hello { static int sum(BinaryTree tree) { return switch (tree) { case BinaryTree.Leaf(var value) -> value; case BinaryTree.Node(var lhs, var rhs, var value) -> sum(lhs) + value + sum(rhs); }; } public static void main(String... args) { var tree = new BinaryTree.Node( new BinaryTree.Leaf(1), new BinaryTree.Node( new BinaryTree.Leaf(2), new BinaryTree.Leaf(3), 4), 5); System.out.println(tree); System.out.println("Sum: " + sum(tree)); } } If you added a new subtype to BinaryTree you would need to fix the switch. EDIT: I didn't handle the `null` case above... so it would be a NullPointerException if someone passed null... apparently, Java decided to make handling `null` optional. More information: https://www.baeldung.com/java-lts-21-new-features https://www.baeldung.com/java-lts-21-new-features
- nextaccountic 2y agoOkay that's better than I expected!
- davidalayachew 2y agoIt absolutely does. Here is a (modified) snippet of my Java code from yesterday. final boolean hasUncollectedSecret = switch (each) { case Wall() -> false; case Goal() -> false; case Player p -> false; case BasicCell(Underneath(_, var collectible), _) -> switch (collectible) { case NONE, KEY -> false; case SECRET -> true; }; case Lock() -> false; };
- thewakalix 2y agoJava doesn't have pattern-matching yet. Haskell is not an imperative language.
- j2kun 2y agoYour examples, on the TIOBE index, are #4, #18, and #28. https://www.tiobe.com/tiobe-index/ https://www.tiobe.com/tiobe-index/
- CraigJPerry 2y agoProduct types are one kind of algebraic data type, only 5 languages from that TIOBE page don’t have them so most common langs have ADTs
- dannymi 2y agoWhen you have natural numbers and a multiplication operation (product) would you say that those form an algebra? (ADT means algebraic data type)
- CraigJPerry 2y agoYou’ve got both a carrier (the set of natural numbers) and a morphism (product operation) so yeah you have an algebra.
- LegionMammal978 2y agoJava's sealed classes are still somewhat more limited than Rust's or Haskell's sum types, in that each instance of the superclass holds a fixed variant (i.e., subclass), so you can't change the variant without creating a new instance. Clearly, this limitation is necessary for references to stay intact, but I've personally ran into this issue when trying to represent a sum type in an ORM.
- munificent 2y ago> so you can't change the variant without creating a new instance. Isn't that true of ADTs in all languages? I can't think of a single language with ADTs that lets you change the tag/variant of an existing value.
- LegionMammal978 2y agoIn Rust [0]: #[derive(Debug)] pub enum Example { Foo(i32), Bar(&'static str), } let mut ex: Example = Example::Foo(42); println!("{ex:?}"); // Foo(42) let ex_ref: &mut Example = &mut ex; *ex_ref = Example::Bar("hello"); println!("{ex:?}"); // Bar("hello") Given a mutable reference to a value of enum type, you can replace it with another variant. Or you can swap it out with any other value of the same type, even if the variants are different. This is most commonly used for Option<T>, where you can insert or remove the contained value as long as you have a reference. The limitation here is that for as long as the mutable reference is live, no other code can access the value. So when you do change the variant, you can't have any other references sitting around that point to the removed subfields. [0] https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=0371824536f6b9050f96379ddb966c7c https://play.rust-lang.org/?version=stable&mode=debug&editio...
- munificent 2y agoThis doesn't have anything to do with ADTs. It's because Rust has both in-place variables and references. You aren't changing the variant of an existing ADT, you're replacing the entire ADT value with a new one. That replacement is visible in multiple places because the language allows you to take references to variables (the `&mut ex` expression). You can accomplish the same thing in C and C++ because they also have in-place value semantics and allow you to take the address of any variable. You can't do that in Java only because Java doesn't let you take references to variables.
- brabel 2y agoPlease don't forget Dart! https://medium.com/dartlang/dart-3-1-a-retrospective-on-functional-style-programming-in-dart-3-a1f4b3a7cdda https://medium.com/dartlang/dart-3-1-a-retrospective-on-func...