Does @tag("x") help with nested variants?

Starting with

type errorType = NeedAuthToken | BadAuthToken

type t =
  | Error(errorType)
  | Loading({ready: bool})
  | Done({data: string})

Responses.Error(Responses.NeedAuthToken)

we get

{ TAG: 'Error', _0: 'NeedAuthToken' }

Using the new @tag(“state”) like

type errorType = NeedAuthToken | BadAuthToken

@tag("state")
type t =
  | Error(errorType)
  | Loading({ready: bool})
  | Done({data: string})

I get

{ state: 'Error', _0: 'NeedAuthToken' }

But can we go deeper? Can we get rid of the weird key _0 ?

Yes, if you use inline records then you’ll get the fields directly on the object.

1 Like