Dhall-lang: Enum Types

Created on 26 Sep 2017  路  6Comments  路  Source: dhall-lang/dhall-lang

When writing dhall configs, I find myself using this pattern to specify enums:

Type: { aType: Text }
Type/A: { aType: "a" }
Type/B: { aType: "b" }

and then when typing dhall code, I want a record to be of type Type which Type/A and Type/B are instances of.

But this feels a bit cludge-y to just specify enum types. Are there plans of having enum types in dhall, maybe an alternative to the Union types where I can do:

< Enum: "a" | "b" >

Most helpful comment

Just a note for anyone else finding this from Google, constructors was deprecated because it's no longer necessary. Enums like in the original request are now basically supported, you can do this:

let Enum = < A | B >
in [ Enum.A, Enum.B ]

All 6 comments

Unions are the recommended way to do this:

-- The type
< Type/A : {} | Type/B : {} >  

-- The values
< Type/A = {=} | Type/B : { } >
< Type/A : { } | Type/B = {=} >

Even if Dhall added support for enums as a special case of unions it wouldn't significantly reduce the boilerplate because an enum literal would still need to specify all other alternatives

What I do in cases like these is define synonyms for each alternative, like this:

    let Type/A = < Type/A = {=} | Type/B : { } >
in  let Type/B = < Type/A : { } | Type/B = {=} >
in  [ Type/A, Type/B, Type/B, TypeA ]

... or save the synonyms in named files:

$ cat Type/A
< Type/A = {=} | Type/B : { } >
$ cat Type/B
< Type/A : { } | Type/B = {=} >
$ cat example
[ ./Type/A , ./Type/B , ./Type/B , ./TypeA ]

This helps reduce the boilerplate so you only need to write out the full union literal once per alternative

This answered my question! Thanks a lot!

You're welcome! :)

... or save the synonyms in named files:

This is not really practical for enums with say 10 constructors, right?

You would have ./Type/A with 9 other alternatives; likewise for Type/B, Type/C, etc.

@srid @Koshroy : Note that newer versions of Dhall have a constructors keyword, which provides a nicer solution to this problem. You can now do:

    let type = constructors < A : {} | B : {} >

in [ type.A {=}, type.B {=} ]

Just a note for anyone else finding this from Google, constructors was deprecated because it's no longer necessary. Enums like in the original request are now basically supported, you can do this:

let Enum = < A | B >
in [ Enum.A, Enum.B ]
Was this page helpful?
0 / 5 - 0 ratings

Related issues

ocharles picture ocharles  路  6Comments

michalrus picture michalrus  路  5Comments

jneira picture jneira  路  5Comments

MICHAELABICK picture MICHAELABICK  路  5Comments

bch29 picture bch29  路  4Comments