Unboxed wrapper types (newtypes) #1398
Replies: 19 comments 1 reply
-
Can it using the existing syntax but just be an optimisation on single value types. e.g.
Because there is only ever one variant is there a reason this needs to produce the tuple Otherwise I would expect something like
But I think that is already used for type aliases. |
Beta Was this translation helpful? Give feedback.
-
That is the approach that OCaml takes but at the moment I would like to have consistency in the code generation in order to make Erlang FFI more straightforward and to have fewer exceptions to the rules of the language. At the very least I would like to see what syntax ideas we can come up with.
What would the syntax be for boxing and unboxing the newtype in this case? It seems there is no constructor? |
Beta Was this translation helpful? Give feedback.
-
I guess construction makes sense if it looks like let cents = Cent(5)
let Cent(number) = cents Perhaps that suggests a syntax of
Questions is there interest in a type that at runtime just puts values in a tuple. Such a thing might be useful for how erlang sometimes represents times or ip addresses. In which case |
Beta Was this translation helpful? Give feedback.
-
I don't plan to have the ability to construct records without a tag. If this is desired it can be achieved using a newtype over a tuple. I don't think the syntaxes above fit the Gleam pattern of |
Beta Was this translation helpful? Give feedback.
-
In Nim that would be |
Beta Was this translation helpful? Give feedback.
-
ooh thanks! I had not looked at Nim |
Beta Was this translation helpful? Give feedback.
-
Nim's way to construct such a value also makes me think of the units of measure notation in F# but that's a completely different feature. |
Beta Was this translation helpful? Give feedback.
-
Wow, that's amazing. F# has so much interesting stuff in it, I need to learn it! |
Beta Was this translation helpful? Give feedback.
-
In that case I'd recommend reading this book, it gives a good idea what Gleam can be for the BEAM ecosystem in very practical terms. |
Beta Was this translation helpful? Give feedback.
-
Oh dear, my just as I thought I was starting to get through books faster than I collected them 😆 Thanks for the recommendation, it sounds great |
Beta Was this translation helpful? Give feedback.
-
Just ordered a copy! :) |
Beta Was this translation helpful? Give feedback.
-
This feature would be great addition. I am thinking of using some reserved keyword to make it explicit. Such as:
where |
Beta Was this translation helpful? Give feedback.
-
If all wrapper types use the same constructor it becomes hard to know what type is to be constructed during type inference. If given an Int and there are two wrappers if Int then we would need a type annotation to continue, which is somewhat more verbose. |
Beta Was this translation helpful? Give feedback.
-
Kotlin's equivalent are inline classes. About the syntax, I'm in favor of just adding a keyword, for example: value type Cent {
Cent(Int)
} or wrapper type Cent {
Cent(Int)
} This has several advantages:
|
Beta Was this translation helpful? Give feedback.
-
Thank you for the Kotlin example, very useful. I quite like the modifier pub inlined type Cent {
Cent(Int)
} Though perhaps that's confusing when using with pub opaque inlined type Cent {
Cent(Int)
} pub inlined opaque type Cent {
Cent(Int)
} |
Beta Was this translation helpful? Give feedback.
-
I'd be interested in attempting this, although it will probably be bigger/take a while. It looks like we need to introduce a new syntax for the type modifier "inlined", similar to "opaque", which semantically functions similar to "opaque" in the type system and produces an error if there is more than one constructor, or the constructor has a different name than the type name. Is this right? |
Beta Was this translation helpful? Give feedback.
-
Yes, that's right! The type name and the constructor name could be different though. |
Beta Was this translation helpful? Give feedback.
-
As an elixirist I would like it to be something like that (example syntax):
so that a custom type with newtype can have more than one constructor, only max one per basic type, which works thanks to guards at runtime, and they are incompatible. So to convert from A int to B int I'd have to:
|
Beta Was this translation helpful? Give feedback.
-
For completeness of the thread, there was a closed PR from @brightly-salty #931, and now @lpil is working on this at #2330 |
Beta Was this translation helpful? Give feedback.
-
Support creation of type wrappers that function like custom types of a single field, require explicit boxing and unboxing, and at runtime all boxing is erased so there is no runtime overhead.
i.e. Cents wraps an Int. At runtime it is just an Int, no record wrapper.
This is called a newtype in Haskell.
What should the syntax be?
Beta Was this translation helpful? Give feedback.
All reactions