#Runtime error when exposing private fn through pub Const

1 messages · Page 1 of 1 (latest)

shy prawn
#

Not sure what is the expected behavior here, but I think maybe it should be a compile-error (similar to when exposing types that are not public)

I have the following in my example:

wobble.gleam

pub type Api {
  Api(method: fn(String) -> String)
}

pub const api1 = Api(impl)

fn impl(s: String) {
  "Prefix " <> s
}

wibble.gleam

import wobble.{api1}
import gleam/io

pub fn main() {
  api1.method("test")
  |> io.debug()
}

When running with:

gleam run -m wibble

I get the following error:

exception error: undefined function wobble:impl/1
in function wibble:main/0 (/Users/john.bjork/git/glimt/build/dev/erlang/glimt/_gleam_artefacts/wibble.erl, line 8)

west spoke
#

ha thats a fun bug, constants are inlined so the imaginary actual gleam in wibble is

Api(wibble.impl).method("test")
|> io.debug
weak ingot
#

I didn't know you could use custom types in constants?!

west spoke
#

you can yeah, but no other function calls

weak ingot
#

this. changes. everything.

west spoke
#

gleam can do this because custom types are just tagged tuples under the hood so we can always inline them

shy prawn
#

So I guess then the expected behavior here should be a compile-time error? I can create an issue for that.

west spoke
#

Yeah i think so, an issue would be great!

shy prawn
# weak ingot this. changes. everything.

Yes, the original context here is that I provide a styling module in a logger and I want to group functions for styling different parts of the log-message into a unit that I can pass when building the logger. My original plan was to keep the actual functions private and only expose them through constants, but I guess there is no real harm in exposing them as well (just makes the docs a bit more cluttered)