Type inferrence limitation?

Why does this work:

mail = user.mail # Type is String?

if mail
  env.session.string("user_mail", mail) # Only takes String

And this not?

if user.mail
  env.session.string("user_mail", user.mail)

I would assume they’re the same.

https://crystal-lang.org/reference/1.8/syntax_and_semantics/if_var.html#method-calls

tl;dr Procs and methods aren’t guaranteed to return the same more-specific type on two successive calls.

Aha… Well, in theory the compiler could notice that user.mail is a simple getter that returns the same insta… Oooh, and the point on why it doesn’t work on instance vars applies…

Makes sense.

Yea, otherwise would technically be possible with concurrency to switch to another fiber that mutates the ivar after the initial check that then could cause issues.