Feature #14164


[Suggestion] Type system for ruby 3x to be usable for e. g. rubocop or autogenerating crystal code and so forth

Added by shevegen (Robert A. Heiler) over 4 years ago. Updated almost 3 years ago.

Target version:


Hello everyone, especially the ruby core team, nobu and of course matz,

This is an idea for ruby 3.x, but the idea is not ... well, really

It is more of an idea.

As I am aware that working with "unfinished" ideas is not easy, please
feel free to close this suggestion at any moment in time. It is more
meant a bit to point towards what nobody else may have pointed out
before ... :)

So the situation:

  • Matz spoke a few times about a "type system" for ruby (3.x). Now, this
    can be anything really, but matz also specified his idea a few times.

One idea that I think matz mentioned here or there was that it will be
optional. That is, people can use ruby light-weight as always, without
being forced into mandatory types and having to change the way they
may have written ruby for the past x years.

There is one blog/interview entry here mentioning types as well, at:

I think matz mentioned a type system a few times lateron when he gave
a presentation, and he also commented on a few issues about the
(possible) type system in different suggestions made at the ruby
tracker here.

In particular, I remember that matz wrote something to a suggestion by
another japanese ruby hacker some time ago about ... I think it had to
do with specifying certain behaviour of objects/methods.

I am sorry that I can not describe this in more detail since I forgot
the name of the one who suggested it; I myself do not fully understand
or know what will come with ruby 3.x. The above URL to the blog/interview
may give an indication as to what MIGHT come eventually.

Back then matz said:

"[...] the third major goal of the Ruby 3 is adding some kind of
static typing while keeping the duck typing, so some kind of
structure for soft-typing or something like that. The main goal
of the type system is to detect errors early. So adding this
kind of static type check or type interfaces does not affect

"It's just a compile time check. Maybe you can use that kind of
information in IDEs so that the editors can use that data for their
code completion or something like that, but not for performance

Ok, so far so good. Matz actually did not necessarily mean it
for performance improvement per se.

But ... here I was thinking ...

I guess many ruby people know the programming language crystal or
have heard about it. Matz even made a considerable donation a
few years ago. :)

Crystal is in many ways similar to ruby; the syntax is somewhat
similar (if we ignore the type system and macros perhaps).

Several ruby people use crystal, too, like jhass and others (I
don't as of yet, largely due to lack of time, inertia and my
own laziness; I even have learning elixir on my todo list and
just did not get around it really).

However had, I somewhat jokingly said in the past that perhaps
we could tune rubocop to generate crystal code. Since the difference
should not be that far apart from ruby code.

It was more meant as a joke though, but ... I think in theory that
could be possible, right? Perhaps not 100% valid crystal, but if
you specify some layout guide for rubocop to conform too, then the
autocorrect option from rubocop could perhaps generate a version
that is very close to the final crystal variant, excluding types

Not long ago, really just a few days ago, I actually remembered that
ruby may have some form of (optional) typing system in the future.

And here I wondered:

  • What if this type system could be used, in an optional way,
    for ruby people to also be able to auto-generate valid crystal
    code, via an indirection? This indirection could be rubocop,
    but it could also be part of MRI itself (once the type system
    is all in place; a bit like the RubyVM).

I do not know how difficult this is but I thought that this
would be pretty cool to have. Write Ruby - have Crystal. :D

(This is meant in addition; I myself obviously prefer to write
ruby, and have the computer autogenerate crystal for me, so
that I can be lazy. :P)

We could write ... ruby ... just as-is, for everyday scripts.

And then at a later time, we may add additional information, e. g.
with any form of type system, IF we need it. And then, ruby
could make use of that additional information to not only
consider speeding up ruby code (perhaps; or perhaps not), but
to also ... have it generate valid crystal code! :D

This is perhaps more wishful thinking on my part or for a
future xmas list; and it may also require the crystal team
to keep this in mind as well AND the ruby team.

But anyway, I only wanted to mention this for the time being;
matz once mentioned that ruby 3.x is more aimed towards the
year 2020, so ... +2 years I suppose, so there is still some
time left before 3.x will be released and "finalized".

I do not know how the crystal team thinks, neither how the
ruby team thinks about it - but I like my own idea. :D

Of course this also depends on what kind of type system there
will be, and how useful it may be. In the design stage, naturally
ruby comes first here (this is a bug tracker for ruby after all,
not for other languages), but I wonder about the second step,
where we could translate ruby-code into other languages code.

I should also note that I do not necessarily mean a "100%
correct output where nobody has to make any changes"; I think
any autogenerated output, that can be customized, would be
a "time win" for people in general - both when it comes to
having faster execution time for ruby code, but also lateron
to anyone who may want to use crystal rather than ruby primarily
because of the speed reason. (In my case, speed was never
a reason to use ruby so I have no reason to switch away
from ruby due to speed, but matz also said that nobody minds
if things are fast, so any speed improvement is a good
thing. And if my ruby code were to be almost valid crystal
code, then I'd only have to invest a tiny bit extra time
to get even more speed almost for free.)

Matz mentioned the additional information towards IDEs but
we could perhaps combine this with other tools. For example,
rubocop makes suggestions what to change; an optional type
system could also perhaps make some kind of analysis as to
what could be omitted or improved etc... a bit like the
did-you-mean-gem but working on a more "sophisticated"
level. (With a type system, I assume that a compiler has
an easier time knowing which constraints can be used etc...)


Also available in: Atom PDF