Module function conflicting with reserve function

Stanislav Blinov stanislav.blinov at gmail.com
Tue Nov 6 21:03:01 UTC 2018


On Tuesday, 6 November 2018 at 20:40:11 UTC, Peter Campbell wrote:
> Hi there. I've been playing with D and have encountered this 
> really awkward behaviour. Basically I'm getting a compiler 
> error inside a function I wrote in my module as it thinks I'm 
> trying to call itself with invalid parameters, when actually I 
> want it to call the reserve function on the array itself. Is 
> this a bug or expected behaviour? It seems quite strange and 
> potentially annoying to me.

It's not a bug, just the way name resolution works. Better have 
collision than silent overloads. Possible solutions:

```
void reserve(ref Bob system, in size_t capacity) {
      // explicitly disambiguate
      object.reserve(system._data, capacity);
}
```

or:

// pull in the runtime 'reserve' into this module's scope
alias reserve = object.reserve;

void reserve(ref Bob system, in size_t capacity) {
      // call reserve as usual
      system._data.reserve(capacity);
}


More information about the Digitalmars-d-learn mailing list