Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing block to class_property tries to return first assignment in block #5295

Closed
vonKingsley opened this issue Nov 15, 2017 · 7 comments
Closed

Comments

@vonKingsley
Copy link

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.

@Sija
Copy link
Contributor

Sija commented Nov 15, 2017

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"

@Sija
Copy link
Contributor

Sija commented Nov 15, 2017

@jhass I'd suggest removing those tags since it's neither bug nor it's related to the compiler.

@jhass
Copy link
Member

jhass commented Nov 15, 2017

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?

@Sija
Copy link
Contributor

Sija commented Nov 15, 2017

@jhass indeed, you're right! even though included usage of class_property was improper, below code should compile alright...

@vonKingsley
Copy link
Author

@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.

@vlazar
Copy link
Contributor

vlazar commented Oct 13, 2019

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

@asterite
Copy link
Member

@vlazar Yes, thanks! It seems to be working fine now. I don't know what fixed it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants