The following happened recently on master.
Running this code.
require "logger"
module Test
class_property(logger) {
@@logger ||= begin
a = 1
Logger.new(STDOUT)
end
}
Test.logger.info("logme")
end
returns class variable '@@logger' of Test must be (Logger | Nil), not Int32
crystal version
Crystal 0.24.0+50 [c9fac5645] (2017-11-14)
LLVM: 4.0.1
Default target: x86_64-apple-macosx
This works correctly in 0.23.1 and worked on 0.24.0 a few days ago so a recent change may have caused this.
Your example shows improper usage of class_property.
The whole point of lazy getter is not to define @@its_cvar by hand...
Proper version is as follows:
require "logger"
module Test
class_property(logger) { Logger.new(STDOUT) }
end
Test.logger.info "logme"
@jhass I'd suggest removing those tags since it's neither bug nor it's related to the compiler.
Well, how does it get typed as Int32 though? It should expand to
def self.logger
@@logger ||= begin
@@logger ||= begin
a = 1
Logger.new(STDOUT)
end
end
end
The last expression in the inner begin is Logger.new, so the type of the inner begin expression should be Logger, which makes the type of the inner @@logger ||= expression Logger, which makes the type of the outer begin expression Logger, which makes the type of the outer @@logger ||= expression, Logger. Where do I go wrong?
@jhass indeed, you're right! even though included usage of class_property was improper, below code should compile alright...
@Sija you are correct I was using class_property incorrectly. I checked the code and saw the block gets assigned to the class variable. I removed @@logger ||= and my code was working again.
@jhass Thanks for minimizing that code, i now know I was using it incorrectly ,but that particular change in behavior didn't seem right to me.
Both examples now show
$ crystal test.cr
I, [2019-10-13 20:45:26 +03:00 #26218] INFO -- : logme
$ crystal --version
Crystal 0.31.1 (2019-10-02)
LLVM: 8.0.1
Default target: x86_64-apple-macosx
@vlazar Yes, thanks! It seems to be working fine now. I don't know what fixed it.