nature
nature copied to clipboard
proposal: type casts and type inference syntax
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. 🤔
'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.