digitalmars.D.learn - Playing with ranges and ufcs
- Andrea Fontana (5/5) Feb 27 2013 This command:
- bearophile (29/33) Feb 27 2013 Please file it in bugzilla.
- Jonathan M Davis (6/28) Feb 27 2013 What it should be doing is using version(assert) to throw a RangeError i...
- bearophile (22/27) Feb 27 2013 Do you mean something like this?
- bearophile (13/21) Feb 27 2013 This is the bug opened by Andrea Fontana for this thread:
- bearophile (7/10) Feb 27 2013 What I meant to say is that if the assert(i <= j) is inside the
- Jonathan M Davis (4/13) Feb 27 2013 Honestly, I think that that's a complete pipe dream anyway, but the poin...
- Andrea Fontana (5/8) Feb 28 2013 I tried it too, hoping for something like
- Jonathan M Davis (4/25) Feb 27 2013 Because then it more closely matches how arrays work. The only part that...
- bearophile (7/12) Feb 27 2013 I will keep dreaming for some more decades. In technology
- Andrea Fontana (2/40) Feb 27 2013 Done!
- bearophile (5/6) Feb 27 2013 I was about to file it myself, so I have added a bit more meat to
This command: writeln(iota(10).cycle()[5..2].take(4)); print: [5, 6, 7, 8] Shouldn't [5..2] slice throw a compile/runtime error?
Feb 27 2013
Andrea Fontana:writeln(iota(10).cycle()[5..2].take(4)); print: [5, 6, 7, 8] Shouldn't [5..2] slice throw a compile/runtime error?Please file it in bugzilla. The opSlice of cycle() lacks those pre-conditions or tests, and there are not enough unittests in Phobos to catch this simple bug: auto opSlice(size_t i, size_t j) { auto retval = this.save; retval._index += i; return takeExactly(retval, j - i); } j - i is positive because those numbers are unsigned, and because D lacks a run-time errors for integral values because the stupid Walter thinks those run-time are too much slow (it's not actually true. Even Clang designers have understood it), and Don thinks that Everything is Fine in D: import std.stdio; struct Foo { auto opSlice(size_t i, size_t j) { writeln(j - i); // Prints: 4294967293 } } void main() { Foo f; f[5 .. 2]; } Maybe we need to run something similar to QuickCheck (http://en.wikipedia.org/wiki/QuickCheck ) on Phobos. Bye, bearophile
Feb 27 2013
On Wednesday, February 27, 2013 23:48:05 bearophile wrote:Andrea Fontana:What it should be doing is using version(assert) to throw a RangeError if the arguments are invalid, but the addition of version(assert) is quite recent (previously, the best that could have been done was to assert). That's the general policy, but it's not always followed like it should be. - Jonathan M Daviswriteln(iota(10).cycle()[5..2].take(4)); print: [5, 6, 7, 8] Shouldn't [5..2] slice throw a compile/runtime error?Please file it in bugzilla. The opSlice of cycle() lacks those pre-conditions or tests, and there are not enough unittests in Phobos to catch this simple bug: auto opSlice(size_t i, size_t j) { auto retval = this.save; retval._index += i; return takeExactly(retval, j - i); }
Feb 27 2013
Jonathan M Davis:What it should be doing is using version(assert) to throw a RangeError if the arguments are invalid, but the addition of version(assert) is quite recent (previously, the best that could have been done was to assert).Do you mean something like this? auto opSlice(size_t i, size_t j) { version(assert) { if (i > j) throw new RangeError("some message here"); } auto retval = this.save; retval._index += i; return takeExactly(retval, j - i); } What's the advantage of that compared to using a normal contract? auto opSlice(size_t i, size_t j) in { assert(i <= j, "some message here"); } body { auto retval = this.save; retval._index += i; return takeExactly(retval, j - i); } Bye, bearophile
Feb 27 2013
auto opSlice(size_t i, size_t j) in { assert(i <= j, "some message here"); } body { auto retval = this.save; retval._index += i; return takeExactly(retval, j - i); }This is the bug opened by Andrea Fontana for this thread: http://d.puremagic.com/issues/show_bug.cgi?id=9612 At its start I show this code, that contains a but that the D compiler is able to catch at compile-time: void main() { auto a = [0,1,2,3,4,5,6,7,8,9][5 .. 2]; } To do the same with user-defined structures time ago I have suggested this, that is currently closed waiting for a better solution: http://d.puremagic.com/issues/show_bug.cgi?id=5906 Bye, bearophile
Feb 27 2013
To do the same with user-defined structures time ago I have suggested this, that is currently closed waiting for a better solution:What I meant to say is that if the assert(i <= j) is inside the pre-condition then there's a hope to run it at compile time introducing some new trick inside the D language. But if you put code inside a version(assert) inside the body of opSlice() then the possibility of inventing a trick to do it is much lower. Bye, bearophile
Feb 27 2013
On Thursday, February 28, 2013 00:46:56 bearophile wrote:Honestly, I think that that's a complete pipe dream anyway, but the point is to make it act like arrays, and they use RangeError. - Jonathan M DavisTo do the same with user-defined structures time ago I have suggested this, that is currently closed waiting for a bettersolution:What I meant to say is that if the assert(i <= j) is inside the pre-condition then there's a hope to run it at compile time introducing some new trick inside the D language. But if you put code inside a version(assert) inside the body of opSlice() then the possibility of inventing a trick to do it is much lower.
Feb 27 2013
On Wednesday, 27 February 2013 at 23:43:49 UTC, bearophile wrote:void main() { auto a = [0,1,2,3,4,5,6,7,8,9][5 .. 2]; }I tried it too, hoping for something like [5,4,3] or at least [4,3,2] (== [0,1,2,3,4,5,6,7,8,9][2..5].reverse)
Feb 28 2013
On Thursday, February 28, 2013 00:35:08 bearophile wrote:Jonathan M Davis:Because then it more closely matches how arrays work. The only part that doesn't is that it's fully tied to -release rather than -noboundschecked. - Jonathan M DavisWhat it should be doing is using version(assert) to throw a RangeError if the arguments are invalid, but the addition of version(assert) is quite recent (previously, the best that could have been done was to assert).Do you mean something like this? auto opSlice(size_t i, size_t j) { version(assert) { if (i > j) throw new RangeError("some message here"); } auto retval = this.save; retval._index += i; return takeExactly(retval, j - i); } What's the advantage of that compared to using a normal contract?
Feb 27 2013
Jonathan M Davis:Because then it more closely matches how arrays work. The only part that doesn't is that it's fully tied to -release rather than -noboundschecked.I see, thank you.Honestly, I think that that's a complete pipe dream anyway,I will keep dreaming for some more decades. In technology progress comes from dreamers that have the skills to create things like the Whiley (http://whiley.org/ ) language. Bye, bearophile
Feb 27 2013
On Wednesday, 27 February 2013 at 22:48:06 UTC, bearophile wrote:Andrea Fontana:Done!writeln(iota(10).cycle()[5..2].take(4)); print: [5, 6, 7, 8] Shouldn't [5..2] slice throw a compile/runtime error?Please file it in bugzilla. The opSlice of cycle() lacks those pre-conditions or tests, and there are not enough unittests in Phobos to catch this simple bug: auto opSlice(size_t i, size_t j) { auto retval = this.save; retval._index += i; return takeExactly(retval, j - i); } j - i is positive because those numbers are unsigned, and because D lacks a run-time errors for integral values because the stupid Walter thinks those run-time are too much slow (it's not actually true. Even Clang designers have understood it), and Don thinks that Everything is Fine in D: import std.stdio; struct Foo { auto opSlice(size_t i, size_t j) { writeln(j - i); // Prints: 4294967293 } } void main() { Foo f; f[5 .. 2]; } Maybe we need to run something similar to QuickCheck (http://en.wikipedia.org/wiki/QuickCheck ) on Phobos. Bye, bearophile
Feb 27 2013
Andrea Fontana:Done!I was about to file it myself, so I have added a bit more meat to your bug report. Bye, bearophile
Feb 27 2013