improving '$' to work with other functions (eg: indexed)

Timothee Cour thelastmammoth at gmail.com
Thu Oct 31 14:46:29 PDT 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 at 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20131031/ee9b2d3d/attachment.html>


More information about the Digitalmars-d-learn mailing list