Tuesday 12 April 2016

Julia: Union type


By using Union, you can create a custom type, which can support instances of any of its argument types, constructed using the special Union function.


julia> IntOrStringOrbool = Union{Int,AbstractString, Bool}
Union{AbstractString,Bool,Int64}

julia> 12::IntOrStringOrbool
12

julia> "hari"::IntOrStringOrbool
"hari"

julia> true::IntOrStringOrbool
true

julia> 10.23::IntOrStringOrbool
ERROR: TypeError: typeassert: expected Union{AbstractString,Bool,Int64}, got Float64

IntOrStringOrbool = Union{Int,AbstractString, Bool}
You can assign a value of type Int (or) AbstractString (or) Bool to the type IntOrStringOrbool. You will get TypeError, when you try to assign any other kind of data.



Previous                                                 Next                                                 Home

No comments:

Post a Comment