mame / typeprof-playground

5 stars 0 forks source link

do not recognized type if use attr_accessor to instantiate type in initializer #5

Closed LeandroRezendeCoutinho closed 3 years ago

LeandroRezendeCoutinho commented 3 years ago

Issue

If i use

def delegate_sum(a,b)
    Service.new.sum(a,b)
  end

result is

def delegate_sum : (Float, Integer) -> (Float | Integer)

On a diferente approach do not recognize output. "if it is not a bug, sorry"

ruby

class Service
  def sum(a,b)
    a + b
  end

  def mult(a,b)
    a * b
  end

  def div(a,b)
    a / b
  end
end

class Calculator
  attr_reader :service

  def initialize
    service = Service.new
  end

  def delegate_sum(a,b)
    service.sum(a,b)
  end
end

service = Service.new
a = 1
b = 2
service.sum(1,2)
a = 1.0
b = 2.5
service.mult(a, b)
service.div(1, b)
Calculator.new.delegate_sum(a,1)

output

# Classes
class Service
  def sum : (Integer, Integer) -> Integer
  def mult : (Float, Float) -> Float
  def div : (Integer, Float) -> Float
end

class Calculator
  attr_reader service : bot
  def initialize : -> Service
  def delegate_sum : (Float, Integer) -> untyped
end

## Version info:
##   * Ruby: 2.7.2
##   * RBS: 0.17.0
##   * TypeProf: 0.7.0
mame commented 3 years ago

Thank you for trying TypeProf!

Your code assigns a Service instance to a local variable.

  def initialize
    service = Service.new
  end

I believe you wanted to assign it to an instance variable like this.

  def initialize
    @service = Service.new
  end

By tweaking the code, TypeProf shows def delegate_sum : (Float, Integer) -> (Float | Integer) as you expect.

https://mame.github.io/typeprof-playground/#rb=class+Service%0A++def+sum%28a%2Cb%29%0A++++a+%2B+b%0A++end%0A++%0A++def+mult%28a%2Cb%29%0A++++a+*+b%0A++end%0A++%0A++def+div%28a%2Cb%29%0A++++a+%2F+b%0A++end%0Aend%0A%0Aclass+Calculator%0A++attr_reader+%3Aservice%0A++%0A++def+initialize%0A++++%40service+%3D+Service.new%0A++end%0A++%0A++def+delegate_sum%28a%2Cb%29%0A++++service.sum%28a%2Cb%29%0A++end%0Aend%0A%0Aservice+%3D+Service.new%0Aa+%3D+1%0Ab+%3D+2%0Aservice.sum%281%2C2%29%0Aa+%3D+1.0%0Ab+%3D+2.5%0Aservice.mult%28a%2C+b%29%0Aservice.div%281%2C+b%29%0ACalculator.new.delegate_sum%28a%2C1%29&rbs=class+User%0A++def+initialize%3A+%28name%3A+String%2C+age%3A+Integer%29+-%3E+void%0A%0A++attr_reader+name%3A+String%0A++attr_reader+age%3A+Integer%0Aend

Thanks!

LeandroRezendeCoutinho commented 3 years ago

I always get confused with attrs... kkkk Nice work!