Stream: beginners

Topic: Import aliases


view this post on Zulip Agus Zubiaga (Mar 07 2023 at 14:54):

Is there a way to alias a module import?

view this post on Zulip Agus Zubiaga (Mar 07 2023 at 14:56):

I made Bytes.Decode and Bytes.Encode modules which expose functions with shared names, e.g. Bytes.Decode.u16 and Bytes.Encode.u16

view this post on Zulip Agus Zubiaga (Mar 07 2023 at 14:57):

I can't do Bytes.Decode.{u16} and expose them unqualified because it would clash with Bytes.Encode.u16

view this post on Zulip Folkert de Vries (Mar 07 2023 at 14:57):

you can at least use these functions in a qualified way

view this post on Zulip Folkert de Vries (Mar 07 2023 at 14:58):

I'm not sure if aliasing of imports themselves works

view this post on Zulip Agus Zubiaga (Mar 07 2023 at 14:58):

Right, yeah. I'm just wondering whether there is a way to alias so I don't have to type the full module name.

view this post on Zulip Agus Zubiaga (Mar 07 2023 at 14:59):

Is that something intentionally left out?

view this post on Zulip Agus Zubiaga (Mar 07 2023 at 15:02):

I can see some drawbacks with aliasing myself. I'm curious about what would be the recommendation for cases like this

view this post on Zulip Folkert de Vries (Mar 07 2023 at 15:02):

I don't think it's intentional, no

view this post on Zulip Folkert de Vries (Mar 07 2023 at 15:03):

though qualifying functions is encouraged in roc (like in elm)

view this post on Zulip Folkert de Vries (Mar 07 2023 at 15:03):

here you could just alias in the actual code of your module, as a stopgap

view this post on Zulip Agus Zubiaga (Mar 07 2023 at 15:04):

Gotcha

view this post on Zulip Agus Zubiaga (Mar 07 2023 at 15:20):

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.

view this post on Zulip Agus Zubiaga (Mar 07 2023 at 15:22):

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

view this post on Zulip Folkert de Vries (Mar 07 2023 at 15:23):

right, and Bytes.Encode as BE does not work ?

view this post on Zulip Agus Zubiaga (Mar 07 2023 at 15:24):

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]⏎

view this post on Zulip Folkert de Vries (Mar 07 2023 at 15:26):

ok. @Richard Feldman we would want this to work, right?

view this post on Zulip Richard Feldman (Mar 07 2023 at 18:36):

yes!

view this post on Zulip Richard Feldman (Mar 07 2023 at 18:36):

it's just never been implemented :sweat_smile:

view this post on Zulip Luke Boswell (Mar 08 2023 at 09:16):

Added an issue #5105 to track this.


Last updated: Jul 06 2025 at 12:14 UTC