Re: A use case for fromStringz
On 04/16/2011 06:55 AM, Andrej Mitrovic wrote: I wonder.. in all these years.. have they ever thought about using a convention in C where the length is embedded as a 32/64bit value at the pointed location of a pointer, followed by the array contents? Sometimes called Pascal strings (actually, IIRC, the length is at the address /before/ the one pointed by the pointer). One of the important diffs between C Pascal from the practical pov. Actually, it's the same diff as C arrays vs true arrays like D's. Denis -- _ vita es estrany spir.wikidot.com
Vector operations doesn't convert to a common type?
int[3] a = [1,2,4]; float[3] b = [1,2,4]; float[3] c; // Why doesn't this work? c = a[] + b[]; // Error: incompatible types for ((a[]) + (b[])): 'int[]' and 'float[]' // When this works? c[0] = a[0] + b[0]; c[1] = a[1] + b[1]; c[2] = a[2] + b[2]; assert(c == [2,4,8]);
Re: Vector operations doesn't convert to a common type?
On 16.04.2011 12:12, simendsjo wrote: int[3] a = [1,2,4]; float[3] b = [1,2,4]; float[3] c; // Why doesn't this work? c = a[] + b[]; // Error: incompatible types for ((a[]) + (b[])): 'int[]' and 'float[]' // When this works? c[0] = a[0] + b[0]; c[1] = a[1] + b[1]; c[2] = a[2] + b[2]; assert(c == [2,4,8]); I tried using a template mixin to avoid having to type this all over, but I cannot get it to work.. I think I have something wrong with my alias usage, but that's just speculating :) mixin template ArrayVectorOp(string op, alias dest, alias a, alias b, int I) if(dest.length == a.length dest.length == b.length I = 1 I = dest.length) { // dest[I-1] = a[I-1] op b[I-1] mixin(dest.stringof~[~(I-1).stringof~] = ~a.stringof~[~(I-1).stringof~] ~op ~b.stringof~[~(I-1).stringof~];); static if(I 1) mixin ArrayVectorOp!(op, dest, a, b, I-1); } void main() { int[3] a = [1, 2, 3]; float[3] b = [2.1, 3.2, 4.3]; float[3] c; mixin ArrayVectorOp!(+, c, a, b, a.length); assert(c == [3.1, 5.2, 7.3]); } Gives the following output: t.d(4): no identifier for declarator c[3 - 1] t.d(4): Error: c is used as a type t.d(4): Error: cannot implicitly convert expression (cast(float)a[2u] + b[2u]) o f type float to const(_error_[]) t.d(4): no identifier for declarator c[2 - 1] t.d(4): Error: c is used as a type t.d(4): Error: cannot implicitly convert expression (cast(float)a[1u] + b[1u]) o f type float to const(_error_[]) t.d(4): no identifier for declarator c[1 - 1] t.d(4): Error: c is used as a type t.d(4): Error: cannot implicitly convert expression (cast(float)a[0u] + b[0u]) o f type float to const(_error_[]) t.d(11): Error: mixin t.main.ArrayVectorOp!(+,c,a,b,3u).ArrayVectorOp!(op,c,a, b,2).ArrayVectorOp!(op,c,a,b,1) error instantiating t.d(11): Error: mixin t.main.ArrayVectorOp!(+,c,a,b,3u).ArrayVectorOp!(op,c,a, b,2) error instantiating t.d(18): Error: mixin t.main.ArrayVectorOp!(+,c,a,b,3u) error instantiating
auto arr = new int[10];
is there any different b/w: auto arr = new int[10]; and int[10] arr; ?
Re: auto arr = new int[10];
%u wrote: is there any different b/w: auto arr = new int[10]; arr is dynamic array of int with ten elements and int[10] arr; ? arr is static array of int with ten elements
Re: Vector operations doesn't convert to a common type?
simendsjo: int[3] a = [1,2,4]; float[3] b = [1,2,4]; float[3] c; // Why doesn't this work? c = a[] + b[]; // Error: incompatible types for ((a[]) + (b[])): 'int[]' and 'float[]' // When this works? c[0] = a[0] + b[0]; c[1] = a[1] + b[1]; c[2] = a[2] + b[2]; assert(c == [2,4,8]); Vector ops are often implemented in assembly, and despite they are less flexible, they sometimes lead to more efficiency (if the arrays are large). The second example uses normal D code, that's much more flexible. Bye, bearophile
Re: A use case for fromStringz
Yeah I basically took the idea from the existing D implementation. Although D's arrays are a struct with a length and a pointer (I think so).
accessing your scope for template metaprogramming ?
Is there any way to access the your current scope? For instance the following pseudocode is what I am after more or less: class Foo { alias byte function(int) BarCompatibleInterface; byte Bar(int) { static assert(is(typeof(localscope) == function)); static assert(is(std.traits.ReturnType!(localscope) == std.traits.ReturnType!(BarCompatibleInterface)); static assert(is(std.traits.ParameterTypeTuple!(localscope) == std.traits.ParameterTypeTuple!(BarCompatibleInterface)); static assert(is(typeof(localscope.outer) == class)); static assert(is(typeof(localscope.outer.outer) == module)); } } My desire for this is to help the compiler generate better error messages from compilation of string mixins, as they could access the function and/or class they are in and give a better error message from within a static assert() construct.
Re: accessing your scope for template metaprogramming ?
Sean Cavanaugh: Is there any way to access the your current scope? For instance the following pseudocode is what I am after more or less: class Foo { alias byte function(int) BarCompatibleInterface; byte Bar(int) { static assert(is(typeof(localscope) == function)); You have just invented another purpose for something like a _function_, that refers to the current function that I have originally desired to solve this different problem. See my Comments 1 and 2 here: http://d.puremagic.com/issues/show_bug.cgi?id=5140 Bye, bearophile