Passing $ as a function argument

Dennis dkorpel at gmail.com
Wed Oct 10 23:26:38 UTC 2018


Can you give a real-world, non-foo/bar example where you want to 
use it? I have trouble understanding what you want to accomplish.

On Wednesday, 10 October 2018 at 23:04:46 UTC, James Japherson 
wrote:
> It also has no context in and of itself. The compiler knows 
> what to do with it... The same can be done with function 
> arguments. You just haven't thought about the problem enough.

> The usefulness comes from the case when bar is local:
> 
> void foo(int loc)
> {
>   auto bar = double[RandomPInt+1];
>
>   return bar[loc];
> }

That also brings some difficulties. What kind of code do you 
expect the compiler to generate when the declaration is unknown?

```
int getFromArray(int loc); // implemented in another file, 
compiled separately

void main() {
   getFromArray($); // what integer is passed?
}
```

Finally I want to note that accessing element $ is a range 
violation, $-1 is the index of the last element in an array. $ 
can be used as an endpoint for intervals (where the endpoint is 
excluded from the range):
```
auto popped = arr[1..$]; //pop the front element
auto elem = popped[$-1]; //okay, last element
auto err = popped[$]; //range violation
```


More information about the Digitalmars-d mailing list