Thread overview
Re: improving '$' to work with other functions (eg: indexed)
Oct 31, 2013
Jonathan M Davis
Oct 31, 2013
Timothee Cour
Oct 31, 2013
Jonathan M Davis
October 31, 2013
On Thursday, October 31, 2013 02:46:32 Timothee Cour wrote:
> can we support this and similar use cases ?
> 
> import std.range;
> void main(){
>   auto a=[1,2,3,4];
>   auto b1=a.indexed([0,a.length-1]);//OK
>   auto b2=a.indexed([0,$-1]);//NG
> }

Aren't you creating new arrays to pass to indexed here? If that's the case, $ isn't associated with any particular array, and I don't see how it could work.

Now, there's issue# 7177

http://d.puremagic.com/issues/show_bug.cgi?id=7177

which aims at getting opDollar to work with ranges which have length (which it generally doesn't at this point), so that might be related to what you're looking for, but if what you're looking for is [0, $-1] to work on its own, I don't see how that could ever work, because $ only works when slicing or indexing, and [0, $-1] in the context above is an array literal.

- Jonathan M Davis
October 31, 2013
well it can be made to work with following rule:

$ binds as follows, using the 1st pattern matched rule:

very roughly:

identifier [$] if typeof(a.length) => a[a.length] //likewise with s/[]/()/
primary_expression . identifier if typeof(primary_expression.length)
=> primary_expression . identifier [identifier.length] (recursively)
if no match is found, error.

eg:

[1,2,3].indexed([0,$-1] ) => indexed.length isn't valid, so it tries to
bind to [1,2,3].

intuitively I believe it makes sense.



On Thu, Oct 31, 2013 at 2:51 AM, Jonathan M Davis <jmdavisProg@gmx.com>wrote:

> On Thursday, October 31, 2013 02:46:32 Timothee Cour wrote:
> > can we support this and similar use cases ?
> >
> > import std.range;
> > void main(){
> >   auto a=[1,2,3,4];
> >   auto b1=a.indexed([0,a.length-1]);//OK
> >   auto b2=a.indexed([0,$-1]);//NG
> > }
>
> Aren't you creating new arrays to pass to indexed here? If that's the
> case, $
> isn't associated with any particular array, and I don't see how it could
> work.
>
> Now, there's issue# 7177
>
> http://d.puremagic.com/issues/show_bug.cgi?id=7177
>
> which aims at getting opDollar to work with ranges which have length
> (which it
> generally doesn't at this point), so that might be related to what you're
> looking for, but if what you're looking for is [0, $-1] to work on its
> own, I
> don't see how that could ever work, because $ only works when slicing or
> indexing, and [0, $-1] in the context above is an array literal.
>
> - Jonathan M Davis
>


October 31, 2013
On Thursday, October 31, 2013 14:46:29 Timothee Cour wrote:
> well it can be made to work with following rule:
> 
> $ binds as follows, using the 1st pattern matched rule:
> 
> very roughly:
> 
> identifier [$] if typeof(a.length) => a[a.length] //likewise with s/[]/()/
> primary_expression . identifier if typeof(primary_expression.length)
> => primary_expression . identifier [identifier.length] (recursively)
> if no match is found, error.
> 
> eg:
> 
> [1,2,3].indexed([0,$-1] ) => indexed.length isn't valid, so it tries to
> bind to [1,2,3].
> 
> intuitively I believe it makes sense.

Well, I can see why you'd think that that makes sense, but it would have surprised me greatly if I had seen code like that compile. And given how little benefit it really provides, I don't think that it's worth the extra complication to the language. So, I'd be against it, but feel free to open an enhancement request. Maybe a compiler dev will take a shine to the idea.

- Jonathan M Davis