One of the things I like about the new Apple language Swift is that it combines procedural programming with algebraic data types. What other languages do this?
-
meta.programmers.stackexchange.com/questions/6483/…gnat– gnat2014-06-30 16:06:13 +00:00Commented Jun 30, 2014 at 16:06
-
2Technically, this is neither a list nor shopping question. It has a simple, objective, canonical answer: "Yes".Jörg W Mittag– Jörg W Mittag2014-06-30 17:30:15 +00:00Commented Jun 30, 2014 at 17:30
-
@JörgWMittag meta.stackexchange.com/a/183183/165773gnat– gnat2014-06-30 18:39:34 +00:00Commented Jun 30, 2014 at 18:39
-
Edited to address Jorg's point. Yes or no wasn't the answer I was hoping for ;)John DeHope– John DeHope2014-06-30 19:41:57 +00:00Commented Jun 30, 2014 at 19:41
-
I'm not asking anyone to recommend anything.John DeHope– John DeHope2014-07-01 17:36:48 +00:00Commented Jul 1, 2014 at 17:36
3 Answers
ML family: OCaml, SML, F#
ML was (AFAIK) the first language to directly support algebraic data types; ML and its descendants have always at least had mutable state through reference variables.
Racket
Within the Racket family there is #lang plai which directly provides algebraic types.
#lang racket supports the same style of program using multiple subtypes of a struct type and using the match macro. There's a good talk on the relationship between algebraic types and this design in this talk, part of the Brown online PL course.
Typed Racket provides explicit union types and matching through the match macro.
Scala
Scala's case classes are algebraic types built on top of the JVM's class system.
Others
Wikipedia has a list of languages with algebraic data types; languages on this list with mutability include Nemerle and haXe.
I've conflated match style operations with algebraic datatypes here, partly because convenience is really all that separates some of these languages from assembly; you can define ad-hoc algebraic types in any language you like, it just won't be very nice.
-
Thanks, that wikipedia page was pretty much what I was looking for. Appreciate it!John DeHope– John DeHope2014-07-01 17:42:14 +00:00Commented Jul 1, 2014 at 17:42
You can go full imperative in ML-family languages (e.g. Ocaml) while enjoying ADTs and pattern matching.
You can have a sort of ADT in Algol descendants like C and Pascal by using variant records. But you will get very little syntactic sugar. You'll have to pack/unpack and check the variants manually, without the static type safety that e.g. Haskell would give you.
-
I would dare say that you can go full imperative in Haskell too, by using monads. Maybe some Haskell expert can comment on this.Giorgio– Giorgio2014-06-30 18:25:54 +00:00Commented Jun 30, 2014 at 18:25
-
2@Giorgio Some people consider Haskell the "world's finest imperative language."Doval– Doval2014-06-30 18:39:38 +00:00Commented Jun 30, 2014 at 18:39
-
@Doval: That's why I wanted to mention Haskell. ;-)Giorgio– Giorgio2014-06-30 19:00:04 +00:00Commented Jun 30, 2014 at 19:00
Java/C# don't support ADTs out of the box but with some boilerplate and lambdas you can pull it off.
If I'm not mistaken that's also pretty much what Scala's case classes are, minus the boilerplate.
-
Yes, Scala case classes are used to model algebraic data types. +1Giorgio– Giorgio2014-06-30 18:26:49 +00:00Commented Jun 30, 2014 at 18:26