Allow type assignments
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.
I think there are two cases here:
- For base_types.rb, I agree we want something like "type_alias :slice '[]'"
- For other classes, we should intercept the alias method and automatically copy over type signatures.
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.
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.
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.
Looking at the old parser, it appears that alias was a keyword. Is there a reason to use type_alias over plain alias?
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?
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?
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.
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")
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.
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.
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.