I am working on a compiler and I have this issue. Here are the current rules.
let x := 100; // x is i64
let x := 100 as i32; // x is i32
let x : i32 = 100; // type error i64 and i32 don't match
Right now all literals that are not determined to be floats are typed as signed 64 bit numbers. In the last example, because the type checker is trying to match i32 to i64 we get an error.
I want to be clear that I am not looking for soething like this. This is what I see people meaning when talking about type inference but I am not looking for this. In this example I want this to give an error as the literal with out a given type shuold be i64.
fn foo(x: i32) void {}
let x := 100; // x's type is yet to be determined or constrained by the context
foo(x); // x is infered as i32
So I am asking for adivce to solve the first issue.
One more thing to note is that I am not just trying to do this for assignments but in other locations aswell like function calls and struct instantiation.
Thank you in advance.
Here is a link I found discussing bi-directional typing in Haskell but compared to what I am doing this seems complex and difficult to actually find out how to implement.