Stream: ideas

Topic: Conditional compilation


view this post on Zulip Johan Lövgren (May 13 2024 at 11:18):

Does module params + comp time = conditional compilation?

This is a thought I have had in the back of my mind for a while. With the suggested module params and compilation time evaluation features, I wonder if it is possible to enable a form of conditional compilation for API design? (Possibly some other changes to the compiler are necessary, as I am not sure comp time enables the example below.)

Here is an example. Imagine there is a module that defines some sorting function for lists, but where it can choose which implementation to export based on a setting parameter that is passed to the module.

Sorting.roc:

module { setting} -> [sort]

sort =
    when setting is
    ShortList -> sortShortList
    LongList -> sortLongList

sortShortList = #implementation optimized for short lists
sortLongList = #implementation optimized for long lists

Then I could import and use this module in my code as

setting = ShortList
import Sorting {setting}

sortedList = lst |> Sorting.sort

With the optimized version for my case, without needing to check the when-is condition on each call of Sorting.sort.

view this post on Zulip Richard Feldman (May 13 2024 at 11:58):

potentially! We have an open question for what module params should compile to

view this post on Zulip Richard Feldman (May 13 2024 at 11:59):

if they compile to adding a hidden extra param to all the module's top-level declarations, then whether or not this would be free at runtime would depend on whether inlining happened (which would be based on a heuristic)

view this post on Zulip Richard Feldman (May 13 2024 at 12:01):

we need to at a minimum support this way of compiling module params, because it's the only way to do it when modules are imported in the middle of functions

view this post on Zulip Richard Feldman (May 13 2024 at 12:01):

but there's a separate question of whether we should also support additional compilation strategies for top-level imports

view this post on Zulip Richard Feldman (May 13 2024 at 12:02):

or for imports that are "effectively top-level" (as in, they are written inline but they only depend on top-level values)

view this post on Zulip Johan Lövgren (May 13 2024 at 17:45):

Great, thanks. All good considerations to keep in mind


Last updated: Jun 16 2026 at 16:19 UTC