Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

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.

Licensed under: CC-BY-SA with attribution
scroll top