(IDEA) Ranged Type System #7571
TheNachoBIT
started this conversation in
Language design
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
This is an idea that i had for a while, heavily inspired by Ada.
I'm not putting this as a proposal, because (imo) it doesn't solve current problems, and its not the end of the world if Carbon doesn't add this, but it could've be cool for both safety, organization and future-proofing for other architechtures.
There's still a ton of edge-cases that could've be added, so feel free to give a comment/opinion on this :D
Intro
Range Types are types that represent a set of all numbers within a specific range.
Integer Range (
irange).Floating-Point Range (
frange).Enums.
Thanks to this system, enums can also have an internal lower and upper bound.
Aliases
A range type can also be derived from an existing range type:
However, by default they can't be implicitly casted.
Architechture Ranges
The only exception of integer ranges that can be implicitly casted, are "Architechture Ranges". Ranges that hit the highest possible range the target you're compiling to can contain.
There's two types of arch ranges:
ARCH_UINT: Highest Unsigned Architechture Range.ARCH_INT: Highest Signed Architechture Range.However, you can't implicitly cast an
ARCH_UINTto anARCH_INT, and viceversa:Implicit Ranges
These are ranges that can be implicitly casted, as long as they fit into the range:
Compile-Time Checking.
Because we're already setting lower and upper bounds, it can be used to check at compile-time.
Same happens with enums.
Run-Time Checking
One of the cons of this system is that, in the worst case scenario, the compiler needs to add checks at runtime:
Standard Library Use.
This system can be used in the standard library to set the basic types:
i32,u32, etc.It brings the possibility for future-proofing for new architechture types, and it allows the language to have no hardcoded primitives, giving more flexibility.
Arrays.
Because ranged types are numbers internally, they can be used in arrays. Arrays will store a range, instead of an integer as a limit:
Newtype Indices:
For limits:
And even use it in bufs (dynamic arrays):
For compatibility and comfort, arrays can have a number, and it'll be converted into an implicit range.
Using enums is also allowed:
However, you can't use floating-range types in arrays:
All reactions