External const #1414
Replies: 5 comments 17 replies
-
That's an interesting idea! I can see how it would be useful with JavaScript. What would this do when compiling to Erlang? I should note that the definition that uses an anonymous function may not work in modules that have another |
Beta Was this translation helpful? Give feedback.
-
I can't answer that since I'm not familiar with Erlang. One possibility is to only allow |
Beta Was this translation helpful? Give feedback.
-
I think the language needs to be consistent across all targets as much as possible, so we would need to determine what it does on Erlang also. Erlang doesn't have importable constants, so we'd need to do something else there. I'm going to turn this into a discussion as we've not got a firm design yet. Once we have that we can move back here. |
Beta Was this translation helpful? Give feedback.
-
Just discovered this since I was interested in making an "external constant" for Erlang. One possibility there would be to generate a function and an // example: can be a non-negative integer or the atom `infinity`
external type Timeout
external const zero: Timeout = "0"
external const infinity: Timeout = "infinity" ⬇️ -compile({inline, [infinity/0, zero/0]}).
infinity() -> infinity.
zero() -> 0. You could also use a macro, but I think it would be a bit more complicated for the Gleam compiler to implement (the definition has to live in a separate |
Beta Was this translation helpful? Give feedback.
-
New suggestion: external type Timeout
external const zero: Timeout = "myapp_ffi" "zero"
external const infinity: Timeout = "myapp_ffi" "infinity" -module(myapp_ffi).
-export([infinity/0, zero/0]).
infinity() -> infinity.
zero() -> 0. Advantages:
The main disadvantage is that you could do some wacky stuff with "constants" since they are actually a function call, which is a notable non-consistency with how Gleam constants work, though it's at least no worse than the native "constant" situation in Erlang. It might also feel strange to apply this paradigm to JavaScript where actual constants exist and it might be preferable to use them over function calls (for performance reasons?). It could be made to work differently in JS, where instead of a function the name must refer to an exported value; |
Beta Was this translation helpful? Give feedback.
-
Gleam currently has
external fn
andexternal type
. However, it is difficult to access an external value. For example, to get thewindow
object in Javascript, one must do:It would be much nicer if we could instead write:
Beta Was this translation helpful? Give feedback.
All reactions