Add shorthand for casting integer types
jfecher opened this issue · 0 comments
jfecher commented
Currently to cast a value to an integer type one needs to use the cast
function and often a type hint as well. This can be somewhat awkward both to read and write:
show_addresses (first: Ptr a) (second: Ptr a) =
difference: Usz = cast second - cast first
print "${second as Usz} - ${first as Usz} = ${difference}"
Ante should support a shorthand for casting to primitive integer types at least - and potentially all primitive types. This shorthand can be similar to constructor syntax for user-defined types:
show_addresses (first: Ptr a) (second: Ptr a) =
difference = Usz second - Usz first
print "${Usz second} - ${Usz first} = ${difference}"
Since the various primitive types tend to be short, this is almost always shorter than the cast
approach while making the intent of casting to a Usz
(in this case) more clear.
Proposed implementation strategy
- The parser will need to accept primitive types in a function position
- When the parser finds a function call to a primitive integer type
t x
it will desugar tocast x : t
instead.
Open questions:
- Perhaps the parallel with type constructors should be pushed forward further. Instead of desugaring
U8 x
immediately, perhapsU8
when used in an expression position should be a valid identifier. This way, users couldd passU8
as a function to higher order functions such asmap
to cast each element of a container. The prelude could also then define the cast functions for each integer type trivially:U8 x = cast x : U8
. A downside of this approach is that new users may be confused on the two different meanings of these types when used in a type position or an expression position. If Ante ever gets fully dependent types, this would conflict with that as well since types and values would need to live in the same namespace.