How about "auto" parameters?

Ary Manzana ary at esperanto.org.ar
Wed Jun 8 02:20:59 PDT 2011


On 6/7/11 9:50 PM, Andrei Alexandrescu wrote:
> On 6/7/11 5:35 AM, Ary Manzana wrote:
>> Well, in Ruby every parameter type is auto. And it works pretty well. :-)
>>
>> When you invoke a method and something goes wrong, like the auto
>> parameter doesn't have a method, it gives a sensible error message.
>
> It does, just at run time. The way dynamic languages and static
> languages set up computation is very different and as such difficult to
> compare something as core as function call resolution as equals for equals.
>
>> So:
>>
>> void foo(auto parameter) {
>> return parameter * 2;
>> }
>>
>> could just be the same as
>>
>> void foo(T)(T parameter) {
>> return parameter * 2;
>> }
>>
>> just with a nicer syntax (but I understand it doesn't add much).
>
> The counterpoint is that you seldom want to write foo() as written
> above. You want to clarify what family of types it is intended for.

Why? Note that in Ruby you *NEVER* say the family of types, and that is 
a huge bennefit. Good tests and good documentation are better than 
restricting the time. Look at this:

======================================================
import std.stdio;

// This is Ruby :-)
auto foo(T)(T param) {
   return param * 2;
}

class MyClass {
   int value;

   this(int value) {
     this.value = value;
   }

   int opBinary(string op)(int other) if (op == "*") {
     return value * other;
   }
}

int main() {
   auto x = foo(3);
   auto y = foo(6L);
   auto z = foo(new MyClass(10));

   writefln("%s", x);
   writefln("%s", y);
   writefln("%s", z);
   return 0;
}
======================================================

This function:

auto foo(T)(T param) {
   return param * 2;
}

Could be just as well:

auto foo(auto param) {
   return param * 2;
}

Yes, in Ruby it is checked on runtime, on D it is checked on compile-time.

What's cool about "auto param"? I have written a function once and it 
will work for everything that can be multiplied and it will be compiled 
efficiently for every type that I pass that complies to the function. 
Why you find it useless? Why do I have to restrict the type? I can 
restrict the type just for the operations being performed on it and then 
my function is open to any param that accepts those operations.

I know, I know. This is the same as just a template, but with a 
different syntax. I just don't understand why you think you will almost 
always would like to restrict the type you pass to the function. If you 
do, you limit your code. If you don't, you open your code and you have 
to write less code.


More information about the Digitalmars-d mailing list