# Non-declared variables

**URL:** <https://www.ruby-forum.com/t/non-declared-variables/72323>\
**Category:** Ruby\
**Created:** [October 16, 2006, 1:42pm UTC](https://www.ruby-forum.com/t/non-declared-variables/72323 "2006-10-16T13:42:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Avatar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8c91f0/32.png) [@Avatar](https://www.ruby-forum.com/u/Avatar)\
**Post date:** [October 16, 2006, 1:42pm UTC](https://www.ruby-forum.com/t/non-declared-variables/72323/1 "2006-10-16T13:42:24Z")

</div>

I would like to hear people’s opinions on the ability to use variables  
without declaring them in dynamic languages like Ruby.

It would seem to me that an argument for non-declared local variables  
that typically occupy a small scope could be made. But, class  
variables? What is the benefit in allowing for runtime definition of  
class variables?

Are there real tangible benefits that non-declared, dynamically typed  
(at binding time) variables provide? Or do dynamic variables simply  
create less compile time errors and more (harder to catch) runtime  
errors?

Thoughts?

---

<div class="post-metadata">

**Author:** ![Avatar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8c91f0/32.png) [@Avatar](https://www.ruby-forum.com/u/Avatar)\
**Post date:** [October 16, 2006, 3:32pm UTC](https://www.ruby-forum.com/t/non-declared-variables/72323/2 "2006-10-16T15:32:49Z")

</div>

On Mon, 16 Oct 2006 04:38:09 -0700, Avatar wrote:

> create less compile time errors and more (harder to catch) runtime  
> errors?
> 
> Thoughts?

I was programming in Java last week, and writing typed code. I found  
that  
after about an hour of coding, my code would have worked correctly (it  
would have been less time if Java supported operator overloading the way  
ruby does), but I still had to spend another hour getting the types  
appropriately declared, and typecasting things correctly as I pulled  
objects out of their various collections.

C++ can be worse. I wanted to use the Boost Spirit library for CFG  
parsing, and found that I couldn’t read the compile errors I was  
getting because 90% of the text was multiply nested template types. The  
C++  
Standard Template Library uses templates so much to get close to Ruby’s  
flexibility, but the result is still more complicated than Ruby,  
impossible to debug, and less flexible. At that point, why not go all  
the  
way to a dynamically typed language?

–Ken B.

---

<div class="post-metadata">

**Author:** ![Avatar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8c91f0/32.png) [@Avatar](https://www.ruby-forum.com/u/Avatar)\
**Post date:** [October 16, 2006, 3:43pm UTC](https://www.ruby-forum.com/t/non-declared-variables/72323/3 "2006-10-16T15:43:20Z")

</div>

> C++ can be worse. I wanted to use the Boost Spirit library for CFG  
> parsing, and found that I couldn’t read the compile errors I was  
> getting because 90% of the text was multiply nested template types. The C++  
> Standard Template Library uses templates so much to get close to Ruby’s  
> flexibility, but the result is still more complicated than Ruby,  
> impossible to debug, and less flexible. At that point, why not go all the  
> way to a dynamically typed language?

Because then you’re a different language with different use cases with  
different benefits and drawbacks.

Boost is an incredibly good set of libraries, with an extremely high  
level of QA. The great tragedy is that facilities like this were not  
part of a C++ standard library a decade ago.

Still, the C++ Standards Committee are well aware of the problems that  
templates cause with respect to indecipherable error messages, which  
is why the new system of concepts has been proposed for C++0x, so the  
compiler can flag straight away when you’ve tried to instantiate a  
template parameter that won’t work.

Also, if you’re a genius, you can use template metaprogramming to do  
some really spanky compile-time shenanigans.

Martin

---

<div class="post-metadata">

**Author:** ![Avatar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8c91f0/32.png) [@Avatar](https://www.ruby-forum.com/u/Avatar)\
**Post date:** [October 16, 2006, 4:13pm UTC](https://www.ruby-forum.com/t/non-declared-variables/72323/4 "2006-10-16T16:13:43Z")

</div>

On 10/16/06, Avatar [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I would like to hear people’s opinions on the ability to use variables  
> without declaring them in dynamic languages like Ruby.

You mean without declaring their _type_, right?

It’s more accurate to say simply that variables have no type in Ruby;  
Rather the variable’s contents have a type.

In any case, Ruby requires that variables be declared before use just  
as any decent language worth the name does.

Martin

---

<div class="post-metadata">

**Author:** ![Avatar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8c91f0/32.png) [@Avatar](https://www.ruby-forum.com/u/Avatar)\
**Post date:** [October 16, 2006, 4:05pm UTC](https://www.ruby-forum.com/t/non-declared-variables/72323/5 "2006-10-16T16:05:41Z")

</div>

Avatar wrote:

> I would like to hear people’s opinions on the ability to use variables  
> without declaring them in dynamic languages like Ruby.

Could you clarify what you mean by that? Are you concerned about  
methods like #instance\_variable\_set and #class\_variable\_set? Or more  
generally about the fact that code like:

class Foo  
@bar = 12  
end

creates an instance variable on the fly at runtime, without requiring  
some sort of  
instance @bar  
line that ‘declares’ it before use?
