I'm not sure if this is a bug but it's definitely unexpected behavior
Expected Behavior:
uninitialized variables should not pass ! var.nil? or if var tests
Actual Behavior
exactly the opposite
Environment
macOS 10.12.4
Crystal 0.22.0 (2017-04-20) LLVM 4.0.0
Sample code
#tester.cr
log_version = uninitialized String
if ! log_version.nil?
puts "it's not nill"
else
puts "totally nil"
end
if log_version
puts "log version is #{log_version}"
else
puts "no log version"
end
compile that then run it
$ ./tester
it's not nill
Invalid memory access (signal 11) at address 0x7fff529c7000
[0x10d25e9ab] *CallStack::print_backtrace:Int32 +107
[0x10d24a38c] __crystal_sigfault_handler +60
[0x7fff9a43ab3a] _sigtramp +26
So, I can test if it's nil and it tells me it's not. I can test if it exists and it tells me it does, but if i try and do anything with it, it blows up.
In my code i had to change it to log_version = nil.as(String?) to get around this because it isn't clear how to test if a variable is uninitialized. It's probably in the doc's somewhere but I haven't found it, and i really don't think that the tests above should BOTH pass... I can see the argument against ! log_version.nil? passing, because it's not really nil. It's uninitialized but the fact that it passes the second test seems crazy. it's the most non-existent form of something. How can that be truthy?
I'm not sure if this is a bug but it's definitely unexpected behavior
Expected Behavior:
uninitialized variables should not pass
! var.nil?orif vartestsActual Behavior
exactly the opposite
Environment
macOS 10.12.4
Crystal 0.22.0 (2017-04-20) LLVM 4.0.0
Sample code
compile that then run it
So, I can test if it's nil and it tells me it's not. I can test if it exists and it tells me it does, but if i try and do anything with it, it blows up.
In my code i had to change it to
log_version = nil.as(String?)to get around this because it isn't clear how to test if a variable is uninitialized. It's probably in the doc's somewhere but I haven't found it, and i really don't think that the tests above should BOTH pass... I can see the argument against! log_version.nil?passing, because it's not really nil. It's uninitialized but the fact that it passes the second test seems crazy. it's the most non-existent form of something. How can that be truthy?