Compiler: Proposal: Steal F#'s nice union type syntax

Created on 17 May 2016  Â·  8Comments  Â·  Source: elm/compiler

tl;dr write union types with a | before each case, like so:

type Maybe a =
    | Just a
    | Nothing

Motivation

The current syntax for defining union types has these problems:

  • It causes needless version control diffs when removing a tag that happens to be in the first position
  • Its multi-line definitions are visually inconsistent with other definitions in Elm, which have = at the end of the line instead of the beginning of the following line
  • Its multi-line definitions are visually inconsistent with its own single-line definitions, meaning switching from single-line to multi-line also entails a larger-than-necessary version control diff

Proposed solution

Do what F# does: require that each tag be on a different line, and preceded by a |.

Examples

Multi-Line

From evancz/elm-todomvc

type Msg
    = NoOp
    | UpdateField String
    | EditingTask Int Bool
    | UpdateTask Int String
    | Add
    | Delete Int
    | DeleteComplete
    | Check Int Bool
    | CheckAll Bool
    | ChangeVisibility String

becomes

type Msg =
    | NoOp
    | UpdateField String
    | EditingTask Int Bool
    | UpdateTask Int String
    | Add
    | Delete Int
    | DeleteComplete
    | Check Int Bool
    | CheckAll Bool
    | ChangeVisibility String

Single-Line

This is currently supported, but would no longer be.

type Bool = True | False

becomes

type Bool =
    | True
    | False

Split-Line

This is currently supported, but would no longer be.

type Maybe a = Just a
    | Nothing

becomes

type Maybe a =
    | Just a
    | Nothing

Most helpful comment

What if the preceding bar was optional, not required? That would be a non-breaking syntax change.

All 8 comments

As an aside, I don't think this is in any way urgent. I just think it's a good idea and wanted to document it.

What if the preceding bar was optional, not required? That would be a non-breaking syntax change.

I'd prefer that to status quo, since then I could write my stuff in the
nice way, so sure! :)

On Mon, May 16, 2016, 6:16 PM Max Goldstein [email protected]
wrote:

What if the preceding bar was optional, not required? That would be a
non-breaking syntax change.

—
You are receiving this because you authored the thread.
Reply to this email directly or view it on GitHub
https://github.com/elm-lang/elm-compiler/issues/1385#issuecomment-219594216

Thanks. Added to #1375. In case any OCaml people ever see this, they did it first ;)

As a side note, with a single case union in F# the first bar is entirely optional:

So

type Msg = UpdateField of string

Is valid syntax rather than having to do:

type Msg =
    | UpdateField of string

If single line unions are no longer allowed as proposed, why not just get rid of the | syntax altogether? Just use indentation as with case.

Maybe, leading bar could be made optional?

@Heimdell See my May 16th comment...

Was this page helpful?
0 / 5 - 0 ratings

Related issues

evancz picture evancz  Â·  4Comments

willnwhite picture willnwhite  Â·  4Comments

jvoigtlaender picture jvoigtlaender  Â·  4Comments

maxsnew picture maxsnew  Â·  4Comments

maxsnew picture maxsnew  Â·  3Comments