Passing $ as a function argument

James Japherson JJ at goolooking.com
Wed Oct 10 23:04:46 UTC 2018


On Wednesday, 10 October 2018 at 13:32:15 UTC, Simen Kjærås wrote:
> On Wednesday, 10 October 2018 at 08:46:42 UTC, James Japherson 
> wrote:
>> Would be nice to be able to pass $ as a function argument to 
>> be used in automatic path length traversing.
>>
>>
>> void foo(int loc)
>> {
>>    return bar[loc];
>> }
>>
>> then foo($) would essentilly become
>>
>> foo(&)
>>
>>   becomes ==>
>>
>>    return bar[$];
>>
>>
>> instead of having do to thinks like foo(bar.length).
>>
>> The usefulness comes from the case when bar is local:
>>
>> void foo(int loc)
>> {
>>    auto bar = double[RandomPInt+1];
>>
>>    return bar[loc];
>> }
>>
>>
>> then foo($) always returns a value and the outside world does 
>> not need to know about foo. Since $ is a compile thing 
>> expression and not used anywhere else this can always be 
>> done(it is a symbolic substitution and has a direct 
>> translation in to standard D code except $ cannot be used as 
>> arguments like this the current D language grammar).
>
> $ requires context (the array) for its value to be known - it's 
> not a compile-time expression any more than rand() + 
> currentWeather(getGpsCoordinates()) is. If $ were a valid 
> identifier, you could do something like this:
>
> struct Sentinel {}
> Sentinel $;
>
> void foo(T)(T loc) {
>     auto bar = double[RandomPInt+1];
>     static if (is(T == Sentinel)) {
>         return bar[$];
>     } else {
>         return bar[loc];
>     }
> }
>
> unittest {
>     foo($);
> }
>
> Note that this would turn foo into a template, so that foo($) 
> creates a separate function from foo(3).
>
> Since $ isn't a valid identifier, this is currently impossible, 
> but bachmeier's suggestion of foo!"$" works:
>
> void foo(string s = "")(int loc = 0)
> if (s == "" || s == "$") {
>     auto bar = double[RandomPInt+1];
>     static if (s == "$") {
>         return bar[$];
>     } else {
>         return bar[loc];
>     }
> }
>
> --
>   Simen

The whole point is not to use $ as an identifier but to specify 
to the compiler of that it can rewrite it.

You seem to think that what the compiler does is absolute 
authority. This is why I said "It would be nice".... meaning that 
if we had some a feature(which is entirely doable, not some 
mathematical impossibility), it would allow one to express the 
limit of an index in a concise way. Your templated version is not 
concise. All you really proved is that the compiler can be given 
a rewrite rule and handle this nicely.

$ is not an used for identifiers, it is used to specify that the 
maximum length of the array it is used in is to be used. It is 
short hand for doing hacks such as specifying -1 for maximum 
length, etc.

You seem to to have not understood the problem.

I mean, don't you understand that the entire point of $ in the 
first place is just syntactic sugar?

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.




More information about the Digitalmars-d mailing list