Crystal: Type inference in assign as condition fails if it's not the first condition

Created on 23 Dec 2016  路  2Comments  路  Source: crystal-lang/crystal

In this example, bar inside the method is inferred to not be nil:

class Foo
  property bar : String?
end

def test
  foo = Foo.new
  foo.bar = "bar"

  unless (bar = foo.bar) && true # <--- !
    return
  end

  p bar.reverse
end

test # => "rab"

In this one it's not:

class Foo
  property bar : String?
end

def test
  foo = Foo.new
  foo.bar = "bar"

  unless true && (bar = foo.bar)  # <--- !
    return
  end

  p bar.reverse
end

test # => undefined method 'reverse' for nil (compile-time type is (String | Nil))

Perhaps it's because the assignment of bar itself depends on the former condition being truthy, but I think the Nil type should be filtered out if the conditions hold.

feature compiler

Most helpful comment

This was fixed at some point so I'm closing it.

All 2 comments

codetriage

Any progress on this? It's still reproducible on 0.25.1. If this is a long-term goal/plan, it should be labeled as such, or some project should be made to contain things like this.

This was fixed at some point so I'm closing it.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

xtagon picture xtagon  路  132Comments

sergey-kucher picture sergey-kucher  路  66Comments

fridgerator picture fridgerator  路  79Comments

asterite picture asterite  路  139Comments

asterite picture asterite  路  60Comments