More on boxer

bearophile bearophileHUGS at lycos.com
Thu Apr 17 02:26:58 PDT 2008


I think I may have found another problem with boxer/writefln:

import std.stdio: writefln;
import std.boxer: box, Box;
import d.func: putr;

void main() {
  auto a = new Box[1];
  a[0] = box(a);
  writefln(a); // Error: Stack Overflow
  putr(a); // Error: Stack Overflow
}

(I have to fix my str/repr/put/putr still for such situation).
Note that in Python there isn't such bug:

>>> a = [1, 2]
>>> a[1] = a
>>> print a
[1, [...]]

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

I think cmp among arrays or Box is buggy so if you need it you may use something similar to the following (not much tested still):

import std.boxer: Box, boxArray;
import std.stdio: writefln;

int boxArrayCmp(Box[] ba1, Box[] ba2) {
    if (ba1.length < ba2.length) {
        foreach (i, field; ba1) {
            if (field < ba2[i])
                return -1;
            else
                if (field > ba2[i])
                return 1;
        }
        return -1;
    } else {
        foreach (i, field; ba2) {
            if (field < ba1[i])
                return 1;
            else
                if (field > ba1[i])
                return -1;
        }
        if (ba1.length == ba2.length)
            return 0;
        else
            return 1;
    }
}

void main() {
    auto b1 = boxArray(10, 20, 15.5);
    auto b2 = boxArray(10, 20, 15.6);
    auto b3 = boxArray(10, 20.0, 15.5);
    auto b4 = boxArray(10, 20.0);
    
    writefln(boxArrayCmp(b1, b2), " ", b1 < b2);  // -1 false
    writefln(boxArrayCmp(b2, b1), " ", b2 > b1);  // 1 false
    writefln(boxArrayCmp(b1, b3), " ", b1 == b3); // 0 false
    writefln(boxArrayCmp(b1, b4), " ", b1 > b4);  // 1 true
}

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list