struct constructors and function parameters

Adam Burton adz21c at gmail.com
Tue Nov 9 16:09:08 PST 2010


Simen kjaeraas wrote:

> Adam Burton <adz21c at gmail.com> wrote:
> 
>> Hi,
>> should the below work?
>>
>> struct A
>> {
>>     public this(B b) {}
>> }
>>
>> struct B {}
>>
>> void foo(A a) {}
>>
>> void main()
>> {
>>     B b;
>>     foo(b);     // Fails
>> }
>>
>> The constructor parameter doesn't need to be a struct, it could be an
>> int.
>> The workaround is to explicity call the constructor.
> 
> Far as I know, that's not supposed to work, no. Guess it has to do with
> overloading:
> 
> struct foo {
>      this( int n ){}
> }
> 
> void bar( foo f ) {}
> void bar( int n ) {}
> 
> bar( 3 ); // which do I call?
> 
True, I had thought of that, but I figured in that situation it would favour 
int over foo (in same way long vs int) so to access foo you'd need to 
explicitly use the constructor. On the other hand I suppose its more of a 
difficult decision when you hit something like below

struct A
{
   this(int)
}
struct B
{
   this(int)
}

void bar(A)
void bar(B)

Again though you could argue to explicitly specify the constructor of the 
appropriate struct.


More information about the Digitalmars-d-learn mailing list