tupl-tufts / rdl

Types, type checking, and contracts for Ruby
BSD 3-Clause "New" or "Revised" License
602 stars 38 forks source link

var_type method does not appear to work #34

Closed Angeldude closed 7 years ago

Angeldude commented 7 years ago

Hi everyone, maybe I'm misunderstanding or misinterpreting the README but all the type variable examples don't produce any type errors for me. For example

RDL.var_type :x, 'Integer'
x = 3       # okay
x = "three" # type error

This does not give me any type errors, it behaves like regular ruby.

Same for this one:

class A
  extend RDL::Annotate
  var_type :@f, 'Integer'
  def m
    @f = 3       # type safe
    @f = "three" # type error, incompatible type in assignment
    @g = 42      # type error, no var_type for @g
  end
end

Although the attr_accessor_type and other attribute methods do report type errors.

Angeldude commented 7 years ago

Further investigations have allowed me to figure out ways of making the above examples work as expected. The class A example will work if I define the type signature of the method m, like this:

class A
  extend RDL::Annotate

  type '() -> Integer', typecheck: :now
  var_type :@f, 'Integer'
  def m
    @f = 3       # type safe
    @f = "three" # type error, incompatible type in assignment
    @g = 42      # type error, no var_type for @g
  end
end

This will then trigger the type errors in the instance variable. For the first example, the variable type will only work from within a method and, again, the method's type signature has to be present.

class ...

  type '() -> Integer', typecheck: :now
  def m
    RDL.var_type :x, 'Integer'
    x = 3
    x = "three"
    5
  end
...

My question is, should this be reflected in the README or is what the README depicts the desired behavior? Again, I have not found a way to make the examples as originally depicted produce the type errors.

jeffrey-s-foster commented 7 years ago

Right, RDL only typechecks the bodies of methods that have type signatures. It ignores any other code. I'll edit the documentation to make this more clear.