Stream: beginners

Topic: PSA: import files as Str/List U8


view this post on Zulip Brendan Hansknecht (Apr 11 2023 at 16:16):

Hi all, I recently added a new feature that I think may be useful to others. It enables importing files directly as a Str or a List U8.

There is a silly example in the repo of a roc file that prints it's own source code. There is also an example of summing the bytes of a file.

The syntax is:

imports [
    "some-file" as someStr : Str,
    "some-file" as someBytes : List U8,
]

This feature is now available in the latest nightly from 2023-04-11.

view this post on Zulip Ayaz Hafiz (Apr 11 2023 at 17:18):

finally: a roc quine

view this post on Zulip Chris Duncan (Apr 11 2023 at 21:48):

What happens if the file doesn't exist or there's no read permission?

view this post on Zulip Brendan Hansknecht (Apr 11 2023 at 21:50):

A compilation error

view this post on Zulip Richard Feldman (Apr 13 2023 at 12:07):

Ayaz Hafiz said:

finally: a roc quine

April Fools idea: change the whole roc-lang.org webpage to be a pitch for how Roc is a programming language designed for speedrunning quines

view this post on Zulip Richard Feldman (Apr 13 2023 at 12:07):

thanks @Brendan Hansknecht , this is awesome! :heart_eyes:

view this post on Zulip Richard Feldman (Apr 13 2023 at 12:09):

I missed the PR, but I'm curious - what happens if you annotate its type as _ and then use it as something other than Str or List U8?

view this post on Zulip Richard Feldman (Apr 13 2023 at 12:10):

also curious if the : is optional syntactically, given that : _ is allowed :smiley:

view this post on Zulip Richard Feldman (Apr 13 2023 at 12:10):

(side note: it's fun to come back from a 2-day conference and discover awesome new features have landed in the meantime! :heart: )

view this post on Zulip Brendan Hansknecht (Apr 13 2023 at 14:08):

: _ just works. It is actually used in the summing bytes example linked above

view this post on Zulip Brendan Hansknecht (Apr 13 2023 at 14:09):

I don't think the : is optional. Currently, this does not really have any special parsing. It parses a string, as , and then a type annotation. I think a type annotation requires the :. I guess that could be changed to make it optional

view this post on Zulip Ayaz Hafiz (Apr 13 2023 at 15:17):

To Brendan’s note- If you annotate as _, the inferred type would be List U8

view this post on Zulip Brendan Hansknecht (Apr 13 2023 at 15:21):

You are correct. I thought at one point I had it working with both, but maybe that was when the PR was broken in a different way. So, I guess, fixing other constraint bugs made it always List U8.

view this post on Zulip Richard Feldman (Apr 14 2023 at 11:57):

to clarify, what I meant about the inference is: suppose I annotate it as : _ and then use it as a Dict or something - do I get a type mismatch saying it can only unify to Str or List U8?

view this post on Zulip Richard Feldman (Apr 14 2023 at 11:58):

also, I really like the idea of having the annotation syntax be optional - that's consistent with how annotations work in general, plus it means tutorials can use the feature even before they teach type annotations! :smiley:

view this post on Zulip Brendan Hansknecht (Apr 14 2023 at 15:43):

Currently, if you annotate as : _, the type will be List U8. If you use it as any other type, that will be a type mismatch. Also, if you annotate as any other type than List U8 or Str, it will be a type mismatch.

view this post on Zulip Richard Feldman (Apr 14 2023 at 17:04):

oh interesting - so when you do : _ it's not actually doing type inference?

view this post on Zulip Brendan Hansknecht (Apr 14 2023 at 18:17):

Nope. Was talking to ayaz about that. Would require more work to be done correctly. Would need a type like RangedNum but specific for ingested files. I initially had a version that kinda did type inference, but it would break if the type was used as List a where a is not U8. Currently, it just picks the first type the matches. Given any type matches _, List U8 wins.


Last updated: Jul 06 2025 at 12:14 UTC