External const #1414
Replies: 6 comments 18 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.
-
I was just about to create a discussion related to this topic, but found this old one! I'd completely forgotten just how much the Gleam language has changed and evolved over time. I'll preface this comment with the fact that I don't have much exposure to Erlang as a language, so my ability to discuss Erlang specific concepts might be severely lacking 😅. I recognize that there's a lot of potential for the JS target with this, especially with the global I've been utilizing Now, obviously there are some work-arounds that I can utilize in my specific case to get around this:
I'm not super excited about either of these work-arounds, but they would work. Utilizing functions instead feels like a lot of changes, and doesn't feel great that I was fine with all my other records until I got to this one. Now I would feel the need to change all of my other defaults for consistency because of this one instance. And the array of tuples solutions would potentially make my code a lot less performant/readable when compared to just using a dict. So I guess my question would be, is this something that's at all desirable for Gleam? Are there some current philosophies in its design that a feature like this would go against? Would love to hear feedback on this and potentially caveats I might be missing! I know there are some intricacies with Erlang and this proposal that could make a feature like this tricky. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
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