Stream: beginners

Topic: ✔ Type alias for payload with specific type


view this post on Zulip Austin Clements (Oct 18 2022 at 21:19):

I'm working through Advent and I made a type alias called MoveCommand that consists of a Direction tag union and a Nat for the distance. Here are the definitions:

MoveCommand : Direction Nat
Direction : [Up, Down, Forward]

But it seems that Nat in this case is being interpreted as a type argument to Direction in this case and I get this error:

── TOO MANY TYPE ARGUMENTS ───────────────────────────────────────── day02.roc ─

The Direction alias expects 0 type arguments, but it got 1 instead:

29│  MoveCommand : Direction Nat
     ^^^^^^^^^^^

Are there missing parentheses?

────────────────────────────────────────────────────────────────────────────────

Is there a way for me to create a MoveCommand type that can only consist of a Direction and a Nat without using a type argument, so that Nat is the only option?

view this post on Zulip Luke Boswell (Oct 18 2022 at 21:29):

You could something like

Direction : [Up Nat, Down Nat, Forward Nat]

up : Nat -> Direction
up = \distance -> Up distance

expect
    whichDirection = up 50
    expectedResult = Up 50
    whichDirection == expectedResult

I used List [Fd U64, Up U64, Dn U64] but didn't alias it as its own type at the time as I hadn't figured out how to do that.

view this post on Zulip Luke Boswell (Oct 18 2022 at 21:31):

I find roc repl to be really helpful when trying new type definitions like this.
Screen-Shot-2022-10-19-at-08.30.44.png

view this post on Zulip Richard Feldman (Oct 18 2022 at 21:37):

I made a type alias called MoveCommand that consists of a Direction tag union and a Nat for the distance

so the way this is written, it's actually specifying something else:

MoveCommand : Direction Nat

this is syntactically equivalent to MoveCommand : List Nat - it's basically saying "MoveCommand is a type alias for a Direction of Nats, because Direction is a type that has a type parameter (just like List does), and I'm specifying Nat for that type parameter here"

view this post on Zulip Richard Feldman (Oct 18 2022 at 21:39):

so if you want it to store both, you could make it a record, e.g.

MoveCommand : {
    direction : Direction,
    distance : Nat
}

view this post on Zulip Austin Clements (Oct 18 2022 at 21:47):

Gotcha that makes sense, thanks to you both :+1:

view this post on Zulip Notification Bot (Oct 19 2022 at 01:36):

Austin Clements has marked this topic as resolved.


Last updated: Jul 06 2025 at 12:14 UTC