The Crystal Programming Language Forum

Use a variable as Type in assignment

hello, everyone, I got this error:


$crystal --version
Crystal 0.30.1 [5e6a1b672] (2019-08-12)
$crystal eval 'u = Int32|String; y : Int32|String = 4 ;puts y '
4

$crystal eval 'u = Int32|String; y : u = 4 ;puts y '
Error: expecting token 'CONST', not 'u'

I want to use variable instead of union types directly, is it possible?
thanks~

Hi @orangeSi!

https://crystal-lang.org/reference/syntax_and_semantics/alias.html might be what you need?

2 Likes

yes, thanks a lot! I thought I had read the documnet almost all, but still ignored alias, I will read the Crystal Doc with more attention!

1 Like