Re: write a function template specialisation that tests if an argument is known at compile time
On Saturday, 11 August 2018 at 05:17:51 UTC, Cecil Ward wrote: T myfunc(T)( T x, uint mask ) if ( mask == 3 ) { return fast_func( x, mask ); } but of course this doesn't work because mask is not known at compile-time. Actually is there an opportunity for some kind of language enhancement there? I do not really know what I am talking about AT ALL but if the compiler could silently add an extra specialisation that gets generated at compile time, with constant folding and all the optimisations that follow from it, if a call with an appropriate constant argument is seen? But this is probably horrible because that kind of stuff is ph performed at a completely different point ?
write a function template specialisation that tests if an argument is known at compile time
T myfunc(T)( T x, uint mask ) if ( mask == 3 ) { return fast_func( x, mask ); } but of course this doesn't work because mask is not known at compile-time. so I wondered if there is a way to do something like static if ( isKnownAtCompileTime( mask ) ) but that would not necessarily help me and probably isn't the right way. Basically there is a fast path for certain known values of a (second in this case) argument where the compiler could produce superb trivial code or where I can work out a shortcut myself. for example myfunc( x, 0 ) == 0 and myfunc( x, -1 ) == x and various other good things, and for some values of mask the thing behaves like an AND operation so I want the compiler to just generate that. The default slow path where the arg is unknown involves calling asm so the compiler cannot use its intelligence as it does not know the detailed semantics. Also: To add further complication: if both arguments of myfunc() are known at compile-time, then I definitely want to take an alternative path because then I can apply CTFE and calculate a compile-time result.
Re: vibe.d: Finding out if currently in webinterface request
On Friday, 10 August 2018 at 18:23:28 UTC, Johannes Loher wrote: On Friday, 10 August 2018 at 09:33:34 UTC, Timoses wrote: On Thursday, 9 August 2018 at 21:59:24 UTC, Johannes Loher wrote: I already posted this in the vibe.d forums (https://forum.rejectedsoftware.com/groups/rejectedsoftware.vibed/thread/58891/), but it seems, there is not a lot of activity over there, so I am cross posting this here: [...] Do you have some code segments boiled down to the problem? Are you using vibe.core.log? Yes, I am using vibe.core.log, but as mentioned, I want to implement my own logger. The gist of what I am trying to do is this: https://run.dlang.io/is/7qpJ6J This actually works the way it is, but it involves catch Throwable (actually AssertError would be enough) and this is bad. I would like to find a different solution. You hit the assert of getRequestContext here: https://github.com/vibe-d/vibe.d/blob/a9589d955f10bd076a67d47ace0c78cfd3aa8246/web/vibe/web/web.d#L871 However, I don't know of a way yet to check whether one is currently in a request context from outside of vibe.web.web (adding a method to do so to vibe.web.web which checks `s_requestContext.req !is null` would probably help.)
Re: vibe.d: Finding out if currently in webinterface request
On Friday, 10 August 2018 at 09:33:34 UTC, Timoses wrote: On Thursday, 9 August 2018 at 21:59:24 UTC, Johannes Loher wrote: I already posted this in the vibe.d forums (https://forum.rejectedsoftware.com/groups/rejectedsoftware.vibed/thread/58891/), but it seems, there is not a lot of activity over there, so I am cross posting this here: [...] Do you have some code segments boiled down to the problem? Are you using vibe.core.log? Yes, I am using vibe.core.log, but as mentioned, I want to implement my own logger. The gist of what I am trying to do is this: https://run.dlang.io/is/7qpJ6J This actually works the way it is, but it involves catch Throwable (actually AssertError would be enough) and this is bad. I would like to find a different solution.
Re: dmd64 on Windows: how?
On Friday, 10 August 2018 at 12:15:55 UTC, Ivan Kazmenko wrote: Hi, How should I set up DMD to be able to `dmd -m64` on Windows nowadays? I usually download the 7z, but it broke when I replaced my Visual Studio with 2017 edition. Now, I tried the current 2.081.1 .exe installer. It didn't propose any additional 64-bit related options. After the installation, `dmd -m64` complains that the linker could not find `libcmt.lib`. The previous time I tried finding the right `libcmt` and treating the subsequent errors, I failed to locate all the correct libraries in Microsoft's Visual Studio and SDKs. This time, I'd rather follow some up-to-date guide than waste the time again. So, what's the most current guide to make 64-bit linking work on Windows? I'm fine with having to install more LLVM or MinGW or Microsoft stuff, I just don't seem to know what I need. Ivan Kazmenko. Did you have a look at the wiki ? It looks like the image shows what needs to be installed: https://wiki.dlang.org/Installing_DMD#Installing_the_Microsoft_toolchain
Re: dmd64 on Windows: how?
The dmd installer should normally deal with it, maybe it doesn't support VS2017 yet or you didn't install VC++. Try to uninstall dmd, delete its configuration files and install again.
Re: dmd64 on Windows: how?
On Friday, 10 August 2018 at 12:15:55 UTC, Ivan Kazmenko wrote: Hi, How should I set up DMD to be able to `dmd -m64` on Windows nowadays? I usually download the 7z, but it broke when I replaced my Visual Studio with 2017 edition. If you were using another Visual Studio version, since VS 2017 operates differently, in a more modular fashion, you'll have to use the Visual Studio 2017 installer to modify your installation and add the correct Windows SDKs. I'm not on my machine and haven't done this in quite some time, so I don't remember which package exactly is needed... But if you have already done this however, then I don't know what the problem could be.
Re: why mixin template can not inclulde statement;
try this: mixin template test(A...){ __gshared int a = A[0]; int dummy = (){ a++; return 0; }(); } import core.stdc.stdio; int main(){ mixin test!1; printf("a=%d\n", a); return 0; }
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 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; } Except that you can't use tuples for that, a working sample: mixin template test(alias A){ __gshared a = A; int dummy = (){ a++; return 0; }(); } extern(C) void main(){ mixin test!123; import core.stdc.stdio; printf("%d", a); }
Re: why mixin template can not inclulde statement;
On Friday, 10 August 2018 at 13:01:21 UTC, learnfirst1 wrote: 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) Looks like some problem with tuple, try __gshared a = A[0];
Re: why mixin template can not inclulde statement;
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; }
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)
Re: Dlang on OpenWrt
On Thursday, 2 August 2018 at 10:47:33 UTC, Ky-Anh Huynh wrote: Hi, can I build my program on OpenWrt? I haven't found any resources on internet maybe I'm missing something. Thanks a lot. A status report on what works and how to get LDC to compile for OpenWRT uClibc ARM: https://github.com/ldc-developers/ldc/issues/2810
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.
dmd64 on Windows: how?
Hi, How should I set up DMD to be able to `dmd -m64` on Windows nowadays? I usually download the 7z, but it broke when I replaced my Visual Studio with 2017 edition. Now, I tried the current 2.081.1 .exe installer. It didn't propose any additional 64-bit related options. After the installation, `dmd -m64` complains that the linker could not find `libcmt.lib`. The previous time I tried finding the right `libcmt` and treating the subsequent errors, I failed to locate all the correct libraries in Microsoft's Visual Studio and SDKs. This time, I'd rather follow some up-to-date guide than waste the time again. So, what's the most current guide to make 64-bit linking work on Windows? I'm fine with having to install more LLVM or MinGW or Microsoft stuff, I just don't seem to know what I need. Ivan Kazmenko.
Re: is this a bug ? mixin template static function missing!
On Friday, 10 August 2018 at 11:17:10 UTC, learnfirst1 wrote: 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(); } If you try the same without the mixin template, you'll see that it doesn't work: void main() { static extern(C) pragma(crt_constructor) void init(); init(); } Depending on the order of static, extern(C) and pragma(crt_constructor), you can get at least two different error messages, but either way - it doesn't work. It would be possible to make this work by changing the compiler, but according to spec, it shouldn't. -- Simen
Re: templated lambda with {} cause GC
On Friday, 10 August 2018 at 11:10:55 UTC, learnfirst1 wrote: 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 ? If you make your main function @nogc, you will get a compile-time error.
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 ?
Re: How do you put log calls in constructors when they may be created in a static context?
On Friday, 10 August 2018 at 09:29:04 UTC, Timoses wrote: On Wednesday, 8 August 2018 at 21:54:34 UTC, aliak wrote: I'm trying to debug stuff, so I want to add verbose logging struct S(T) { this() { writeln("created S(T) with properties and ID"); } } static a = S!int(); // bah I guess users can call this code from any context, but when i'd also like to see the log output for debugging purposes. Is there a way around this? Can I maybe only do a writeln in a non compile-time context? Cheers, - ali How about debug [1]? struct S()T { this() { debug { writeln("Created..."); } } } and compile with the debug flag. You can also specify a debug identifier or level. [1]: https://dlang.org/spec/version.html#debug Very nice :D Thanks!
Re: templated lambda with {} cause GC
On Friday, 10 August 2018 at 09:57:53 UTC, learnfirst1 wrote: T!(t => { printf("test 2 name = %s\n".ptr, t.name.ptr); }, "test") ; // build error This is not doing what you think it's doing. The syntax t => { return t; } is equivalent to t => () => t. That is, it's returning a function that takes no arguments, not a value. For that very same reason, if you compile and run your code without -betterC, only the first printf() will be executed, and only one line of output will be generated. 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
Re: is this a bug ? mixin template static function missing!
On Friday, 10 August 2018 at 08:31:21 UTC, learnfirst1 wrote: #!/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: It is indeed. Reduced example: template G(){ extern(C) pragma(crt_constructor) void init(){} } void main(){ mixin G!(); init(); } For nested functions, extern(C) is simply ignored[0]. Since pragma(crt_constructor) requires that the symbol it's applied to uses C linkage (and an error message to that effect is shown if you write pragma(crt_constructor) void fun() {} in module scope: Error: function `fun` must be extern(C) for pragma(crt_constructor) In addition, if you try to apply pragma(crt_constructor) to a nested function, you get this error message, showing further that nested functions can't be crt_constructors: Error: unrecognized pragma(crt_constructor) 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 [0]: from https://dlang.org/spec/attribute.html#linkage: Note that extern(C) can be provided for all types of declarations, including struct or class, even though there is no corresponding match on the C side. In that case, the attribute is ignored. This behavior applies for nested functions and nested variables as well.
Re: templated lambda with {} cause GC
On Friday, 10 August 2018 at 09:57:53 UTC, learnfirst1 wrote: 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 } -- The arrow syntax doesn't work for multi-line lambdas; you need to write them using what the spec calls "function literal" syntax [1]: T!((t) { // <- Parens around the argument, no arrow printf("test 2 name = %s\n".ptr, t.name.ptr); }, "test") ; [1] https://dlang.org/spec/expression.html#function_literals
Re: templated lambda with {} cause GC
On 10/08/2018 9:57 PM, learnfirst1 wrote: 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. Without the brackets it is inferring to not have state and hence is a function. But with the brackets it is assuming there is state required and hence has to allocate. Without manually allocating said state (not what you want), you cannot do this.
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.
Re: vibe.d: Finding out if currently in webinterface request
On Thursday, 9 August 2018 at 21:59:24 UTC, Johannes Loher wrote: I already posted this in the vibe.d forums (https://forum.rejectedsoftware.com/groups/rejectedsoftware.vibed/thread/58891/), but it seems, there is not a lot of activity over there, so I am cross posting this here: [...] Do you have some code segments boiled down to the problem? Are you using vibe.core.log?
Re: How do you put log calls in constructors when they may be created in a static context?
On Wednesday, 8 August 2018 at 21:54:34 UTC, aliak wrote: I'm trying to debug stuff, so I want to add verbose logging struct S(T) { this() { writeln("created S(T) with properties and ID"); } } static a = S!int(); // bah I guess users can call this code from any context, but when i'd also like to see the log output for debugging purposes. Is there a way around this? Can I maybe only do a writeln in a non compile-time context? Cheers, - ali How about debug [1]? struct S()T { this() { debug { writeln("Created..."); } } } and compile with the debug flag. You can also specify a debug identifier or level. [1]: https://dlang.org/spec/version.html#debug
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: