On May 15, 2006, at 2:04 PM, Regg wrote:

> In your example:
>
> " struct vector a = {2.0, 3.0, 4.0};
>  struct some24ByteStuct b = (struct some24ByteStruct)a;"
>
> You are manually forcing this relationship my casting one type to
> another.
>
> To me that is one of the things I love about C is that it will  
> allow you
> do something that the compiler otherwise would not allow.

Sure, but in order for you to manipulate that data in the new  
structure, you probably have to translate the data from the first  
form to the other anyway. An explicit cast is really most useful when  
casting from one primitive to another (moot in Ruby, of course), but  
not for more complex data types (excluding buffers, which is the only  
special case I can think of right now... also moot in Ruby).

> But in Ruby you are not "forcing" the type casting, it just does it
> without any warning or error.

There is no type casting going on in the Ruby code that I can see. Do  
you mean the reassignment of the variable to an object of a different  
type? There is no conversion taking place.

> What benefit is gained from a variable being (switched/pointed to)  
> from
> one type to another? (This is an honest question, maybe I'm missing
> something)
>
> It seems like a recipe for disaster.

Consider this:

class Foo
   def do_something
     puts "FOO!"
   end
end

class Bar
   def do_something
     puts "BAR!"
   end
end

def call_do_something(arg)
   arg.do_something
end

call_do_something(Foo.new)
call_do_something(Bar.new)

Notice something cool here? We didn't have to subclass to pass two  
different types into the same method. This is known by some as "duck  
typing," referring to the "looks like a duck, talks like a duck"  
metaphor. Any object that responds to the do_something method can be  
passed into the call_do_something method, and it will work perfectly.  
This is powerful, and not as dangerous as you think.

- Jake McArthur