Some questions
Deewiant
deewiant.doesnotlike.spam at gmail.com
Wed Aug 1 03:58:04 PDT 2007
bearophile wrote:
> 2) Do you know why the following isin2() doesn't work?
>
> class Gen {
> int stop;
> this(int instop) { stop = instop; }
> int opApply(int delegate(ref int) dg) {
> int result;
> for (int i = 0; i < stop; i++) {
> result = dg(i);
> if (result) break;
> }
> return 0;
> }
> }
> bool isin1(Gen items, int el) {
> bool result = false;
> foreach(el2; items)
> if (el == el2) {
> result = true;
> break;
> }
> return result;
> }
> bool isin2(Gen items, int el) {
> foreach(el2; items)
> if (el == el2)
> return true;
> return false;
> }
> void main() {
> assert(isin1(new Gen(10), 5) );
> assert(!isin1(new Gen(10), 20) );
> assert(isin2(new Gen(10), 5) );
> assert(!isin2(new Gen(10), 20) );
> }
You're returning 0, instead of result, in opApply.
> 4) This code:
> foreach (k, v; ["ab":12])
> putr(k, ' ', v);
>
> Gives me this error:
> Error: cannot have out or ref parameter of type char[2u]
> Is this a bug of DMD, or some limitation I have to know about?
"ab" is of type char[2]. If you want a char[] (and you do), write "ab"[] or the
more verbose cast(char[])"ab".
putr evidently takes its third parameter as inout/out/ref, but you can't do that
with static arrays, as the error message says. You need to pass a dynamic array
instead.
> 5) Is this a bug of the function overload system? If the static if is set to
> false it gives an error, the only difference is in the order of the
> definitions of the overloard alternative() function:
>
> import std.stdio;
>
> int fun(TyElem)(TyElem[] arr) {
> return arr.length;
> }
>
> static if (true) { // change this
> alias fun!(char) alternative;
> int alternative(int[] arr) {
> return arr[0];
> }
> } else {
> int alternative(int[] arr) {
> return arr[0];
> }
> alias fun!(char) alternative;
> }
>
> void caller(TyOp)(TyOp op, char[] s) {
> writefln( op(s) );
> }
>
> void main() {
> caller(&alternative, "abc"); // prints 3
> }
>
It's a bug, probably 810: http://d.puremagic.com/issues/show_bug.cgi?id=810
--
Remove ".doesnotlike.spam" from the mail address.
More information about the Digitalmars-d-learn
mailing list