Titanion 0.4 + Phobos2 bug
bearophile
bearophileHUGS at lycos.com
Tue Apr 28 02:03:00 PDT 2009
Robert Jacques:
> struct Vec(T = float,size_t N = 3) {
> T[N] _data;
> alias _data this;
> string toString() { std.conv.return text(_data); }
> }
Very nice.
> plus functions as array properties which apparently got upgraded at some
> point to the extra () isn't needed anymore. ( i.e. vec.x instead of
> vec.x() )
>
> i.e.
>
> T x(T, size_t N)(T[N] v) {
> return v[0];
> }
A name like "x" is too much common, I don't like that as free function.
I like this better (I have borrowed SeriesGen2 from my D1 dlibs):
import std.string: toString, writeln;
import std.conv: to;
import std.metastrings: Format, ToString;
template SeriesGen2(string txt, string separator, int max, int min=0) {
static if (min > max)
const SeriesGen2 = "";
else static if (min == max)
const SeriesGen2 = Format!(txt, ToString!(max),
ToString!(max));
else
const SeriesGen2 = SeriesGen2!(txt, separator, max-1, min) ~ separator ~
Format!(txt, ToString!(max),
ToString!(max));
}
struct Vec(T=float, size_t N=3) {
static assert(N > 0);
T[N] _data;
alias _data this;
string toString() { return to!string(this._data); }
// You may also use SeriesGen4 here
mixin(SeriesGen2!("T d%s(size_t i){ return this._data[%s]; }", "\n", N-1));
mixin(SeriesGen2!("void d%s(T value){ this._data[%s] = value; }", "\n", N-1));
}
Vec!(T, N) foo(T, size_t N)(T[N] v) {
Vec!(T,N) r = v;
return r;
}
void main() {
float[3] a = [1,2,3];
auto v = foo(a);
writeln(v, " ", a[2], " ", v.d1);
v.d0 = 5;
writeln(v);
}
Unfortunately I think I have found another bug. If I run:
import std.metastrings: Format;
pragma(msg, Format!("good %s test", 5));
void main() {}
D1 outputs correctly:
good 5 test
But D2 outputs the wrong:
good %s test5
This also may mean that std.metastrings module lacks compile-time unittests (some static asserts suffice).
Bye,
bearophile
More information about the Digitalmars-d-announce
mailing list