# Feature suggest: implicit AND explicit variable definitions

**URL:** <https://www.ruby-forum.com/t/feature-suggest-implicit-and-explicit-variable-definitions/105318>\
**Category:** Ruby\
**Created:** [May 28, 2007, 7:40pm UTC](https://www.ruby-forum.com/t/feature-suggest-implicit-and-explicit-variable-definitions/105318 "2007-05-28T19:40:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 28, 2007, 7:40pm UTC](https://www.ruby-forum.com/t/feature-suggest-implicit-and-explicit-variable-definitions/105318/1 "2007-05-28T19:40:53Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 28, 2007, 9:16pm UTC](https://www.ruby-forum.com/t/feature-suggest-implicit-and-explicit-variable-definitions/105318/2 "2007-05-28T21:16:06Z")

</div>

On May 28, 7:35 pm, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 28, 2007, 9:46pm UTC](https://www.ruby-forum.com/t/feature-suggest-implicit-and-explicit-variable-definitions/105318/3 "2007-05-28T21:46:09Z")

</div>

Hi –

On Tue, 29 May 2007, removed\_email\_address@domain.invalid 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 🙂 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
