Type detection: Difference between revisions
Content added Content deleted
No edit summary |
|||
Line 127: | Line 127: | ||
3 is printable |
3 is printable |
||
3 is a hexadecimal digit |
3 is a hexadecimal digit |
||
</pre> |
|||
=={{header|Crystal}}== |
|||
<lang Ruby>def print_type(x) |
|||
puts "Compile-time type of #{x} is #{typeof(x)}" |
|||
puts " Actual runtime type is #{x.class}" if x.class != typeof(x) |
|||
end |
|||
print_type 123 |
|||
print_type 123.45 |
|||
print_type rand < 0.5 ? "1" : 0 |
|||
print_type rand < 1.5 |
|||
print_type nil |
|||
print_type 'c' |
|||
print_type "str" |
|||
print_type [1,2] |
|||
print_type({ 2, "two" }) |
|||
print_type({a: 1, b: 2}) |
|||
print_type ->(x : Int32){ x+2 > 0 } |
|||
</lang> |
|||
{{out}} |
|||
<pre>Compile-time type of 123 is Int32 |
|||
Compile-time type of 123.45 is Float64 |
|||
Compile-time type of 0 is (Int32 | String) |
|||
Actual runtime type is Int32 |
|||
Compile-time type of true is Bool |
|||
Compile-time type of is Nil |
|||
Compile-time type of c is Char |
|||
Compile-time type of str is String |
|||
Compile-time type of [1, 2] is Array(Int32) |
|||
Compile-time type of {2, "two"} is Tuple(Int32, String) |
|||
Compile-time type of {a: 1, b: 2} is NamedTuple(a: Int32, b: Int32) |
|||
Compile-time type of #<Proc(Int32, Bool):0x5645695ab9f0> is Proc(Int32, Bool) |
|||
</pre> |
</pre> |
||