Issue #9999 has been updated by jonathan rochkind.


Existing ruby has interesting bits of a kind of formal duck-typing for some core library classes, like String#to_s and #to_str.  There are a lot of methods that want 'a String', and will silently accept anything that can be made one with `to_str` (or in some cases `to_s`? not sure), otherwise through a TypeError. 

It would be interesting to incorporate this into a more formal type annotation system. If an argument is annotated as being `String`, maybe it will happily accept anything with a `to_str`, silently calling it? 

Maybe extensible, so any class can define it's own "as if" method, `String.type_conversion_method = :to_str`, and if an argument type is annotated as being SomeClass, then any argument will have `SomeClass.type_conversion_method` called on it if possible. 

Not sure of all the details, there is some messiness in the existing patterns (when/whether to_s vs to_str is already sometimes confused; having to add a conversion method to every class that can be converted isn't quite right).  But something along these lines might be a good idea to actually be consistent with existing Ruby conventions instead of just bolting on a completely new system that has nothing to do with the existing conventions, as well as having an optional typing system still have some notion of duck typing/typing to interface, which are just good OO design principles in addition to being part of ruby community norms. 

----------------------------------------
Feature #9999: Type Annotations (Static Type Checking)
https://bugs.ruby-lang.org/issues/9999#change-59323

* Author: Davide D'Agostino
* Status: Feedback
* Priority: Normal
* Assignee: 
----------------------------------------
Hi all,

I know @matz is interested in introducing **type annotations** in ruby. More here: https://bugs.ruby-lang.org/issues/5583

I think it's time for ruby to get this.

Before working on a patch I would like to know:

1. Syntax of methods signatures
2. Syntax of variables guards (?)
3. Implementation

For point **1** I was thinking in some like:

~~~ruby
def connect(r -> Stream, c -> Client) ->  Fiber
def connect(Stream r, Client c) -> Fiber # quite sure this will make some reduce problems in the grammar
~~~

Before making a proposal consider: keyword arguments and default value collisions.

Then for point **2** I'm not sure if we want also check assignments but as before a syntax could be:

~~~ruby
r: Client = something # will throw an exception if something is not kind of Client
~~~

Finally, **implementation**. Do we want some in python style and then leave the programmer/library for the implementation **or** (and I'm for this) we want MRI do that, if so how?

Cheers!
DD

p.s. Sorry if this issue was already discussed but I didn't find anything except the link posted.



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>