DEV Community

Discussion on: If you were designing a programming language what would your favourite features be?

Collapse
bootcode profile image
Robin Palotai

Good question. There's a set of features I can't easily live with nowadays (like generics you mentioned), but let me focus on one of the most neglected language feature:

ADTs (Algebraic Data Types, aka enum / union on steroids, case classes):

Most languages have records, which store various data together. But few has ADTs, which let case selection on a closed set of named records. For example a Haxe-style enum*:

// plain enum so far
enum NumberSystem {
  Roman;  // I, II, ..
  Arabic;  // 1, 2, ..
}

// ADT-y enum
enum MyNumber {
  Numeral(value: Int, writtenIn: NumberSystem);  // I, II, 3, ..
  Textual(value: Int, rawValue: String);  // "one", "hundred", ...
}

var num1: MyNumber = Numeral(1, Roman);
var num2: MyNumber = Textual(25, "twenty-five");

// silly example, a better one would be a render
// function which gives back the original representation
var v = switch(num1) {
  case Numeral(v, _): return v;
  case Textual(v, _): return v;
};

ADTs can be emulated by subclassing to some extent, but then you need to resort to instanceof hacks, which defeats the purpose. Also no guarantee on completeness on cases checked that way.

It also brings a different mindset compared to subclassing: with subclassing, I tried to massage all differences of the subs into a generic interface. With ADTs, I embrace differences and handle them explicitly.

Typically languages with ML (sml, ocaml, mlton..) or Haskell-lineage (haskell, elm, purescript..) have ADTs. Scala has case classes which can be sealed to get similar effect. C has tagged unions which is similar if we squint, but forces manual rolling of tag-to-part mapping.

Apart from this, on the high level I would love an integrated language experience, where all the tooling and syntax work together in unison, batteries-included, with as much upfront guarantees as possible. Ur/web is an experimental (now in maintenance mode) language that kind of promised this for the syntax at least, but in practice didn't satisfy me. Also far from production. From the old times, Turbo Pascal had a nice experience, but it didn't have SQL integrated or any modern language features either.

They say SmallTalk had a nice closed world you could live in, but I have no experience.

*: I don't like Haxe's Color enum example, as the constructors are not orthogonal (red, blue, green, rgb(r,g,b)). Not that they can always be, but this is a particularly bad first example.

Collapse
dansilcox profile image
Dan Silcox Author

Thanks for your post, I knew I was going to learn so much by asking this question - never even heard of Haxe! I like the example and I agree that native ADT (or at the very least enum) support is definitely better than using user-land sub-classing for the same thing.

Yes I've heard good things about SmallTalk too, but never used it myself.

Collapse
bootcode profile image
Robin Palotai

Haxe is a curiosity I would say - compiler written in Ocaml, compiles very fast, and has a balanced feature set. Has both server- and client-side runtimes (lots of targets actually).

I used it in the past when developed flash games, it (the language) was cleanly superior to ActionScript. ActionScript (and flash) on the other hand came with the Adobe IDE (don't remember the name), and people more interested in writing games than in programming languages produced more shiny stuff. Classic fallacy, writing game engine instead of game..

The limiting factor last time I looked at it was the cross-platform graphics stuff (Neash/NME), which tried to stuff the gfx API behind a flash-like interface. The experience was not always smooth on all targets. Wonder how that fares today.

The original developers, Motion Twin used it in a full-stack fashion. It backed most of their games AFAIK, for example Die2Nite. It seems the author (Nicolas Cannassee, @ncannassee) is up to something again, see youtube.com/watch?v=SfwLOOkcSfo.