-
-
Notifications
You must be signed in to change notification settings - Fork 379
Document opDollar (issue 7520) #292
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
opDollar can also be a non-template function, in which case no compile-time index is passed to it. |
Updated. Hmm, it just occurred to me. is it possible to declare opDollar with a runtime argument instead? Or only compile-time index is supported? |
Only compile-time index is supported. It's always known at compile time, so there's no point to a runtime version. |
Hmm, apparently the compiler rejects it if you use more than one argument in [] but declare opDollar without the compile-time argument. |
Why not ask @9rnsr, he implemented this and likely knows all working cases rather than us guessing what works and doesnt. |
@AndrejMitrovic No problem. @donc is the first opDollar patch implementer (see bug 3474, so he knows about the detail well. I can say the things he talked are correct. More details:
|
It might be interesting to note that ˋopDollarˋ is not restricted in its return type, and can return any type you want, provided it is accepted by opSlice (or opDollar). This is exploited with infinite ranges, as a convenient way to random access "slice to end": ˋmyInfiniteRange[5 .. $];ˋ. Basically, opDollar just returns a state-less object typed as ˋstruct DollarTokenˋ, and then, an ˋopSlice(size_t low, DollarToken)ˋ overload is provided. The actual value returned by ˋopDollarˋ is irrelevent here, and only the static type it carries is of importance. As a matter of fact, in that case, opDollar can even be declared as a compile time known enum: ˋenum opDollar = DolarToken.init;ˋ Some RA ranges with "expensive but still o(1) length" can also exploit this: If the rane knows you want to slice to the end, it can get away with not evaluating the actual length (for example, range.chunks, in an open pull request). If you want to see some code example, look in std.range: repeat, cycle, chunks, and a couple others exploit this. |
I think there was a recent optimization that when you slice [$ .. $], then opDollar is only evaluated once. This is the impression I was given looking at a compiler change that had broken one of my pulls, but I could be mistaken. Not sure it is worth documenting though. |
This is absolutely positively a bug. This is an optimization that is observable by the code. It is invalid. Please file a bug. Stuff like that is not OK. |
That's what I kind of thought actually: Is it allowed to do that? In any case, the first thing to do is verify my claims: They could be wrong. I won't have a test machine for a couple of days though, that I won't be able to confirm or deny myself (much less file a bug) in the near future. |
I think I missunderstood this: It is actual the "ce" in "(ce)[0 .. $]" that got only evaluated once. False alarm then. |
} | ||
|
||
void test() { | ||
Rectangle r(10,20); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a C++'ism, not valid D syntax.
Expand with feedback from Kenji, Don, et al.
This looks good. Are there more examples to be added or are we good with this so far? |
It's better to open new pull requests for more examples later. I merge this now. |
I got tired of waiting for somebody to do this, so I decided to do it myself. Please review and tell me if my understanding of opDollar is off. :)