Ruby 中的静态类型推断,我能做什么?

Static type inferring in Ruby, what can I do?

我有一个旨在静态键入一些 Ruby 代码的学校项目。所以,我的输入只是一个 .rb 文件,我应该能够键入程序中分配的每个变量。

现在,我打算做的是:

我只接受一些非常基本的 Ruby 作为输入(= 不调用外部库,只是 ruby 的核心 + 文件中定义的方法)

我的问题是:您如何看待我的方法?是否有任何 gem/existing 程序可以帮助我?

你的方法技术上是正确的,但你的表达方式听起来很奇怪。这个:

Right now, what I'm planning to do is :

  • get the file's AST with the Parser library
  • put each kind of nodes in container objects
  • implement the visitor pattern to recursively go through the program
  • try to infer something from there (I was thinking of somehow creating a table of possible input and output types from the core's methods)

听起来有点像你想像这样去火星:

Right now, what I'm planning to do is :

  • get a pencil
  • get a piece of paper
  • get a desk
  • sit down at the desk and use my pen and paper to design a space launch system and Mars lander

换句话说,你列出了三个完全微不足道的点,对于一个有经验的程序员来说可能需要一个小时的工作,然后是第四点,那就是多年的工作,值得获得博士学位。

我所知道的关于 Ruby 静态类型推断的最先进的工作是 Diamondback Ruby (DRuby)(不要与分布式 Ruby 标准库 aka dRb / d[=50= 混淆) ]).但是,Diamondback Ruby 现在已被放弃,因为作者放弃了 Ruby.

的静态类型推断

Diamondback Ruby 背后的主要研究人员之一现在正在开展一个名为 RDL 的新项目。 Diamondback Ruby 和 RDL 之间的主要区别是:

  • RDL 执行动态检查,而不是静态检查
  • RDL 依赖于显式注释,而不是隐式推理

Steep 是另一个类似的项目。它也依赖于动态检查和注释,而且实际上并不追求类型正确性。

Ruby Type Inference for IDEA 是对 JetBrains 计划如何在 IDEA / RubyMine IDE 中为 Ruby 进行类型推断的完全重新思考. 确实使用类型推断,但它使用动态类型推断,而不是静态类型推断。

因此,如您所见,Ruby 的静态类型推断非常困难,甚至没有人尝试过,而 尝试过 的人放弃了它现在正在使用显式类型注释进行动态类型检查。

Ruby Type Checking Roundup on Robert Mosolgo's blog 很好地概述了 Ruby 打字的当前最先进技术。