Stream: contributing

Topic: ?? Operator


view this post on Zulip Anthony Bullard (Dec 23 2024 at 12:11):

I currently am working on https://github.com/roc-lang/roc/issues/7089 and have this working correctly. I just wanted to make sure this (non-sensical) program is doing what it should.

app [main!] {
    pl: platform "../../basic-cli/platform/main.roc",
}

import pl.Stdout
import pl.Stdin
import pl.Stderr

main! = \_args ->
    tick! {}

tick! = \{} ->
    try Stdout.write! "Enter a number: "
    when Stdin.line! {} is
        Ok str ->
            num =
                Str.toU8 str ?? 255
                |> addOne
            try Stdout.line! "Did you write $(Num.toStr num) (Y/n)"
            when Stdin.line! {} is
                Ok "n" -> Ok {}
                Ok _ -> tick! {}
                Err _ -> Err Done

        Err EndOfFile ->
            try Stderr.line! "EOF"
            Err Done

        Err (StdinErr _) ->
            try Stderr.line! "Couldn't read from stdin"
            Err Done

addOne = \n ->
    dbg { n }
    if n == 255 then
        255
    else
        n + 1

Here it will only actually get it right if you happen to type exactly 255 because the ?? has stronger precedence that |>. Does everyone else expect that that would be the case?

view this post on Zulip Anthony Bullard (Dec 23 2024 at 12:12):

And given that it is, what are all the places I need to look at to add tests around it?

view this post on Zulip Anthony Bullard (Dec 23 2024 at 13:02):

https://github.com/roc-lang/roc/pull/7406 is the PR

view this post on Zulip Richard Feldman (Dec 23 2024 at 14:14):

that's the behavior I would expect!

view this post on Zulip Richard Feldman (Dec 23 2024 at 14:19):

as an aside for the future, this is the first short-circuiting operator implementation we have, so it would be awesome to follow up with changing over && and || to short-circuit as well, per #beginners > Short-circuit on boolean operations

view this post on Zulip Anthony Bullard (Dec 23 2024 at 14:28):

That makes sense. So just desugar these like

idx > 32 && idx + 1  < 79

becomes

if idx > 32 then
  idx + 1 < 79
else
  Bool.false

and

idx > 32 || idx + 1  < 79

becomes

if idx > 32 then
  Bool.true
else
  idx + 1 < 79

?

view this post on Zulip Richard Feldman (Dec 23 2024 at 14:37):

yeah exactly!


Last updated: Jul 06 2025 at 12:14 UTC