Is there a way to alias a module import?
I made Bytes.Decode
and Bytes.Encode
modules which expose functions with shared names, e.g. Bytes.Decode.u16
and Bytes.Encode.u16
I can't do Bytes.Decode.{u16}
and expose them unqualified because it would clash with Bytes.Encode.u16
you can at least use these functions in a qualified way
I'm not sure if aliasing of imports themselves works
Right, yeah. I'm just wondering whether there is a way to alias so I don't have to type the full module name.
Is that something intentionally left out?
I can see some drawbacks with aliasing myself. I'm curious about what would be the recommendation for cases like this
I don't think it's intentional, no
though qualifying functions is encouraged in roc (like in elm)
here you could just alias in the actual code of your module, as a stopgap
Gotcha
In Elm, I'm used to aliasing Json.Encode
to JE
and Json.Decode
to JD
, but that might be more a habit than anything else.
In this case, I wanted to write:
msgDecoder =
messageType <- BD.await BD.u8
messageLength <- BD.await BD.i32
when messageType is
'R' ->
authMsgDecoder
_ ->
BD.succeed (UnknownMessageType messageType)
authMsgDecoder =
success <- BD.await BD.i32
if success == 0 then
BD.succeed AuthenticationOk
else
BD.succeed AuthenticationFailed
right, and Bytes.Encode as BE
does not work ?
No, I get a syntax error:
13│ pg.Bytes.Decode as BD,
^
I am expecting a comma or end of list, like
imports [Shape, Vector]⏎
ok. @Richard Feldman we would want this to work, right?
yes!
it's just never been implemented :sweat_smile:
Added an issue #5105 to track this.
Last updated: Jul 06 2025 at 12:14 UTC