digitalmars.D - $, __dollar, opDollar: can't define my own?
- Chris Miller (5/5) Oct 03 2006 There should be an opDollar, or some other name, such as opEnd, to
- Oskar Linde (45/51) Oct 03 2006 It would also be very nice to support $ in some way for multidimensional...
- Andrey Khropov (8/15) Oct 04 2006 Yes, I'm also curious why slices aren't first class objects in D:
- Fredrik Olsson (20/28) Oct 04 2006 YES! Ranges and sets, as I have pleaded for ages. It is a bliss for
- icee (1/3) Oct 04 2006 just take the place of .length, not a separate overload.
There should be an opDollar, or some other name, such as opEnd, to overload $ for user-defined classes/structs/unions. Or instead, it could even simply just access a length member if it exists. I have a struct that wraps an array and works like an array in almost every way except for the $ feature of arrays.
Oct 03 2006
Chris Miller wrote:There should be an opDollar, or some other name, such as opEnd, to overload $ for user-defined classes/structs/unions. Or instead, it could even simply just access a length member if it exists. I have a struct that wraps an array and works like an array in almost every way except for the $ feature of arrays.It would also be very nice to support $ in some way for multidimensional indexing: myclass[$-1, $-1] myclass[1..$-1, x % $] For some of my own classes, I have a templated opIndex that supports: myclass[end-1,end-1] myclass[range(1,end-1), x % end] etc... It would be really nice to get the same functionality with the first syntax. My solution is to make special types. Briefly: struct End { EndRel opMinus(int i) { EndRel x = {i}; return x; } EndMod opMod_r(int i) { EndMod x = {i}; return x; } } End end; // Global end instance struct EndRel { int i; } struct EndMod { int i; } struct FullRange {} Fullrange all; // Global all range struct Range { int start, end; } struct EndRange { int start; } struct EndRelRange { int start; int endrel; } Range range(int start, int end) { Range x = {start,end}; return x; } EndRange range(int start, End dummy) { EndRange x = {start}; return x; } EndRelRange range(int start, EndRel endrel) { EndRelRange x = {start,endrel}; return x; } The class template code for opIndex then need to handle arguments of types int, End, EndRel, EndMod, FullRange, Range, EndRange, EndRelRange, etc... This may all be a bit overkill, but what would be really great is having a range type that a..b expands into at lest. That type could then support opApply making foreach(i; 0..10) { ... } possible for instance. /Oskar
Oct 03 2006
Oskar Linde wrote:
This may all be a bit overkill, but what would be really great is having a
range type that a..b expands into at lest. That type could then support
opApply making
foreach(i; 0..10) { ... }
possible for instance.
Yes, I'm also curious why slices aren't first class objects in D:
http://www.digitalmars.com/d/archives/40556.html.
It would be just so handy, especially if you do a lot of vector/matrix/tensor
operations. I just got sick and tired writing infinite
"for(int i = 0; i < n; ++i)" in my C++ code :(
--
AKhropov
Oct 04 2006
Oskar Linde skrev:
<snip>
This may all be a bit overkill, but what would be really great is having
a range type that a..b expands into at lest. That type could then
support opApply making
foreach(i; 0..10) { ... }
possible for instance.
YES! Ranges and sets, as I have pleaded for ages. It is a bliss for
reducing code size, and allows some very nice readability stuff. It is
not that hard to implement either, most Pascal implementations does it
with a simple bit-field.
Doing it with templates and tricks gets you the functionality, but
misses the point of a clean and readable syntax.
// is it lowercase ASCII?
if (ch in 'a'..'z') { ... }
// Is the selection bold?
if (TextAttriibutes.BOLD in text.selection.attributes() { ... }
// Make selected text underline.
text.selection.attributes += TextAttriibutes.UNDERLINE;
// Get all days where both Adam and Beatrice are free, but not monday.
goodDays = adam.availableDays - beatrice.availableDays - Days.MONDAY;
Ranges and sets are priceless for handling "a bag of values", without
the need to write error prone iterations, or just as error prone and
large boolean expressions.
// Fredrik Olsson
Oct 04 2006
overload $ for user-defined classes/structs/unions. Or instead, it could even simply just access a length member if it exists.just take the place of .length, not a separate overload.
Oct 04 2006









"Andrey Khropov" <andkhropov_nosp m_mtu-net.ru> 