One of the main drawbacks of Ruby/Python is performance. I understand that they are interpreted and C is compiled. (And there are things like JRuby which do JIT compilation with Ruby). But they never manage to get the same speed/memory usage as a compiled program. Would it be possible to create a new language that had the same syntax as Ruby for example (with one change: static typing) that compiled down to something as fast as a C program?I'm trying to think of what other differences there are other than static typing than prevent you from getting the same speed. (Maybe allocating buffers of a fixed size as opposed to an abstraction which handles this for you). But it seems like you could get pretty close?If so, why has no one created a compiled version of Ruby/Python with static typing?
Would it be possible to create a language similar to Ruby/Python with static typing that had the speed/memory usage of a compiled C program?
ruby;compiler;go;static typing
Just adding explicit type annotations to Ruby wouldn't do the trick. One key characteristic of Ruby is that any method can be redefined at runtime. So even if you know that some value will always be an instance of class C, that doesn't allow you to do things like inlining.Note that in Ruby, even loops are actually implemented as method calls. So your compiler couldn't even assume that a loop is... actually a loop, since #each could be redefined at runtime.You could design a language with Ruby syntax, but take away all the runtime metaprogramming. That could be compiled to efficient machine code, but it wouldn't be Ruby. The key characteristic which makes Ruby what it is, is the extreme flexibility to modify almost everything dynamically, at runtime.You could conceivably write an incredibly sophisticated compiler which would do global analysis of an entire program, and determine whether any of those dynamic features are actually used or not. In many cases, this would practically require something close to strong AI. Not practical.Another option, which has been done with other languages and could be done in Ruby, would be to write a JIT compiler which does optimizations like inlining based on the assumption that the current class and method definitions won't change, and backs out of those optimizations if they do. That is the way to make Ruby code very, very fast. Unfortunately, nobody has done it yet.