Re: struct static initializer method apply to UDA
On Thursday, 6 December 2018 at 12:50:34 UTC, Radu wrote: On Thursday, 6 December 2018 at 11:09:47 UTC, Basile B. wrote: Which would be a real nice feature to have. this is what I need, I guess I has to wait.
struct static initializer method apply to UDA
my question is how to easy use struct static initializer method with UDA. Fake code: struct DbColumn { string name; boolunique ; boolsigned ; boolnullable ; } struct Order { uint id; @DbColumn({ .nullable= true}) // not working string order_id; @DbColumn({ :nullable= true}) // not working string order_time; @DbColumn(nullable= true) // not working string order_time; }
is function pointer least significant bit always zero ?
I plan to use function pointer least significant bit to store some information. If there is no GC on my system, I think it will help the memory is well aligned. The question is all the function least significant bit is zero ?
Re: betterC generate dynamic array throw Error: TypeInfo cannot be used with -betterC
On Wednesday, 17 October 2018 at 16:50:36 UTC, Paul Backus wrote: You can't append to an array in betterC code, because making space for the new elements requires allocating memory, and that uses the GC. In theory, since you're only using the GC during CTFE, it shouldn't be a problem, but the D compiler doesn't *know* that's what you're doing, so it has to assume the function could be called at runtime--which would be an error. The test2 is not build as source code, like you can use std.* in butterC ctfe function even it use GC. for example you can use this function in betterC if it not build as source code(include from include path): string add_prefix(string exp) { string reg = "^" ~ exp; return reg ; } the dynamic array append working for basic type but not for struct. so I guess this is a dmd front bug.
Re: try find the fastest way to convert a group string into index?
On Sunday, 16 September 2018 at 11:51:24 UTC, learnfirst1 wrote: On Sunday, 16 September 2018 at 11:30:11 UTC, learnfirst1 wrote: On Sunday, 16 September 2018 at 10:14:24 UTC, Vladimir Panteleev wrote: I will test pcre solution vs mpfc for benchmark. the pcre is easy to deal with low/up case. The PCRE: /^(?:Accept(*:0)|Accept\-Charset(*:1)|Accept\-Encoding(*:2)|Accept\-Language(*:3)|Access\-Control\-Allow\-Credentials(*:4)|Access\-Control\-Allow\-Origin(*:5)|Access\-Control\-Allow\-Methods(*:6)|Access\-Control\-Allow\-Headers(*:7)|Access\-Control\-Max\-Age(*:8)|Access\-Control\-Expose\-Headers(*:9)|Access\-Control\-Request\-Method(*:10)|Access\-Control\-Request\-Headers(*:11)|Age(*:12)|Allow(*:13)|Authorization(*:14)|Cache\-Control(*:15)|Connection(*:16)|Content\-Encoding(*:17)|Content\-Language(*:18)|Content\-Length(*:19)|Content\-Location(*:20)|Content\-Range(*:21)|Content\-Security\-Policy(*:22)|Content\-Type(*:23)|Cookie(*:24)|Date(*:25)|ETag(*:26)|Expect(*:27)|Expires(*:28)|Host(*:29)|If\-Match(*:30)|If\-Modified\-Since(*:31)|If\-None\-Match(*:32)|If\-Range(*:33)|If\-Unmodified\-Since(*:34)|Last\-Modified(*:35)|Location(*:36)|Origin(*:37)|Pragma(*:38)|Proxy\-Authenticate(*:39)|Proxy\-Authorization(*:40)|Range(*:41)|Referer(*:42)|Retry\-After(*:43)|Sec\-Websocket\-Exte! nsions(*:44)|Sec\-Websocket\-Key(*:45)|Sec\-Websocket\-Origin(*:46)|Sec\-Websocket\-Protocol(*:47)|Sec\-Websocket\-Version(*:48)|Server(*:49)|Set\-Cookie(*:50)|Strict\-Transport\-Security(*:51)|Transfer\-Encoding(*:52)|Upgrade(*:53)|User\-Agent(*:54)|Vary(*:55)|Via(*:56)|WWW\-Authenticate(*:57))$/i
Re: try find the fastest way to convert a group string into index?
On Sunday, 16 September 2018 at 11:30:11 UTC, learnfirst1 wrote: On Sunday, 16 September 2018 at 10:14:24 UTC, Vladimir Panteleev wrote: It has to be case Case Insensitive, so before I run mpfh for each new request headers, I need to convert the keys into low or up case. this value can be storage on a stack local tmp cache. and I has to run it on the get(string key) method again.
Re: try find the fastest way to convert a group string into index?
On Sunday, 16 September 2018 at 10:14:24 UTC, Vladimir Panteleev wrote: On Sunday, 16 September 2018 at 10:04:09 UTC, learnfirst1 wrote: how to make this more fast like with one loop and get the results. thanks for reply, minimal perfect hashing seems good for this task, I will do more test for confirm. I try to storage the value into a static array, so when diff middleware try access the key it will reused the array offset without need to run hash on key string. (all middleware can access the key by static enum name). with Perfect Hash Function, I can translate the hash number into static enum and use them. The code also need provide string get(string key) method, in this case the header key may not on the static preset arrays, in this case it should search a smaller array (save on the parse process). with this solution I has to run hash for each of request headers , with less than 127 items (maybe around 16 at max) I am not sure it is fast then direct string compare. for example: foreach(int header_index, ref header; new_request.headers) { int hash = mpfh(header.key); auto enum_index = static_eum[ hash ]; new_request.header[enum_index] = header_index ; } the simple static switch is not the best way as I know here. with compiled PCRE match it will "Context-Type", "Context-Encoding" in once ( and maybe there is some SSE4 to pass 16 byte for each loop). Diederik de Groot please ignore the Header.data assign part. which just for the demo. in the real app it will only parse once and storage in pre -alloc request/response object.
try find the fastest way to convert a group string into index?
The use case is translate http header key into enum. this is the current code : https://run.dlang.io/is/lpw29w In this fake code I only list a few headers, it should be more. but less then 128 and only include the a few codes. how to make this more fast like with one loop and get the results. I am think of use PCRE compile for this, but not sure how. It should also save the non default headers in to a new array, so when search non default headers will reduce the total size. I am plan to write a more fast http server with D betterC compare to vibed.
is this a betterC bug ?
enum string[] a = ["a"]; extern(C) void main() { int i = 0; auto s = a[i]; } --- Error: TypeInfo cannot be used with -betterC
Re: why mixin template can not inclulde statement;
On Friday, 10 August 2018 at 13:17:13 UTC, learnfirst1 wrote: this work, it report no error but give a link problem. (this could be a bug ?) mixin template test(A...){ __gshared int a = A[0]; pragma(inline, true) // remove this will work static extern(C) int test(){ a++; return 0; } int dummy = test(); } import core.stdc.stdio; extern(C) void main(){ mixin test!1; printf("a=%d\n", a); } --- Undefined symbols for architecture x86_64: "__D4test4mainUZ8__mixin1QvUNbNiZi", referenced from: _main in test.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) Error: linker exited with status 1
Re: why mixin template can not inclulde statement;
On Friday, 10 August 2018 at 13:10:57 UTC, Kagamin wrote: On Friday, 10 August 2018 at 13:01:21 UTC, learnfirst1 wrote: Looks like some problem with tuple, try __gshared a = A[0]; this work, it report no error but give a link problem. (this could be a bug ?)
Re: why mixin template can not inclulde statement;
On Friday, 10 August 2018 at 13:05:24 UTC, Kagamin wrote: Mixim template can only introduce declarations, not statements, a workaround is a lambda called in place. mixin template test(A...){ __gshared a = A; int dummy = (){ a++; return 0; }(); } extern(C) void main(){ mixin test!123; } Thanks, this work for me. my second example should be a dmd bug ? (ldc work)
Re: why mixin template can not inclulde statement;
On Friday, 10 August 2018 at 12:38:55 UTC, learnfirst1 wrote: mixin template test(A...){ mixin template test(A...){ __gshared a = A; } extern(C) void main(){ mixin test!123; } --- duplicate symbol __D4test4mainUZ8__mixin111__a_field_0i in: test.o ld: 1 duplicate symbol for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) Error: linker exited with status 1 this really make no sense, what is wrong with it?(same code build with ldc2)
why mixin template can not inclulde statement;
mixin template test(A...){ __gshared a = A; a++; } extern(C) void main(){ mixin test!123; } - I dont want to use string mixin to intro a lot un want symbol, try with mixin template find this not work. I know if mixin test on global it should not working, but why not make the limit for function scope ? I try use D for a WASM project then find so much limitation with no good reason. or I missing some thing here ?
Re: is this a bug ? mixin template static function missing!
On Friday, 10 August 2018 at 12:05:52 UTC, Simen Kjærås wrote: On Friday, 10 August 2018 at 11:17:10 UTC, learnfirst1 wrote: If you try the same without the mixin template, you'll see that it doesn't work: struct Test { extern(C) pragma(crt_constructor) static void init(){ // work int i = 3; } } void main(){ extern(C) pragma(crt_constructor) static void init(){ // not work int i = 3; } } -- It not work make no sense, since it can work on struct. I am not be able to search the related spec docs, only this link: https://dlang.org/blog/2018/01/04/dmd-2-078-0-has-been-released/ Based on my understand, nested static extern(C) function is all about visibility. It just like put private before it, there is really no reason to treat them in diff way.
Re: is this a bug ? mixin template static function missing!
On Friday, 10 August 2018 at 10:24:55 UTC, Simen Kjærås wrote: On Friday, 10 August 2018 at 08:31:21 UTC, learnfirst1 wrote: Filed a bug: https://issues.dlang.org/show_bug.cgi?id=19153 template G(){ pragma(crt_constructor) static extern(C) void init(){} } void main(){ mixin G!(); // Line 5 init(); } same missing symbols.
Re: is this a bug ? mixin template static function missing!
On Friday, 10 August 2018 at 10:24:55 UTC, Simen Kjærås wrote: On Friday, 10 August 2018 at 08:31:21 UTC, learnfirst1 wrote: The correct behavior would be for the compiler to show the latter error message for a mixin'd function as well. Filed a bug: https://issues.dlang.org/show_bug.cgi?id=19153 -- Simen I think the static extern(C) nested function should just work like global extern(C) function. DMD still report missing symbols. Or I am wrong about this ? template G(){ static extern(C) pragma(crt_constructor) void init(){} } void main(){ mixin G!(); // Line 5 init(); }
Re: templated lambda with {} cause GC
On Friday, 10 August 2018 at 10:38:53 UTC, Simen Kjærås wrote: What you should do instead is: T!((t){ printf("test 2 name = %s\n".ptr, t.name.ptr); }, "test"); (note the lack of the => arrow) -- Simen rikki cattermole , Paul Backus, Simen Kjærås: thanks for the explain, it work. Still, if my first example is use GC, why dmd not throw error at compile time, instead at link time report symbols is missing. Is this a bug ?
templated lambda with {} cause GC
import core.stdc.stdio; struct Test { string name ; } void T(alias pred, A...)(){ __gshared t = Test(A) ; pred(t); } extern(C) void main(){ T!(t => printf("test 1 name = %s\n".ptr, t.name.ptr), "test") ; // build OK T!(t => { printf("test 2 name = %s\n".ptr, t.name.ptr); }, "test") ; // build error } -- build this with betterC Undefined symbols for architecture x86_64: "__d_allocmemory", referenced from: __D4test4mainUZ__T9__lambda2TSQBb4TestZQvFNaNbNfQtZDFNbNiZv in test.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) Error: linker exited with status 1 to use without {}, it work as expect. Is there a way to avoid this GC with {}, because we need multi line here.
is this a bug ? mixin template static function missing!
#!/usr/bin/env rdmd import core.stdc.stdio; template G(size_t line = __LINE__, A...){ int i = 3; static extern(C) pragma(crt_constructor) void init2(){ printf("init: %d\n", line); } } pragma(crt_constructor) extern(C) void init1(){ printf("init from global\n"); } struct A { mixin G!(); } extern(C) void main(){ mixin G!() g; printf("g.i=%d\n", g.i); g.init2(); // remove this can build, but g.init2 not get called! } - build error: Undefined symbols for architecture x86_64: "__D4test4mainUZ1g5init2UNbNiZv", referenced from:
Re: variable _param_0 cannot be read at compile time
On Thursday, 9 August 2018 at 13:42:03 UTC, Kagamin wrote: struct M { int i; S*[100] s; } struct S { M* mp; bool x; } S* add(A...)() { alias m = A[0]; __gshared s = S(,A[1..$]); m.s[m.i++] = return } void main(){ __gshared M m = M(0); __gshared S s = S(, false); m.s[m.i++] = auto p = add!(m, true); } this is what I want! thanks.
Re: variable _param_0 cannot be read at compile time
On Wednesday, 8 August 2018 at 13:13:42 UTC, Simen Kjærås wrote: On Wednesday, 8 August 2018 at 12:57:43 UTC, learnfirst1 wrote: Why this is a error ? ``` struct S { bool v; string x; } S* add(A...)(ref A a) { __gshared s = S(a); return } void main(){ auto p = add(true); } ``` test.d(9): Error: variable _param_0 cannot be read at compile time __gshared and static need to be initialized with a value known at compile-time. You're trying to give it a run-time value. You can set this after it's first created: S* add(A...)(ref A a) { __gshared S s; s = S(a); return } That's a little kludgy, but apparently that's how it be. You also get another error message: Error: function test.add!bool.add(ref bool _param_0) is not callable using argument types (bool) That's because add takes its arguments by ref, and true is a literal that has no canonical address, and thus can't be passed by ref. You might consider using auto ref. -- Simen is there a way to pass A a... as alias into template ? so the code can be just like this: __gshared S s = S(, false); the binary size is short since in this case the s is static build int. and run a bit fast.
Re: variable _param_0 cannot be read at compile time
On Wednesday, 8 August 2018 at 12:57:43 UTC, learnfirst1 wrote: Why this is a error ? this code example can explain what I am try to do here: struct M { int i; S*[100] s; } struct S { M* mp; bool x; } S* add(A...)(ref A a) { __gshared s = S(a); alias m = a[0]; m.s[m.i++] = return } void main(){ __gshared M m = M(0); __gshared S s = S(, false); m.s[m.i++] = auto p = add(, true); // variable _param_0 cannot be read at compile time } because S has the optional ctor parameters, I can not use template alias param here( some how not work) .
variable _param_0 cannot be read at compile time
Why this is a error ? ``` struct S { bool v; string x; } S* add(A...)(ref A a) { __gshared s = S(a); return } void main(){ auto p = add(true); } ``` test.d(9): Error: variable _param_0 cannot be read at compile time test.d(14): Error: template instance `test.add!bool` error instantiating test.d(14): Error: function test.add!bool.add(ref bool _param_0) is not callable using argument types (bool) test.d(14):cannot pass rvalue argument true of type bool to parameter ref bool _param_0 I try pass some into the template to return a __gshared var pointer. the A length is dynamic.