Implicit type conversion for function calls?

bearophile bearophileHUGS at lycos.com
Sun Jul 10 07:41:17 PDT 2011


Simen Kjaeraas:

> I'm trying to have this sort of code compile:
> 
> ////////////////
> struct Foo {
>       int data;
>       this(int n) {
>           data = n;
>       }
> }
> 
> void bar(Foo f) {}
> 
> bar(3);
> ////////////////
> 
> Is this even possible?

There is a uncommon syntax that allows you to do it with a class, but I don't know why structs don't work here:

class Foo {
    int data;

    this(int n) {
        data = n;
    }
}

void bar(Foo f ...) {
    assert(f.data == 3);
}

void main() {
    bar(3);
}

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list