Stream: beginners

Topic: ✔ why "NOT END OF FILE" error?


view this post on Zulip Ivo Balbaert (Jun 10 2023 at 08:23):

Hi all! Working through the tutorial (excellently written!), trying out code and not being used to work with functional languages, I came across a few error messages which I find baffling, but perhaps are easily explained by experienced Roc developers. I also didn't find an explanation in the GitHub issues, nor after searching here (Sorry if it has been asked before).
Here is one of them. The following program runs correct when each of the if-then-else expressions are executed separately:
(I use the basic-cli 0.3.2 platform / Ubuntu-WSL / latest nightly Roc build)

main =
    if rec1 == rec2 then
        Stdout.line "rec1 is equal to rec2"
    else
       Stdout.line "rec1 is NOT equal to rec2"

    if rec1 == rec4 then
       Stdout.line "rec1 is equal to rec4"
    else
       Stdout.line "rec1 is NOT equal to rec4"

# output compiler:
# ── NOT END OF FILE ───────────────────────────────────────── records_equal.roc ─

# I expected to reach the end of the file, but got stuck here:

# 17│      if rec1 == rec4 then

But when they are both executed, I get the above error, which I totally don't understand.
Any explanation would be very much appreciated!

view this post on Zulip Luke Boswell (Jun 10 2023 at 10:49):

If you want to chain two Tasks to run in sequence you will need to use Task.await, or Task.attempt etc. main just expects a single value to be provided, ideally a Task {} *

view this post on Zulip Luke Boswell (Jun 10 2023 at 10:50):

Sorry on my phone rn so can't write out a better example

view this post on Zulip Luke Boswell (Jun 10 2023 at 10:52):

If you do something like

{} <- Stdout.line "hi" |> Task.await
{} <- Stdout.line "another hi" |> Task.await
Task.succeed {}

etc is one way to do this

view this post on Zulip Luke Boswell (Jun 10 2023 at 10:54):

Actually I just wrote up some basic docs for using Tasks earlier today here might help. Also here is an example that hasnt yet landed in the Examples repo, which might also help.

view this post on Zulip Richard Feldman (Jun 10 2023 at 11:36):

yeah, this is not the most helpful error message! :sweat_smile:

here's a way you could refactor this that should compile:

main =
    msg1 =
        if rec1 == rec2 then
            "rec1 is equal to rec2"
        else
            "rec1 is NOT equal to rec2"

    msg2 =
        if rec1 == rec4 then
            "rec1 is equal to rec4"
        else
            "rec1 is NOT equal to rec4"

    {} <- Stdout.line msg1 |> Task.await

    Stdout.line msg2

view this post on Zulip Richard Feldman (Jun 10 2023 at 11:38):

another way that should also work:

main =
    task1 =
        if rec1 == rec2 then
            Stdout.line "rec1 is equal to rec2"
        else
            Stdout.line "rec1 is NOT equal to rec2"

    task2 =
        if rec1 == rec4 then
            Stdout.line "rec1 is equal to rec4"
        else
            Stdout.line "rec1 is NOT equal to rec4"

    {} <- task1 |> Task.await

    task2

view this post on Zulip Ivo Balbaert (Jun 10 2023 at 12:01):

Thanks so much to both of you for these helpful answers :smile: This seems to solve a lot of my problems. It had to be some misunderstanding on my part. I think I gained a fundamental insight here :blush:

view this post on Zulip Richard Feldman (Jun 10 2023 at 12:45):

awesome, happy to help! :smiley:

view this post on Zulip Notification Bot (Jun 11 2023 at 11:53):

Ivo Balbaert has marked this topic as resolved.


Last updated: Jul 06 2025 at 12:14 UTC