You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#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
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?
The text was updated successfully, but these errors were encountered:
When you use it, you basically renounce most of the safety guarantees that Crystal provides, for the sake of performance or interoperability with C code.
Are you sure you really need to use declare an uninitialized var? What would be your use case?
You can always model what you would have done with uninitialized with nil, and it'll be safer and cleaner code. The only use for uninitialized is for performance where you don't want to create a type union, but unless you really know what you're doing removing safety for a tiny increase in speed is unwise.
Unless you're binding C code then uninitialized is useful.
For reference, there's no way to know if a variable is initialized or not because it will contain entirely random data. If it's a reference it will likely point to uninitialized memory (at least on 64bit), if it's a struct it will contain garbled data. There's no sane way to detect what is uninitialized.
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 var
testsActual 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?The text was updated successfully, but these errors were encountered: