Stream: beginners

Topic: weird compilation error


view this post on Zulip Artur Swiderski (Nov 25 2022 at 20:00):

code :
app "hello"
packages { pf: "/home/arti/roc/examples/cli/cli-platform/main.roc" }
imports [pf.Stdout, pf.Program]
provides [main] to pf

main =
num = addAndStringify 4 5
Stdout.line "hiden number \(num)" |> Program.quick

addAndStringify = \num1, num2 ->
sum = num1 + num2
if sum == 0 then
""
else
Num.toStr (num1 + num2)

error:
roc dev

── TYPE MISMATCH ──────────────────────────────────────────────────── main.roc ─

This 2nd argument to isEq has an unexpected type:

14│ if sum == 0 then
^

The argument is a number of type:

Num *

But isEq needs its 2nd argument to be:

Num a | a has Eq

── TYPE MISMATCH ──────────────────────────────────────────────────── main.roc ─

This 1st argument to addAndStringify has an unexpected type:

9│ num = addAndStringify 4 5
^

The argument is a number of type:

Num *

But addAndStringify needs its 1st argument to be:

Num * | * has Eq

── TYPE MISMATCH ──────────────────────────────────────────────────── main.roc ─

This 2nd argument to addAndStringify has an unexpected type:

9│ num = addAndStringify 4 5
^

The argument is a number of type:

Num *

But addAndStringify needs its 2nd argument to be:

Num * | * has Eq

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

3 errors and 0 warnings found in 39 ms.

You can run the program anyway with roc run

a bit unexpected : )

view this post on Zulip Folkert de Vries (Nov 25 2022 at 20:02):

@Ayaz Hafiz how can we do a better job here?

view this post on Zulip Folkert de Vries (Nov 25 2022 at 20:02):

Num * should always be Eq

view this post on Zulip Artur Swiderski (Nov 25 2022 at 20:03):

is there version which have it done right ?

view this post on Zulip Artur Swiderski (Nov 25 2022 at 20:03):

I mean nightly build ?

view this post on Zulip Richard Feldman (Nov 25 2022 at 20:04):

I think this is https://github.com/roc-lang/roc/issues/4594 probably?

view this post on Zulip Folkert de Vries (Nov 25 2022 at 20:04):

yes looks like it

view this post on Zulip Folkert de Vries (Nov 25 2022 at 20:05):

for now you just need to tell the compiler what types your numbers have

view this post on Zulip Folkert de Vries (Nov 25 2022 at 20:05):

so num = addAndStringify 4i64 5 would probably do it

view this post on Zulip Artur Swiderski (Nov 25 2022 at 20:07):

I tried u8 before and it doesn't worked but I will try again

view this post on Zulip Folkert de Vries (Nov 25 2022 at 20:08):

maybe you need a type signature on the function then

view this post on Zulip Artur Swiderski (Nov 25 2022 at 20:10):

main =
num = addAndStringify 4i8 5i8
Stdout.line "hiden number \(num)" |> Program.quick

addAndStringify = \num1, num2 ->
sum = num1 + num2
if sum == 0i8 then
""
else
Num.toStr (num1 + num2)

view this post on Zulip Artur Swiderski (Nov 25 2022 at 20:11):

works

view this post on Zulip Artur Swiderski (Nov 25 2022 at 20:11):

maybe I messed up before


Last updated: Jul 06 2025 at 12:14 UTC