rtc icon indicating copy to clipboard operation
rtc copied to clipboard

Allow type assignments

Open jtoman opened this issue 13 years ago • 12 comments

Related to #22, we should allow assigning the type of one method to another method. For example, the slice method of the Array object has the exact same type as the [] method. How we should add this to the grammar and parser is up in the air, we could use a syntax like alias slice '[]' or we could use something that looks like OCaml's destructive type assignment: slice := '[]'. Alias is already a keyword in Ruby, so that may be more intuitive to users and it gets my vote.

jtoman avatar Mar 18 '12 15:03 jtoman

I think there are two cases here:

  1. For base_types.rb, I agree we want something like "type_alias :slice '[]'"
  2. For other classes, we should intercept the alias method and automatically copy over type signatures.

jeffrey-s-foster avatar Mar 18 '12 15:03 jeffrey-s-foster

Looking at Ruby's parse.y, it appears that alias is actually built into the parser and is not a function call (I'm surprised too...). I don't know how practical it is for us to intercept that, although there may be a hook.

jtoman avatar Mar 18 '12 15:03 jtoman

I have confirmed that alias is not a method call:

class Foo
  def self.alias(foo,bar)
    p foo,bar
  end
  def foo
  end
  alias :bar :foo
end

The self.alias method is never called.

jtoman avatar Mar 18 '12 15:03 jtoman

Huh, odd. In any case, we can go with the slightly more awkward "type_alias" for everything, and at some point perhaps get a change made to the language so we can intercept the call.

jeffrey-s-foster avatar Mar 18 '12 15:03 jeffrey-s-foster

Looking at the old parser, it appears that alias was a keyword. Is there a reason to use type_alias over plain alias?

jtoman avatar Mar 18 '12 15:03 jtoman

I thought we would do this outside of the parser. So the typesig method would assign type signatures, and then the type_alias method would make aliases. That makes more sense to me than putting it inside the string passed to typesig. What do you think?

jeffrey-s-foster avatar Mar 18 '12 15:03 jeffrey-s-foster

I think that makes more sense. I would add the rtc_ prefix to it so we completely avoid confusion with the alias method. Do you think the type abbreviations mentioned in #22 should also be a method call?

jtoman avatar Mar 18 '12 15:03 jtoman

Sure, rtc_alias sounds good. Yes, I think the type abbreviations in #22 should probably also be a method call---it removes an extra layer and simplifies the parser, so it seems like a win.

jeffrey-s-foster avatar Mar 18 '12 16:03 jeffrey-s-foster

Well, we'll still need to invoke the parser, something will have to parse the type that someone writes, it's just a difference between

rtc_type :my_type,":foo or :bar"
# vs.
typesing("type my_type = :foo or :bar")

jtoman avatar Mar 18 '12 17:03 jtoman

Right, but with rtc_alias, you don't have to (a) extend the grammar with new "type my_type = ..." productions, and (b) explain to programmers what the new syntax means.

jeffrey-s-foster avatar Mar 18 '12 17:03 jeffrey-s-foster

I think we're confusing the two issues. Type abbreviations, as mentioned in the ticket, saves people from having to write long complicated types over and over again. Type aliasing allows for assigning the type of one function to another. The discussion of rtc_type above refers to type abbreviations. rtc_alias would require no help from the parser.

jtoman avatar Mar 18 '12 17:03 jtoman

Sorry, you're right. For type aliases, I think they need to be in the parser, since we might at some point need a bit more complexity with them.

jeffrey-s-foster avatar Mar 18 '12 18:03 jeffrey-s-foster