Some questions

bearophile bearophileHUGS at mailas.com
Wed Aug 1 03:15:56 PDT 2007


Hello, more than one month ago I have started learning D (dmd V.1.015 and successive ones) on Windows, and I like it a lot. I know various languages, among them there is Python. In this my first post on digitalmars newsgroups I'd like to ask few things about some problems I have found that I hope you can explain/solve to/for me.
I have some suggestions too for D, but I'll probably post them in one or two posts on digitalmars.D. (In my posts I use 2 spaces indent to reduce line length, hopefully the web interface will keep them).


1a/1b) I am developing a package of some modules (using the really useful "bud") each one of them contains some unittests. I'd like a way to just ignore the main() function eventually present inside imported modules (or maybe to allow importing and using it as modulename.main()). So they can be used both as main modules and imported modules (Python allows something similar, using a different syntax). Related: if I have many modules, I don't want to unittest them all all the time, I'd like to unittest only the main module I am developing in that moment. Maybe you can suggests me ways to solve those two problems in a good way with no changes to the D language :-) (I currently comment out the main() of all the modules I am not developing at that moment, and I wait for the unittests of all the modules, but those aren't good solutions).

----------------------------

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) );
}

-------------------

3) Probably the last assert has to be true, is this a little bug of std.boxer?

import std.boxer;
int arraycmp(Ty)(Ty[] a1, Ty[] a2) {
  if (a1.length < a2.length) {
    foreach(i, field; a1) {
      if (field < a2[i]) return -1;
      else
        if (field > a2[i]) return 1;
    }
    return -1;
  } else {
    foreach(i, field; a2) {
      if (field < a1[i]) return 1;
      else
        if (field > a1[i]) return -1;
    }
    return (a1.length==a2.length)?0:1;
  }
}
void main() {
  float[] a1 = [1.0, 6.0];
  float[] a2 = [1.0, 35.0];
  assert( arraycmp(a1, a2) == -1 );
  assert( a1 < a2 );
  Box[] b1 = [box("aa"), box(6.0)];
  Box[] b2 = [box("aa"), box(35.0)];
  assert( arraycmp(b1, b2) == - 1 );
  assert( b1 < b2 ); // Error
}

-----------------------

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?

----------------------------

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
}

Bye and thank you,
bearophile


More information about the Digitalmars-d-learn mailing list