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.
finally: a roc quine
What happens if the file doesn't exist or there's no read permission?
A compilation error
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
thanks @Brendan Hansknecht , this is awesome! :heart_eyes:
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
?
also curious if the :
is optional syntactically, given that : _
is allowed :smiley:
(side note: it's fun to come back from a 2-day conference and discover awesome new features have landed in the meantime! :heart: )
: _
just works. It is actually used in the summing bytes example linked above
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
To Brendan’s note- If you annotate as _, the inferred type would be List U8
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
.
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
?
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:
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.
oh interesting - so when you do : _
it's not actually doing type inference?
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