Hypothetical C++: easy type creation
spiria.com
Hypothetical C++: easy type creation
1–10 of 12 posts
Re: Hypothetical C++: easy type creation
#2Re: Hypothetical C++: easy type creation
#3The "Allow internal functionality" proposal doesn't fully work as-is. Imagine you have a type Scalar, which has a function:
Scalar Scalar::scale(Scalar);
If you now do typedecl Scalar Celcius;
you'd get a function for it with the following signature: Celcius Celcius::scale(Celcius);
However, the function you'd actually want is Celcius Celcius::scale(Scalar);
The root problem here is that since the original type mixes different uses of the type, you can't blindly convert all those uses to the new type.Re: Hypothetical C++: easy type creation
#4Re: Hypothetical C++: easy type creation
#5Re: Hypothetical C++: easy type creation
#6Using single-member structs (as explained in [0]) gets you the basic idea, but that's still missing the automatic lifting of the functions of your underlying element. I'm not sure if there is a sane way to do this given the current ecosystem. Perhaps a syntax like `newtype Celsius = int using std::{+, -, abs}` would work?
[0]: https://blog.nelhage.com/2010/10/using-haskells-newtype-in-c...
Re: Hypothetical C++: easy type creation
#7Sounds a bit like Haskell's newtype and its class derivation. Using single-member structs (as explained in [0]) gets you the basic idea, but that's still missing the automatic lifting of the functions of your underlying element. I'm not sure if there is a sane way to do this given the current ecosystem. Perhaps a syntax like `newtype Celsius = int using std::{+, -, abs}` would work? [0]: https://blog.nelhage.com/2010…
Maybe support will be added later?
Re: Hypothetical C++: easy type creation
#8As another possible way to achieve this, here's the huge proposal to add C++ metaclasses, which I'm personally not really a fan of, since it looks like it would add an additional third language on top of C++.
Thus, the general opinion is that everybody would like that feature in one form or another, but it's quite impossible to find a consensus on what the feature would look like.
Re: Hypothetical C++: easy type creation
#9Friday Q&A 2013-08-02: Type-Safe Scalars with Single-Field Structs
https://mikeash.com/pyblog/friday-qa-2013-08-02-type-safe-sc...
Re: Hypothetical C++: easy type creation
#10This is already possible even in vanilla C: Friday Q&A 2013-08-02: Type-Safe Scalars with Single-Field Structs https://mikeash.com/pyblog/friday-qa-2013-08-02-type-safe-sc...