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?
Your code is nested lambdas that return both tasks and results.
Result.map returns a result
Do you have an example of how to read an integer from stdin?
Task.await (called by !
) returns a task/requires a task
So they aren't capatible
https://www.roc-lang.org/packages/basic-cli/Task#fromResult should help
shift = Str.toI8 shiftStr |> Task.fromResult!
That will let you just accumulate the errors
Could also just use a raw when ... is
on the result.
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
Mainly to just not care about the error path
This version works too
shiftStr = Stdin.line!
shift = (Str.toI8 shiftStr |> Task.fromResult)!
The parens are required in that last example? If so, pretty sure that is a bug in the !
desugaring
yes the parens are required
yeah should be able to do |> Task.fromResult!
instead, sounds like a bug :thumbs_up:
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 !
.
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
Result.map
returns a Result
Task.await
requires an inner function that returns a Task
So it is a type mismatch
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.
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