Sun, 29 Sep 2002 12:09:32 +0900, dblack@candle.superlink.net dblack@candle.superlink.net pisze:
The on-going discussion of explicit typing and Ruby has whetted my
appetite for traveling further into the typeless universe
Ruby is not typeless. Every Ruby object has a well-defined type.
In a perfectly typeless world, if when the only action is sending
a message, you wouldn’t be able to compute anything (at least
effectively).
Consider the integer addition. You send the message to a number passing
it another number, and the first number must somehow determine the
answer. How does it do that if all it can do is to pass objects around
and send messages? Which message does it send to the other object to
determine that it is a number? How does it determine its value?
What does Ruby do now?
static VALUE
fix_plus(x, y)
VALUE x, y;
{
if (FIXNUM_P(y)) {
[…]
}
if (TYPE(y) == T_FLOAT) {
return rb_float_new((double)FIX2LONG(x) + RFLOAT(y)->value);
}
return rb_num_coerce_bin(x, y);
}
It checks the type of the second argument! Evil!
But how would you do it otherwise?
(My answer: multi-dispatch; but I don’t know how it can fit Ruby.)
Now look at Array#==:
static VALUE
rb_ary_equal(ary1, ary2)
VALUE ary1, ary2;
{
long i;
if (ary1 == ary2) return Qtrue;
if (TYPE(ary2) != T_ARRAY) return Qfalse;
if (RARRAY(ary1)->len != RARRAY(ary2)->len) return Qfalse;
for (i=0; i<RARRAY(ary1)->len; i++) {
if (!rb_equal(RARRAY(ary1)->ptr[i], RARRAY(ary2)->ptr[i]))
return Qfalse;
}
return Qtrue;
}
Again it tests the type of the second object and returns false if
it’s not an array. It’s not Ruby or what?
You would perhaps want to send .size and with integer arguments
instead, to compare only values they return, ignoring the type.
But then you have:
[“a”] == {0 => “a”}
[97, 98, 99] == “abc”
and an error instead of false trying to compare
[“a”] == 10
because Fixnum doesn’t have .size.
···
–
__("< Marcin Kowalczyk
__/ qrczak@knm.org.pl
^^ Blog człowieka poczciwego.