Stream: beginners

Topic: How to use Stdin.line and a function returning a result aftr


view this post on Zulip Matthieu Pizenberg (May 09 2024 at 13:11):

I have the following code:

shiftStr = Stdin.line!
shift <- Str.toI8 shiftStr |> Result.map
dbg shift

and the compiler doesn’t like it, giving me a long error about a type mismatch between something which is a Task ... and something which is a Result .... Maybe there is a section in the tutorial I’ve skipped that give an example similar to this I’m trying to do?

view this post on Zulip Brendan Hansknecht (May 09 2024 at 13:17):

Your code is nested lambdas that return both tasks and results.

view this post on Zulip Brendan Hansknecht (May 09 2024 at 13:17):

Result.map returns a result

view this post on Zulip Matthieu Pizenberg (May 09 2024 at 13:17):

Do you have an example of how to read an integer from stdin?

view this post on Zulip Brendan Hansknecht (May 09 2024 at 13:18):

Task.await (called by !) returns a task/requires a task

view this post on Zulip Brendan Hansknecht (May 09 2024 at 13:18):

So they aren't capatible

view this post on Zulip Brendan Hansknecht (May 09 2024 at 13:21):

https://www.roc-lang.org/packages/basic-cli/Task#fromResult should help

view this post on Zulip Brendan Hansknecht (May 09 2024 at 13:21):

shift = Str.toI8 shiftStr |> Task.fromResult!

view this post on Zulip Brendan Hansknecht (May 09 2024 at 13:22):

That will let you just accumulate the errors

view this post on Zulip Brendan Hansknecht (May 09 2024 at 13:22):

Could also just use a raw when ... is on the result.

view this post on Zulip Matthieu Pizenberg (May 09 2024 at 13:25):

Thanks ok. I was trying the exercise of having all in one indentation. This seems to work

shiftStr = Stdin.line!
shift <- Str.toI8 shiftStr |> Task.fromResult |> Task.await

view this post on Zulip Matthieu Pizenberg (May 09 2024 at 13:26):

Mainly to just not care about the error path

view this post on Zulip Matthieu Pizenberg (May 09 2024 at 13:27):

This version works too

    shiftStr = Stdin.line!
    shift = (Str.toI8 shiftStr |> Task.fromResult)!

view this post on Zulip Brendan Hansknecht (May 09 2024 at 16:28):

The parens are required in that last example? If so, pretty sure that is a bug in the ! desugaring

view this post on Zulip Matthieu Pizenberg (May 09 2024 at 16:51):

yes the parens are required

view this post on Zulip Richard Feldman (May 09 2024 at 17:21):

yeah should be able to do |> Task.fromResult! instead, sounds like a bug :thumbs_up:

view this post on Zulip Anton (May 10 2024 at 09:06):

I would advise against mixing ! and <- (backpassing) in the same code unless you're very experienced with both. We may get rid of backpassing in favor of !.

view this post on Zulip Karakatiza (May 10 2024 at 11:37):

Does anyone know why specifically the initial version doesn't compile? I thought syntactically it is valid?

Matthieu, are there any other statements after dbg in your original code? If not, maybe the body of the function is incomplete - you declare a backpassed argument, but then you might have no statements which I think is invalid syntax, you would atleast need to return something, e.g. that argument

view this post on Zulip Brendan Hansknecht (May 10 2024 at 12:40):

Result.map returns a Result

view this post on Zulip Brendan Hansknecht (May 10 2024 at 12:40):

Task.await requires an inner function that returns a Task

view this post on Zulip Brendan Hansknecht (May 10 2024 at 12:40):

So it is a type mismatch

view this post on Zulip Ian McLerran (May 10 2024 at 14:51):

Speaking of mixing back passing and bang syntax, I discovered a bug where using the bang operator in a when block causes a compiler panic, but back passing does not. Haven’t got a min repo yet though, so haven’t determined for sure that it’s related to the when.

view this post on Zulip Matthieu Pizenberg (May 10 2024 at 15:16):

Karakatiza said:

Does anyone know why specifically the initial version doesn't compile? I thought syntactically it is valid?

Matthieu, are there any other statements after dbg in your original code? If not, maybe the body of the function is incomplete - you declare a backpassed argument, but then you might have no statements which I think is invalid syntax, you would atleast need to return something, e.g. that argument

yep, as Brendan says, it was a type mismatch between a Result and a Task


Last updated: Jul 06 2025 at 12:14 UTC