Syntax: Allow ocaml style leading pipe | in multiline union types

Ocaml based languages like F# and Rescript allow a leading pipe, which enables visual verticall alignment of pipe operators

// fsharp
type Shape =
    | Rectangle of width : float * length : float
    | Circle of radius : float
    | Prism of width : float * float * height : float
// rescript
type myResponse =
  | Yes
  | No
  | PrettyMuch

Also in typescript

// typescript
type ContextType =
    | '2d'
    | 'webgl'
    | 'webgl2'
    | 'webgpu'
    | 'bitmaprenderer'

on scala the first item cannot have left pipe operator

type ContextType =
  "2d"
  | "webgl"
  | "webgl2"
  | "webgpu"
  | "bitmaprenderer"

I’m not for or against this proposal, just like to mention that the Scala analogue of the examples is enum, not union types.

In Scala case keyword replaces the |. For example

enum Shape:
  case Rectangle(width: Float, length: Float)
  case Circle(radius: Float)
  case Prism(width: Float, height: Float)

So type in Fsharp / Ocaml has a very different meaning and purpose than type in Scala, which is just a type alias, not a data definition. enum and unions are sometimes confused in Scala.

There was discussion about making case more concise for pattern matches at least. (People brought up concise Ocaml guard syntax there too.) I think case is a really good keyword for both cases (no pun intended) as I explained there.

2 Likes

I am not against, but in today’s Scala I would format the above as

type CanvasImageSource =
  ( HTMLElement 
  | HTMLImageElement 
  | HTMLVideoElement
  )
3 Likes

Just updated examples to model typescript literal union types, which can only be expresed using scala 3 literal union types, example for Scala.JS

1 Like

Would say, visually looks better, but not enough yet