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

Type hinting #74

Open
LPeter1997 opened this issue Aug 2, 2022 · 0 comments
Open

Type hinting #74

LPeter1997 opened this issue Aug 2, 2022 · 0 comments
Labels
Design document This one came out from an idea but considers many cases and tries to prove the usabity Type system This issue is about type system

Comments

@LPeter1997
Copy link
Member

LPeter1997 commented Aug 2, 2022

There are a couple of ugly edges that our language already needs to solve/fix. Particularly, I believe there are three very related things we could solve with this proposed feature. Namely:

Many languages solve these by introducing some syntactical differentiator:

  • For literals, these are literal suffixes: 123U, 1.0f, ...
  • For character literals, single quotes are used instead: 'a' vs "a", or it's inferred (like in Swift)
  • For encoding, some languages (like C++) introduce prefixes, like u8"Hello"

Personally, I find some of these solutions ugly. For example, why can't 1 be a floating point number?

Type hints

Since Fresh uses : meaning "has type", I think there is no reason not to allow its use in arbitrary expressions. For example, (1 + 2): int + 3 would be equivalent to 1 + 2 + 3, but in the former case, the compiler would explicitly type-check the type of the subexpressions. This is already allowed by some ML languages to do coercions or aid type inference.

We could make these type hints denote the exact type for our literals, and default to something, when none is provided:

var x = 4; // x: int32
var y = 4: uint32; // y: uint32
// var z = x: uint32; // ERROR: only literals can be casted/coerced implicitly like this

var s1 = "a"; // s1: string
var ch = "a": char; // ch: char
// var ch2 = "ab": char; // ERROR: non-single-character string literals can't be coerced to char
var s2 = "hello UTF8 encoding!": u8string; // s2: u8string

Note, that this colon syntax could be used on literals in more complex expressions too. For example:

var x = 3; // x: int32
var a = x * 2: float32; // Equivalent to x * 2.0f in C#, a: float32

Important: These are not runtime casts, purely compile-time hints for the compiler.

Open questions

  • What should the precedence of : be? Does 1 + 2: float32 equal to (1 + 2): float32 or 1 + (2: float32)?
  • What should the following code produce?
var s = "Hello, World!";
if (s[3] == "l") {
    // ...
}

Should the literal l also be a character literal, given that it's the only valid overload for ==? This will likely belong to the type inference proposal for discussion. My initial reaction to this is that this is already trying to be "smart" about it, so I'd say this would be a type error for trying to compare char to string.

@LPeter1997 LPeter1997 added Design document This one came out from an idea but considers many cases and tries to prove the usabity Type system This issue is about type system labels Aug 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Design document This one came out from an idea but considers many cases and tries to prove the usabity Type system This issue is about type system
Projects
None yet
Development

No branches or pull requests

1 participant