Stream: beginners

Topic: A union containing Bool.True and Bool.False?


view this post on Zulip Aurélien Geron (Aug 22 2026 at 11:49):

Consider this code:

f : [Foo, True, False] -> Str
f = |x| Str.inspect(x)

expect f(Foo) == "Foo"
expect f(True) == "True"
expect f(False) == "False"
expect f(2 + 2 == 4) == "True" # Fail!

With roc test, the first three tests pass, but not the last one. I get this error:

── ✗ type mismatch ────────────────────────────────────────── /Users/ageron/dev/roc/exercism/exercism-roc/test2.roc:7:10

The first argument being passed to this function has the wrong type.

expect f(2 + 2 == 4) == "True" # Fail!
         ^^^^^^^^^^

This argument has the type:

    Bool

But f needs the first argument to be:

    [False, Foo, True]

All (3) tests passed in 130.1 ms.

I tried replacing the first line with:

f : [Foo, Bool.True, Bool.False] -> Str

but the error doesn't change.

Is there any way to make this work?

The following works, but it's not exactly what I was going for:

f : [Foo, Bool(Bool)] -> Str
f = |x| Str.inspect(x)

expect f(Foo) == "Foo"
expect f(Bool(True)) == "Bool(True)"
expect f(Bool(False)) == "Bool(False)"
expect f(Bool(2 + 2 == 4)) == "Bool(True)"

view this post on Zulip Luke Boswell (Aug 22 2026 at 12:11):

I imagine you would need something like [Foo, Other(Bool)]

view this post on Zulip Luke Boswell (Aug 22 2026 at 12:12):

But that isn't going to evaluate to "True" because you don't have a Bool

view this post on Zulip Anton (Aug 22 2026 at 12:54):

I tried replacing the first line with:

f : [Foo, Bool.True, Bool.False] -> Str

but the error doesn't change.

Bool is a closed union so it will not auto expand/merge

view this post on Zulip Richard Feldman (Aug 22 2026 at 13:26):

yeah I don't think this can really work :smile:

view this post on Zulip Richard Feldman (Aug 22 2026 at 13:27):

what's the use case?

view this post on Zulip Aurélien Geron (Aug 22 2026 at 20:39):

Thanks for your answers. The use case is an exercism exercise where the user has to write a DSL to build graphs. Each node can have custom attributes whose values can be strings, integers, or booleans. I started with [Text(Str), Int(I64), Bool(Bool)], which works fine, then I though that Bool.True and Book.False are tags, so I could perhaps avoid wrapping the boolean, for simplicity. But I guess not! :sweat_smile:


Last updated: Sep 03 2026 at 15:16 UTC