nature icon indicating copy to clipboard operation
nature copied to clipboard

proposal: type casts and type inference syntax

Open weiwenhao opened this issue 1 year ago • 1 comments

Due to the existence of the any type, type inference is necessary. However, the method similar to golang any.(Type) requires one small symbol, one large symbol, and two parentheses, which can be cumbersome. Therefore, I would like to refer to the as method in Rust and TypeScript.

Type casting

var foo = 1
var bar =  foo as bool  // bar type is bool 

Type assertion

any foo = 1
var bar = any is int // bar type is int 

One point that is quite tangled is whether the "is" syntax is necessary, or whether to use the "as" syntax uniformly, both for type assertions and type conversions. 🤔

weiwenhao avatar May 14 '23 06:05 weiwenhao

'is' syntax will be used for type checking, bool b = foo is int, checking if foo is of type int, and returning a bool type.

'as' will be used for both type assertion and type casting.

weiwenhao avatar May 17 '23 10:05 weiwenhao