Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add shorthand for casting integer types #173

Open
jfecher opened this issue Dec 18, 2023 · 0 comments
Open

Add shorthand for casting integer types #173

jfecher opened this issue Dec 18, 2023 · 0 comments
Labels
good first issue An issue that doesn't require deep knowledge of the compiler to fix or implement language feature A feature proposal for something added to the core language (not the standard library)

Comments

@jfecher
Copy link
Owner

jfecher commented Dec 18, 2023

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 to cast x : t instead.

Open questions:

  • Perhaps the parallel with type constructors should be pushed forward further. Instead of desugaring U8 x immediately, perhaps U8 when used in an expression position should be a valid identifier. This way, users couldd pass U8 as a function to higher order functions such as map 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.
@jfecher jfecher added language feature A feature proposal for something added to the core language (not the standard library) good first issue An issue that doesn't require deep knowledge of the compiler to fix or implement labels Dec 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue An issue that doesn't require deep knowledge of the compiler to fix or implement language feature A feature proposal for something added to the core language (not the standard library)
Projects
None yet
Development

No branches or pull requests

1 participant