On Thu, Jan 29, 2009 at 1:48 PM, Bill Baxter <wbax...@gmail.com> wrote: > On Thu, Jan 29, 2009 at 1:37 PM, Andrei Alexandrescu > <seewebsiteforem...@erdani.org> wrote: >> I stumbled upon a very interesting problem. Consider an infinite range that >> generates the numbers 1, 2, 3, ... >> >> That range doesn't have a "length" member. However, it is a random access >> range, which makes things rather interesting. Now consider I want to advance >> 10 steps in that range. Being an obedient D programmer I'd write: >> >> auto r = iota(1); >> // skip 10 steps >> r = r[10 .. $]; >> >> Now this is very cool. If a range is infinite, I can use the $ symbol in the >> right position, but nothing else. So I tried to effect that and got the >> following to compile: >> >> struct DollarType {} >> enum DollarType __dollar = DollarType(); >> >> struct S >> { >> void opSlice(uint, DollarType) >> { >> } >> } >> >> void main() >> { >> S s; >> s[0 .. $]; >> } >> >> This is cool because it allows detection of passing $. Now the problem is, I >> can't seem to get rid of the __dollar definition! Has anyone found a >> general-enough trick? I'd want a[...$...] to morph the $ into a.length *iff* >> a defines length, and go with the __dollar otherwise. > > You need to give your DollarType an int and some arithmetic operators > and have it represent an /offset/ from the end rather than signifying > the end itself. > Otherwise a[$-1] can't work. > > Once you do that, then you can do things like > struct S > { > T opSlice(uint a, DollarType b) { > return opSlice(a, length+b.offset); > } > T opSlice(uint a, uint b) { > .... > } > ... > }
Nevermind, it just sunk in what you meant. I think it's only a problem when you want to have some things in a module with special __dollar and some with not? Or I suppose you're probably trying to make template things which may or may not be infinite? I guess what's required is the ability to define the __dollar in the scope of a class/struct/template. --bb