Feature suggest: implicit AND explicit variable definitions

I remember reading that, in Python, working with variables can be slow
because it needs to check if the variable is being changed to another
type every time it’s used. I don’t know if Ruby is like that too, but
this is for if it is.

If they make it possible for explicit variable definitions. Let’s say
I’ve got an integer and there’s no reason it would ever turn into a
float or anything like that unless specifically noted. It might run
faster because Ruby wouldn’t have to mess with dynamic typing. And
I’ve got a class that has a variable that is a string. I could
immediately define it as a string the same way I would in C or Java.
Ruby wouldn’t have to check the type AND THEN initialize, it would
already know the type!

But I’m guessing that if this would be useful, it would have already
been done. So am I right or can you tell me why I’m wrong?

On May 28, 7:35 pm, “[email protected][email protected] wrote:

immediately define it as a string the same way I would in C or Java.
Ruby wouldn’t have to check the type AND THEN initialize, it would
already know the type!

But I’m guessing that if this would be useful, it would have already
been done. So am I right or can you tell me why I’m wrong?

A language can be plenty fast with dynamic typing, but it does need
fancy optimizations like runtime type feedback.

It works like this:

def double(a)
a * 2
end

Ruby doens’t know the type of a. It could be a float, integer, string,
etc.

double(3)
double(2)
double(3.2)

When running these programs Ruby could collect type information. For
example: a was an int 200 times, and a float 4 times. If Ruby detects
that this function is called with ints a lot it could compile (at
runtime) a specialized version for ints:

def double_for_ints(int a)
a * 2
end

This one could be much more efficient. If you have this optimization
you only pay for dynamic typing where it’s actually used.

Ruby doesn’t perform this optimization, unfortunately. The programming
language Self does, and it’s very effective: papers about Self suggest
that it’s about twice as slow as C (the current Ruby implementation is
much slower). Keep in mind that Self is even more dynamic than Ruby
(other aspects of Ruby like eval which has access to the current
environment do make optimization harder). There are many other
optimizations for Self, and most of them could be used for Ruby.

Jules

Hi –

On Tue, 29 May 2007, [email protected] wrote:

immediately define it as a string the same way I would in C or Java.
Ruby wouldn’t have to check the type AND THEN initialize, it would
already know the type!

s/type/class :slight_smile: Type and class are not the same in Ruby.

But I’m guessing that if this would be useful, it would have already
been done. So am I right or can you tell me why I’m wrong?

If you tie variables themselves down to certain classes, you lose the
power of being able to address objects dynamically and without concern
for their class. It would militate pretty directly against the kind
of “duck typed” programming Ruby is so suited for.

David