How to use the result of __traits( allMembers , T ) with string mixins ?
DMD tells me "Error: variable m cannot be read at compile time", but why ? [code] struct MyStruct { float float_value = 0.0f ; ubyte ubyte_value = 2 ; } enum members = [ __traits( allMembers , MyStruct ) ] ; foreach( m ; members ) { mixin( "writeln( " ~ m ~ " , \" : \" , ( MyStruct." ~ m ~ ".offsetof ) ;" ) ; } [\code] I also tried ref m and foreach( i ; 0..members.length ) with m[i]. A simple writeln( m or m[i] ) always worked. I read the limitation of "String Mixins and Compile Time Function Execution" here: http://dlang.org/function.html#interpretation But it doesn't make sense to me as members are enum values and known at compile time. What am I doing wrong, and how could it be done ? Regards, ParticlePeter
Re: How to use the result of __traits( allMembers , T ) with string mixins ?
On Monday, 28 April 2014 at 13:57:56 UTC, Andrej Mitrovic wrote: On Monday, 28 April 2014 at 13:52:52 UTC, ParticlePeter wrote: DMD tells me "Error: variable m cannot be read at compile time", but why ? Because 'static foreach' is not an explicit feature yet, so it depends on the context. When you wrap the trait via: [__traits(allMembers, MyStruct)] You're creating an array, and foreach will *not* by default attempt to become a static foreach, even if the array is known at compile-time. If you remove the parentheses it will work. You've had a few bugs in the mixin code though, anyway here's the working sample: - import std.stdio; struct MyStruct { float float_value = 0.0f; ubyte ubyte_value = 2; } enum members = __traits(allMembers, MyStruct); void main() { foreach (m; members) { mixin("writeln( `" ~ m ~ "` , \" : \" , ( MyStruct." ~ m ~ ".offsetof ) );"); } } - Thank you very much, it works. I never came so far to see those mixin errors at all :-) I found the code with parenthesis in the dlang __traits docs and also Philippe Sigauds "D Templates", and I haven't seen any other example which works without them. So, when to use which syntax ( for which purpose ) ? Is this clarified somewhere ? Regards, ParticlePeter
How you guys go about -BetterC Multithreading?
Any experience reports or general suggestions? I've used only D threads so far.
Re: How you guys go about -BetterC Multithreading?
On Thursday, 9 November 2017 at 12:43:54 UTC, Petar Kirov [ZombineDev] wrote: On Thursday, 9 November 2017 at 12:30:49 UTC, rikki cattermole wrote: On 09/11/2017 12:19 PM, Petar Kirov [ZombineDev] wrote: On Thursday, 9 November 2017 at 11:08:21 UTC, ParticlePeter wrote: Any experience reports or general suggestions? I've used only D threads so far. It would be far easier if you use druntime + @nogc and/or de-register latency-sensitive threads from druntime [1], so they're not interrupted even if some other thread calls the GC. Probably the path of least resistance is to call [2] and queue @nogc tasks on [3]. If you really want to pursue the version(D_BetterC) route, then you're essentially on your own to use the threading facilities provided by your target OS, e.g.: https://linux.die.net/man/3/pthread_create https://msdn.microsoft.com/en-us/library/windows/desktop/ms682516(v=vs.85).aspx You can use a library like libuv to handle threads (non-language based TLS too, not sure that it can be tied in unfortunately). Yeah, any cross-platform thread-pool / event loop library with C interface should obviously be preferred than manual use of raw thread primitives. Essentially, try to follow Sean Parent's advice on "No Raw/Incidental *": https://www.youtube.com/watch?v=zULU6Hhp42w This all is good input, thanks. I was looking into: https://github.com/GerHobbelt/pthread-win32 Anyone used this?
Re: How you guys go about -BetterC Multithreading?
On Thursday, 9 November 2017 at 12:19:00 UTC, Petar Kirov [ZombineDev] wrote: On Thursday, 9 November 2017 at 11:08:21 UTC, ParticlePeter wrote: Any experience reports or general suggestions? I've used only D threads so far. It would be far easier if you use druntime + @nogc and/or de-register latency-sensitive threads from druntime [1], so they're not interrupted even if some other thread calls the GC. Probably the path of least resistance is to call [2] and queue @nogc tasks on [3]. If you really want to pursue the version(D_BetterC) route, then you're essentially on your own to use the threading facilities provided by your target OS, e.g.: https://linux.die.net/man/3/pthread_create https://msdn.microsoft.com/en-us/library/windows/desktop/ms682516(v=vs.85).aspx Though you need to be extra careful not to use thread-local storage (e.g. only shared static and __gshared) and not to rely on (shared) static {con|de}structors, dynamic arrays, associative arrays, exceptions, classes, RAII, etc., which is really not worth it, unless you're writing very low-level code (e.g. OS kernels and drivers). [1]: https://dlang.org/phobos/core_thread#.thread_detachThis [2]: https://dlang.org/phobos/core_memory#.GC.disable [3]: https://dlang.org/phobos/std_parallelism#.taskPool Forgot to mention, I'll try this first, I think its a good first step towards -BetterC usage. But in the end I want to see how far I can get with the -BetterC feature.
Re: How you guys go about -BetterC Multithreading?
On Friday, 10 November 2017 at 11:55:57 UTC, Guillaume Piolat wrote: For now we do have some @nogc alternatives for mutex, condition variables, thread-pool, file reading, etc... (dplug:core package) for use with the runtime disabled - the middle ground that's way more usable than -betterC. They may, or not, be applicable to -betterC. Your thread module (among others) is an amazing read and a very nice starting point for my endeavor. Thanks for pointing me in this direction!
Linker error since upgrade to DMD 2.077.1: fatal error C1905: Front end and back end not compatible
I upgraded from DMD 2.074.1 (!) to 2.077.1 and tried to compile a mixed c++/d project (DMD links to one c++ lib). Here is the full error message: fatal error C1905: Front end and back end not compatible (must target same processor). LINK : fatal error LNK1257: code generation failed Error: linker exited with status 1257 dmd failed with exit code 1257. No such problems with my previous DMD version. What has changed with linking since then? (Skimmed through changelogs but found nothing). I found a workaround with specifying LINKCMD64 to my VS 2017 linker, but this is not a viable solution. The project must build with DMD on any system with VS installed without me knowing its exact location. What can I do to make it run out of the box and without the link command specified?
Re: Linker error since upgrade to DMD 2.077.1: fatal error C1905: Front end and back end not compatible
On Sunday, 17 December 2017 at 15:57:08 UTC, ParticlePeter wrote: I upgraded from DMD 2.074.1 (!) to 2.077.1 and tried to compile a mixed c++/d project (DMD links to one c++ lib). Here is the full error message: Forgot most important info, ita an x64 project those used VS linker by default afaik.
Re: Linker error since upgrade to DMD 2.077.1: fatal error C1905: Front end and back end not compatible
On Sunday, 17 December 2017 at 16:40:46 UTC, John wrote: Yah the sc.ini file is wrong for Environment64. [Environment64] LIB="%@P%\..\lib64" . . . ; Windows installer uncomments the version detected LINKCMD=%VCINSTALLDIR%\bin\HostX86\x86\link.exe Thanks! Is this a known, reported bug?
Re: Linker error since upgrade to DMD 2.077.1: fatal error C1905: Front end and back end not compatible
On Sunday, 17 December 2017 at 17:56:47 UTC, John wrote: I don't think so, all that would need to be changed is this line: https://github.com/dlang/dmd/blob/v2.077.1/ini/windows/bin/sc.ini#L53 Not very many people use it I guess if it's been there for 8 months lol. Hm, actually that line IS uncommented in my installed sc.ini and VCINSTALLDIR is properly detected. Any idea what still might go wrong?
Re: Linker error since upgrade to DMD 2.077.1: fatal error C1905: Front end and back end not compatible
On Sunday, 17 December 2017 at 19:16:02 UTC, ParticlePeter wrote: On Sunday, 17 December 2017 at 17:56:47 UTC, John wrote: I don't think so, all that would need to be changed is this line: https://github.com/dlang/dmd/blob/v2.077.1/ini/windows/bin/sc.ini#L53 Not very many people use it I guess if it's been there for 8 months lol. Hm, actually that line IS uncommented in my installed sc.ini and VCINSTALLDIR is properly detected. Any idea what still might go wrong? Think I figured it out. For me it works when changing that particular line to: LINKCMD=%VCINSTALLDIR%\bin\HostX32\x64\link.exe or LINKCMD=%VCINSTALLDIR%\bin\HostX64\x64\link.exe
Re: Linker error since upgrade to DMD 2.077.1: fatal error C1905: Front end and back end not compatible
On Sunday, 17 December 2017 at 19:29:00 UTC, ParticlePeter wrote: On Sunday, 17 December 2017 at 19:16:02 UTC, ParticlePeter wrote: On Sunday, 17 December 2017 at 17:56:47 UTC, John wrote: I don't think so, all that would need to be changed is this line: https://github.com/dlang/dmd/blob/v2.077.1/ini/windows/bin/sc.ini#L53 Not very many people use it I guess if it's been there for 8 months lol. Hm, actually that line IS uncommented in my installed sc.ini and VCINSTALLDIR is properly detected. Any idea what still might go wrong? Think I figured it out. For me it works when changing that particular line to: LINKCMD=%VCINSTALLDIR%\bin\HostX32\x64\link.exe or LINKCMD=%VCINSTALLDIR%\bin\HostX64\x64\link.exe First one is BS, of course it must be: LINKCMD=%VCINSTALLDIR%\bin\HostX86\x64\link.exe
Re: Linker error since upgrade to DMD 2.077.1: fatal error C1905: Front end and back end not compatible
On Sunday, 17 December 2017 at 20:09:02 UTC, ParticlePeter wrote: On Sunday, 17 December 2017 at 19:29:00 UTC, ParticlePeter wrote: On Sunday, 17 December 2017 at 19:16:02 UTC, ParticlePeter [snip] LINKCMD=%VCINSTALLDIR%\bin\HostX32\x64\link.exe or LINKCMD=%VCINSTALLDIR%\bin\HostX64\x64\link.exe First one is BS, of course it must be: LINKCMD=%VCINSTALLDIR%\bin\HostX86\x64\link.exe Filed a bug: https://issues.dlang.org/show_bug.cgi?id=18098
How to instantiate a template struct with a template constructor without relying on auto deduction?
struct Foo(T) { T bar; this(S)(S s) { bar = convert(s); } } auto foo = Foo!int(some_float); this works because S is deduced as typeof(some_float), but how would I instantiate the struct without relying on auto deduction? Suppose we would have this kind of constructor where auto deduction is not possible: this(int n)(float f) { static foreach( i; 0..n) { do_some_ctfe_magic;} } How to instantiate Foo then?
Re: How to instantiate a template struct with a template constructor without relying on auto deduction?
On Wednesday, 21 February 2018 at 14:29:31 UTC, Simen Kjærås wrote: On Wednesday, 21 February 2018 at 14:11:10 UTC, ParticlePeter wrote: struct Foo(T) { T bar; this(S)(S s) { bar = convert(s); } } auto foo = Foo!int(some_float); this works because S is deduced as typeof(some_float), but how would I instantiate the struct without relying on auto deduction? Suppose we would have this kind of constructor where auto deduction is not possible: this(int n)(float f) { static foreach( i; 0..n) { do_some_ctfe_magic;} } How to instantiate Foo then? No can do. The solution is to use a factory function: Feared the same, thanks. struct Foo(T) { static Foo create(int n)(float f) { Foo result; static foreach( i; 0..n) { do_some_ctfe_magic;} return result; } } -- Simen I will consider this, actually I use something quite close, but my create is not static and does not return anything. It simply initializes the struct after it has been constructed with the default ctor. The templated user ctor would have been nice, though.
mixed in struct constructor is ignored when another (non mixed in) constructor is specified
mixin template Common() { private int m_member; this( int m ) { m_member = m; } } struct Foo { mixin Common; } struct Bar { mixin Common; this( int m, float n ) { m_member = m * n; } } auto foo = Foo(1); // ok auto b_1 = Bar( 1, 2 ); // ok auto b_2 = Bar( 3 ); // Error: constructor main.Bar.this (int m, int n) is not callable using argument types (int) Is this expected behavior?
Re: mixed in struct constructor is ignored when another (non mixed in) constructor is specified
On Monday, 26 February 2018 at 12:47:48 UTC, Jonathan M Davis wrote: On Monday, February 26, 2018 12:30:24 ParticlePeter via Digitalmars-d-learn wrote: mixin template Common() { private int m_member; this( int m ) { m_member = m; } } struct Foo { mixin Common; } struct Bar { mixin Common; this( int m, float n ) { m_member = m * n; } } auto foo = Foo(1); // ok auto b_1 = Bar( 1, 2 ); // ok auto b_2 = Bar( 3 ); // Error: constructor main.Bar.this (int m, int n) is not callable using argument types (int) Is this expected behavior? Yes. Stuff that's mixed in is treated as having a different scope than other mixins or stuff that wasn't mixed in. To quote towards the bottom of here: https://dlang.org/spec/template-mixin.html "Alias declarations can be used to overload together functions declared in different mixins" It gives an example of mixin Foo!() F; mixin Bar!() B; alias func = F.func; alias func = B.func; I don't know if you can do the same thing with constructors or not, though alias this statements don't allow the = syntax. So, maybe that won't conflict, and it will work to do something like alias this = Common.this; If that doesn't work, then it seems like a good enhancement request. - Jonathan M Davis Thanks for clarification, unfortunately your suggestion doesn't work. Since when is alias this = something; supposed to work? alias Common.this this; doesn't work as well and following also not: struct Baz { Foo foo; alias foo this; this( int m, int n ) { m_member = m * n; } } auto baz = Baz(1); // Error, same as above Not sure if I do require an enhancement, I just stumbled on that and was wondering.
Re: mixed in struct constructor is ignored when another (non mixed in) constructor is specified
On Monday, 26 February 2018 at 14:02:56 UTC, Adam D. Ruppe wrote: On Monday, 26 February 2018 at 12:30:24 UTC, ParticlePeter wrote: Is this expected behavior? yes sort of, but there are bugs associated with it too... I wrote about this in the "Tip of the Week" section here before http://arsdnet.net/this-week-in-d/2016-feb-07.html there's a workaround there and a bit more explanation of weirdness. This cool, I didn't know that we can name mixins when instantiating but also never taught that there could be any purpose for naming. Works, thanks.
Re: mixed in struct constructor is ignored when another (non mixed in) constructor is specified
On Monday, 26 February 2018 at 14:42:58 UTC, Adam D. Ruppe wrote: On Monday, 26 February 2018 at 14:38:22 UTC, ParticlePeter wrote: This cool, I didn't know that we can name mixins when instantiating but also never taught that there could be any purpose for naming. Works, thanks. oh yes, there's a lot of cool things with mixin. You might want to skim my tip of the week index: http://arsdnet.net/this-week-in-d/totw-index.html and see if more jump out at you. template mixins have behavior that look stupid until you understand why - then it gets pretty nice. Like the naming and overloading rules allow selective overriding and integration once you know how. Ok and that is an awesome link as well, I wished having this on several occasions. By the way, I also remembered reading your tip already once ... yeah, while reading it again. Anyway, thanks.
Re: Game Development Using D
On Saturday, 21 May 2016 at 16:01:26 UTC, Stefan Koch wrote: On Saturday, 21 May 2016 at 15:53:18 UTC, David wrote: Hi, I want to try to create a game using D. I'm a complete newbie though (other than having C/C++ experience). Where would I start? Does D have an openGL binding? I am assuming I'll need to leverage a good amount C APIs? Any list of these that would be useful it a game setting? Obviously this all depends on *how* much work I want to do. Ideally, I'd like a collection of tools that will get me roughly the equivalent of what XNA provides. The is derilict-gl and then there is the dgame library Check out the DerelictOrg bindings in general: https://github.com/DerelictOrg In particular DerelictAssimp3 might help with animation and scene loading. Other related game libraries are in the dub registry: https://code.dlang.org/search?q=game
Introspect alias name of an aliased type
alias uint32_t = uint; struct Offset() { uint32_t x; uint32_t y; } // Introspect with: void printStructInfo( T )( T info ) { import std.stdio : writefln; foreach (memb; __traits(allMembers, T)) { writefln(typeof(__traits(getMember, info, memb)).stringof); } } // Result is uint Is there a way to get the alias uint32_t instead ?
Why does std.container.array does not work with foraech( i, a; array ) {} ?
Which of the op(Index) operators is responsible for enabling this kind of syntax? Would it be possible to get it work with UFCS or would I have to wrap the array?
Re: Why does std.container.array does not work with foraech( i, a; array ) {} ?
On Sunday, 29 May 2016 at 09:07:07 UTC, Jonathan M Davis wrote: On Sunday, May 29, 2016 07:14:12 ParticlePeter via Digitalmars-d-learn wrote: Which of the op(Index) operators is responsible for enabling this kind of syntax? Would it be possible to get it work with UFCS or would I have to wrap the array? std.container.array.Array works with foreach via ranges. foreach(e; myContainer) { } gets lowered to foreach(e; myContainer[]) { } which in turn gets lowered to something like for(auto r = myContainer[]; !r.empty; r.popFront()) { auto e = r.front; } Ranges do not support indices with foreach, and that's why you're not able to get the index with foreach and Array. However, if you use std.range.lockstep, you can wrap a range to get indices with foreach. e.g. foreach(i, e; lockstep(myContainer[])) { } http://dlang.org/phobos/std_range.html#.lockstep - Jonathan M Davis Thanks, due to your answer I found a way which is even better for me. I pimped the Array containers with some UFCS functions anyway, one of them returns the array data as a slice and this works nicely with that foreach variant as well auto data( T )( Array!T array ) { if( array.length == 0 ) return null; return (&array.front())[ 0..array.length ]; } // this works now foreach( i, a; someArrayContainer.data ) { ... } - PP
Emulate C's (polymorphic) NULL type
In C NULL can be used as integer as well as null pointer. Is there a way to create such a type in D? The type should have only one value which is obviously (0/null). A extern( C ) function should be able to take it as either one. Overloaded enum pops into my mind as example: enum NULL = 0; enum NULL = null; Is this possible somehow?
Re: Emulate C's (polymorphic) NULL type
On Monday, 6 June 2016 at 11:40:11 UTC, Anonymouse wrote: On Monday, 6 June 2016 at 09:43:23 UTC, ParticlePeter wrote: In C NULL can be used as integer as well as null pointer. Is there a way to create such a type in D? The type should have only one value which is obviously (0/null). A extern( C ) function should be able to take it as either one. Overloaded enum pops into my mind as example: enum NULL = 0; enum NULL = null; Is this possible somehow? If you want it for use in logical expressions then implicit boolean conversion will treat them as the same. https://dpaste.dzfl.pl/d82f60657c37 I don't see the connection here, you introduced two symbols with two different types. I want one symbol which can pose as two different (constant) types.
Re: Emulate C's (polymorphic) NULL type
On Monday, 6 June 2016 at 16:19:02 UTC, Alex Parrill wrote: On Monday, 6 June 2016 at 09:43:23 UTC, ParticlePeter wrote: In C NULL can be used as integer as well as null pointer. Is there a way to create such a type in D? The type should have only one value which is obviously (0/null). A extern( C ) function should be able to take it as either one. Overloaded enum pops into my mind as example: enum NULL = 0; enum NULL = null; Is this possible somehow? I already asked about this: https://forum.dlang.org/post/bnkqevhyxwdjjxsct...@forum.dlang.org Tldr; doesn't seem to be possible without multiple alias this or using ABI hacks. O.k., my web search didn't find that topic. The last reply looks promising, wouldn't that work?
Re: Emulate C's (polymorphic) NULL type
On Monday, 6 June 2016 at 18:33:36 UTC, ParticlePeter wrote: On Monday, 6 June 2016 at 16:19:02 UTC, Alex Parrill wrote: On Monday, 6 June 2016 at 09:43:23 UTC, ParticlePeter wrote: In C NULL can be used as integer as well as null pointer. Is there a way to create such a type in D? The type should have only one value which is obviously (0/null). A extern( C ) function should be able to take it as either one. Overloaded enum pops into my mind as example: enum NULL = 0; enum NULL = null; Is this possible somehow? I already asked about this: https://forum.dlang.org/post/bnkqevhyxwdjjxsct...@forum.dlang.org Tldr; doesn't seem to be possible without multiple alias this or using ABI hacks. O.k., my web search didn't find that topic. The last reply looks promising, wouldn't that work? Lets bump it and discuss there.
Re: Enum that can be 0 or null
On Saturday, 21 May 2016 at 06:36:53 UTC, tsbockman wrote: ... As an example, if VK_NULL_HANDLE only ever needs to be assigned to opaque types on the D side (that is, types that serve only as an ID or address for communicating with the C side), you could do this: private struct VkNullHandle { } enum VK_NULL_HANDLE = VkNullHandle.init; mixin template VkHandle(bool dispatchable) { static if (dispatchable || (size_t.sizeof == 8)) void* bits = null; else ulong bits = 0; this(typeof(this) that) { this.bits = that.bits; } this(VkNullHandle that) { this.bits = typeof(this.bits).init; } ref typeof(this) opAssign(typeof(this) that) { this.bits = that.bits; return this; } ref typeof(this) opAssign(VkNullHandle that) { this.bits = typeof(this.bits).init; return this; } } struct VkDevice { mixin VkHandle!true; } struct VkInstance { mixin VkHandle!true; } struct VkFence { mixin VkHandle!false; } struct VkSemaphore { mixin VkHandle!false; } void main() { VkInstance a = VK_NULL_HANDLE; VkFence b = VK_NULL_HANDLE; } (DPaste: https://dpaste.dzfl.pl/8f4ce39a907f ) The above is typesafe, and can easily be made compatible with a typical C API, since C does no parameter-related name mangling. In this case I don't see how it would be possible to use your VkDevice, etc. as argument to the C functions, as they are of different type, no?
Re: Enum that can be 0 or null
On Monday, 6 June 2016 at 20:32:23 UTC, Alex Parrill wrote: They'd be the same type, since you would define the vulkan functions to take these structures instead of pointer or integer types. It relies on a lot of assumptions about the ABI that make a raw pointer work the same as a structure containing just one pointer, which is why I did not give it much consideration. Is there a way to use opCast (just an idea, not working code) ? private struct VK_HANDLE_HELPER { const void * handle = null; alias handle this; T opCast(T)() if( is( T == uint64_t )) { return 0uL; } } const VK_NULL_HANDLE = VK_HANDLE_HELPER();
Re: Enum that can be 0 or null
On Tuesday, 7 June 2016 at 14:31:40 UTC, Alex Parrill wrote: I don't think opCast gets called for implicit conversions; it only gets called for explicit casts. I'll test it later. It does for type bool, but I fear that's the only exception.
Transform/Compile to C/CPP as a target
Is there any kind of project or workflow that converts D (subset) to C/CPP ?
Re: Transform/Compile to C/CPP as a target
On Saturday, 23 July 2016 at 12:29:45 UTC, rikki cattermole wrote: On 24/07/2016 12:27 AM, ParticlePeter wrote: Is there any kind of project or workflow that converts D (subset) to C/CPP ? This probably will interest you for ldc: http://stackoverflow.com/questions/5180914/llvm-ir-back-to-human-readable-source-language Cool, I didn't know that one but I also didn't invest time in LLVM till now. However, this converts bitcode to C/CPP, my guess would be that the job can be done much better if the original D source would be used in tandem.
Re: Transform/Compile to C/CPP as a target
On Saturday, 23 July 2016 at 19:20:10 UTC, Jacob Carlborg wrote: On 2016-07-23 14:27, ParticlePeter wrote: Is there any kind of project or workflow that converts D (subset) to C/CPP ? No idea about the status but: https://github.com/adamdruppe/tools/blob/dtoh/dtoh.d Thanks, I am looking into this, but I think its still not that what I am searching, it seems to create only C/CPP headers for D libs. I would like to have the whole source code transformed.
Search elemnt in Compile-time Argument List of strings
I want to generate one function for any struct data member, but also want to be able to skip few of the members. The first part works, but I have some trouble with the skipping. I pass the struct type and a Compile-time Argument List of strings as template arguments to a template function, list all members of the struct and compare each member to each element of the List. If the member is in the List skip processing of that member. Pretty straight forward ... should be. // First approach doesn't work: // Error: variable skip cannot be read at compile time void processMember( T, ignore... )() { foreach( member; __traits( allMembers, T )) { bool skip = false; foreach( arg; ignore ) skip = skip || ( arg == member ); static if( !skip ) { // process member here, generate e.g. setter function as string mixin } } } // Second approach, get warnings for every skipped member // and every line after the return statement: // Warning: statement is not reachable void processMember( T, ignore... )() { foreach( member; __traits( allMembers, T )) { foreach( arg; ignore ) static if( arg == member ) return; // process member here, generate e.g. setter function as string mixin } } So how can I achieve my goal the right way?
Re: Search elemnt in Compile-time Argument List of strings
On Tuesday, 26 July 2016 at 19:30:18 UTC, ParticlePeter wrote: // Second approach, get warnings for every skipped member // and every line after the return statement: // Warning: statement is not reachable void processMember( T, ignore... )() { foreach( member; __traits( allMembers, T )) { foreach( arg; ignore ) static if( arg == member ) return; // process member here, generate e.g. setter function as string mixin } } So how can I achieve my goal the right way? I just realized that the second approach, despite the warnings, does not achieve its goal. The members are still forwarded. So I should rather ask how I could filter the members at all.
Re: Search elemnt in Compile-time Argument List of strings
On Tuesday, 26 July 2016 at 20:18:48 UTC, Steven Schveighoffer wrote: ... Thanks a lot for this really cool and detailed explanation (upvoting!). It's a bit weird to work on these compile-time things, but they are so cool when you look at what is available in std.meta and std.traits :) Agreed with each aspect. When I (just) read Philippe Sigaud's D Templates Tutorial I didn't get a thing. Important thing is getting your hands dirty, then it comes slowly.
Re: Search elemnt in Compile-time Argument List of strings
On Tuesday, 26 July 2016 at 20:18:48 UTC, Steven Schveighoffer wrote: ... void processMember( T, ignore... )() { foreach( member; __traits( allMembers, T )) { // this is a compile-time list, so it's a static foreach. foreach(i, arg; ignore ){ // i is the index into the ignore tuple static if( arg == member ) break; // break out of the foreach loop, need to ignore it. else static if(i + 1 == arg.length) // this is the last element! { // process member here, generate e.g. setter function as string mixin } } } } There is one problem with this approach, ignore might be empty (I should have mentioned it). Would you know a workaround for that case as well?
Re: Search elemnt in Compile-time Argument List of strings
On Tuesday, 26 July 2016 at 21:01:19 UTC, Ali Çehreli wrote: On 07/26/2016 01:58 PM, ParticlePeter wrote: On Tuesday, 26 July 2016 at 20:18:48 UTC, Steven Schveighoffer wrote: ... void processMember( T, ignore... )() { foreach( member; __traits( allMembers, T )) { // this is a compile-time list, so it's a static foreach. foreach(i, arg; ignore ){ // i is the index into the ignore tuple static if( arg == member ) break; // break out of the foreach loop, need to ignore it. else static if(i + 1 == arg.length) // this is the last element! { // process member here, generate e.g. setter function as string mixin } } } } There is one problem with this approach, ignore might be empty (I should have mentioned it). Would you know a workaround for that case as well? It should work for empty ignore. Can you show with a short example please. Ali First of all there seems to be a typo, it should not be: else static if(i + 1 == arg.length) ignore must be used instead of arg, as arg.length is the length of a string: else static if(i + 1 == ignore.length) if ignore is empty, its length is 0, so that the statement would always evaluate to false. Btw, if ignore is not empty, only the last element (arg) is skipped.
Re: Search elemnt in Compile-time Argument List of strings
On Tuesday, 26 July 2016 at 21:20:18 UTC, ParticlePeter wrote: ... First of all there seems to be a typo, it should not be: else static if(i + 1 == arg.length) ignore must be used instead of arg, as arg.length is the length of a string: else static if(i + 1 == ignore.length) if ignore is empty, its length is 0, so that the statement would always evaluate to false. Btw, if ignore is not empty, only the last element (arg) is skipped. Test: void processMember( T, ignore... )() { foreach( member; __traits( allMembers, T )) { foreach( i, arg; ignore ) { // i is the index into the ignore tuple static if( arg == member ) break; // break out of the foreach loop, ... else static if( i + 1 == ignore.length ) { // this is the last element! pragma( msg, "processing ", member ); } } } } struct Foo { float a, b, c, d; } int main() { processMember!( Foo );// nada processMember!( Foo, "c" ); // works processMember!( Foo, "c", "b" ); // skips only b }
How to overload member function pointer and a regualr member function
I would like to have this kind of struct: struct Foo { private int i; void function( int i, float f ) bar; // will be defined at runtime void bar( float f ) { bar( i, f ); } } But apparently the function pointer and the member function cannot have the same name: Error: function main.Foo.bar conflicts with variable main.Foo.bar ... I tried with an inner struct: struct Foo { private int i; void function( int i, float f ) bar; // will be defined at runtime private struct Inner { void bar( float f ) { bar( i, f ); } } Inner inner; } But this time I get following error: Error: need 'this' for 'i' of type 'int' What does this message tell me? Should the inner struct not be able to access Foo.i? How else can I get the required behavior? I would prefer to avoid another indirection like this: struct Foo { private int i; void function( int i, float f ) bar; // will be defined at runtime void baz( float f ) { bar( i, f ); } void baz( int ii, float f ) { bar( ii, f ); } }
Re: How to overload member function pointer and a regualr member function
On Tuesday, 25 April 2017 at 09:50:14 UTC, Basile B. wrote: On Monday, 24 April 2017 at 16:46:21 UTC, ParticlePeter wrote: I would like to have this kind of struct: struct Foo { private int i; void function( int i, float f ) bar; // will be defined at runtime void bar( float f ) { bar( i, f ); } } [...] How else can I get the required behavior? Like this: struct Foo1 { private void function(int,float) _bar; void bar(float){} void bar(int i, float f){_bar(i,f);} } Thanks for your reply, but that's what I would like to avoid, the additional indirection to call the function pointer with the original argument count. Do you have any idea about the likelihood of the compiler removing this indirection as an optimizations?
Re: How to overload member function pointer and a regualr member function
On Tuesday, 25 April 2017 at 16:27:43 UTC, Basile B. wrote: On Tuesday, 25 April 2017 at 15:43:48 UTC, ParticlePeter wrote: On Tuesday, 25 April 2017 at 09:50:14 UTC, Basile B. wrote: On Monday, 24 April 2017 at 16:46:21 UTC, ParticlePeter wrote: Thanks for your reply, but that's what I would like to avoid, the additional indirection to call the function pointer with the original argument count. Oops, i can believe i didn't read the last part of your question. Do you have any idea about the likelihood of the compiler removing this indirection as an optimizations? with pragma(inline, true), the function body should be injected at the call sites. This would not help I fear, the body of the function pointer is unknown in an external lib. I rather hoped that the compiler "sees" the parameter forwarding to the fp and is able to directly call it. Best thing would be for both overloads, but I would not know how to verify this.
Re: How to overload member function pointer and a regualr member function
On Wednesday, 26 April 2017 at 08:24:08 UTC, Basile B. wrote: On Tuesday, 25 April 2017 at 18:58:58 UTC, Ali Çehreli wrote: On 04/25/2017 11:54 AM, Ali Çehreli wrote: My analysis is wrong because that writefln() is for the bar(float) overload but I still think what you want is achieved. Ali No it's ok, it works. The additional indirection is well avoided: Let's take this module: == #!dmd -release -inline -O module runnable; struct Foo { private void function(int,float) _bar; void bar(float){} pragma(inline, false) void bar(int i, float f){_bar(i,f);} } struct FooInline { private void function(int,float) _bar; void bar(float){} pragma(inline, true) void bar(int i, float f){_bar(i,f);} } void testInlined(ref FooInline foo) { foo.bar(0,0); } void test(ref Foo foo) { foo.bar(0,0); } void main() { import disassembler, std.stdio; disassembler.symbolTable.addModule!runnable; prettyDisasm(&testInlined).writeln; prettyDisasm(&test, 2).writeln; // dig up to 2 levels, required for the indir. } == and looks at the output: ;--- SUB 00459970h --- ; NAMED: testInlined 00459970h push rbp 00459971h mov rbp, rsp 00459974h sub rsp, 20h 00459978h mov qword ptr [rbp-08h], rdi 0045997Ch xor edi, edi 0045997Eh mov dword ptr [rbp-20h], edi 00459981h movss xmm0, dword ptr [rbp-20h] 00459986h mov rax, qword ptr [rbp-08h] 0045998Ah call qword ptr [rax] 0045998Dh mov rsp, rbp 00459990h pop rbp 00459991h ret ;- ;--- SUB 00459934h --- ; XREFS: [004599A6h] 00459934h push rbp 00459935h mov rbp, rsp 00459938h sub rsp, 10h 0045993Ch mov qword ptr [rbp-08h], rdi 00459940h mov rdi, rsi 00459943h mov rax, qword ptr [rbp-08h] 00459947h call qword ptr [rax] 0045994Ah mov rsp, rbp 0045994Dh pop rbp 0045994Eh ret ;- ;--- SUB 00459994h --- ; NAMED: test 00459994h push rbp 00459995h mov rbp, rsp 00459998h sub rsp, 10h 0045999Ch xor esi, esi 0045999Eh mov dword ptr [rbp-10h], esi 004599A1h movss xmm0, dword ptr [rbp-10h] 004599A6h call 00459934h 004599ABh mov rsp, rbp 004599AEh pop rbp 004599AFh ret ;- - testInlined() contains only the delegate call. (call qword ptr [rax]) - test() contains a call (call 00459934h) which contains the delegate call (call qword ptr [rax]) Actually i've even had to add (pragma inline false) to show the difference since DMD inlined automatically bar() in test(). Guys, you're great! Thanks a lot!
C++ Interfacing:'static' array function parameter contradiction
C++ Function: bool cppFunc( float[3] color ); D binding: extern(C++) bool cppFunc( float[3] color ); Using with: float[3] my_color; cppFunc( my_color ); -> Error: Internal Compiler Error: unable to pass static array to extern(C++) function. Error: Use pointer instead. Using with: cppFunc( my_color.ptr ); -> Error: function cppFunc( float[3] color ) is not callable using argument types (float*) Altering D binding: extern(C++) bool cppFunc( float* color ); Using with: cppFunc( my_color.ptr ); -> error LNK2001: unresolved external symbol "bool __cdecl cppFunc(float *)" Binding.exe : fatal error LNK1120: 1 unresolved externals Error: linker exited with status 1120 dmd failed with exit code 1120. So what next? How can I interface to the cpp function?
Re: C++ Interfacing:'static' array function parameter contradiction
On Friday, 28 April 2017 at 17:15:54 UTC, kinke wrote: On Friday, 28 April 2017 at 15:56:17 UTC, ParticlePeter wrote: So what next? How can I interface to the cpp function? *** C++: bool cppFunc(float (&color)[3]) { color[0] = 1; color[1] = 2; color[2] = 3; return true; } *** D: extern(C++) bool cppFunc(ref float[3] color); void main() { float[3] my_color; cppFunc(my_color); assert(my_color == [ 1, 2, 3 ]); } The c++ lib is not mine and your answer implies extra work on the c++ from my side. Possible, but not desired, I think calling my original c++ function should interface with an d pointer. That being said, I think Kagamin is right.
Re: C++ Interfacing:'static' array function parameter contradiction
On Friday, 28 April 2017 at 17:57:34 UTC, Ali Çehreli wrote: On 04/28/2017 08:56 AM, ParticlePeter wrote: > C++ Function: > bool cppFunc( float[3] color ); > > D binding: > extern(C++) bool cppFunc( float[3] color ); > > Using with: > float[3] my_color; > cppFunc( my_color ); > > -> Error: Internal Compiler Error: unable to pass static array to That part is a bug at least in the compiler message. Is it really an internal ctompiler error? Doesn't look like it: the compiler is talking to us happily. :) My simple test works for me: // deneme.cpp float cppFunc(float color[3]) { return color[0] + color[1] + color[2]; } $ g++ -c deneme.cpp -o deneme_cpp.o // deneme.d extern(C++) float cppFunc(float * color); void main() { float[3] my_color = [ 1.5, 2.5, 3.5 ] ; assert(cppFunc(my_color.ptr) == 7.5); } $ dmd deneme_cpp.o deneme.d -of=deneme Builds and runs fine... on Linux... I don't know whether that's significant. Ali Interesting, your example corresponds to my third case, the linker error. I am on Window, building an x64 App, afaik in that case the MS Visual Studio linker is used instead of optilink. Will add your findings to the bug report.
Re: C++ Interfacing:'static' array function parameter contradiction
On Friday, 28 April 2017 at 17:57:34 UTC, Ali Çehreli wrote: On 04/28/2017 08:56 AM, ParticlePeter wrote: > C++ Function: > bool cppFunc( float[3] color ); > > D binding: > extern(C++) bool cppFunc( float[3] color ); > > Using with: > float[3] my_color; > cppFunc( my_color ); > > -> Error: Internal Compiler Error: unable to pass static array to That part is a bug at least in the compiler message. Is it really an internal ctompiler error? Doesn't look like it: the compiler is talking to us happily. :) My simple test works for me: // deneme.cpp float cppFunc(float color[3]) { return color[0] + color[1] + color[2]; } $ g++ -c deneme.cpp -o deneme_cpp.o // deneme.d extern(C++) float cppFunc(float * color); void main() { float[3] my_color = [ 1.5, 2.5, 3.5 ] ; assert(cppFunc(my_color.ptr) == 7.5); } $ dmd deneme_cpp.o deneme.d -of=deneme Builds and runs fine... on Linux... I don't know whether that's significant. Ali Btw, according to [1] your example should not work either, I doubt that there is a difference between C and C++ interfacing, it should be: extern(C++) float cppFunc( ref float[3] color ); In my case its a linker error as well. [1] http://dlang.org/spec/interfaceToC.html#passing_d_array
Re: C++ Interfacing:'static' array function parameter contradiction
On Saturday, 29 April 2017 at 01:49:56 UTC, Atila Neves wrote: On Friday, 28 April 2017 at 18:41:22 UTC, kinke wrote: On Friday, 28 April 2017 at 18:07:49 UTC, ParticlePeter wrote: Interesting, your example corresponds to my third case, the linker error. I am on Window, building an x64 App, afaik in that case the MS Visual Studio linker is used instead of optilink. Will add your findings to the bug report. Apparently Microsoft's C++ compiler doesn't mangle `float arg[3]` parameters identically to `float* arg`: void cppSArray(float color[3]) => ?cppSArray@@YAXQEAM@Z void cppPtr(float* color) => ?cppPtr@@YAXPEAM@Z The worst part about that is mangling aside, the two declarations are identical to the compiler. Atila In this context, can anybody explain [1], in particular, in this case, one should extern( C++ ) void cppSArray( ref float[3] color ); instead of: extern( C++ ) void cppSArray( float* color ); Others and me in this discussion seem to agree that parameter (float color[3]) is equivalent to (float* color) in C++ world. [1] http://dlang.org/spec/interfaceToC.html#passing_d_array
Re: C++ Interfacing:'static' array function parameter contradiction
On Saturday, 29 April 2017 at 00:31:32 UTC, Nicholas Wilson wrote: If you are having problems with the linker with Ali's you can do ``` extern(C++) bool cppFunc( float[3] color ); // correct signature, but causes compiler error pragma(mangle, cppFunc.mangleof) float cppFunc(float * color); // compatible signature but wrong mangling overridden with pragma(mangle,...) Thanks for that hint! I got it to work. Side note, cppFunc.mangleof cannot be used as it is unknown. I guess your intention was to get the C++ mangling from somewhere else, I got it from dependency walker.
Re: C++ Interfacing:'static' array function parameter contradiction
On Saturday, 29 April 2017 at 10:17:47 UTC, Atila Neves wrote: On Saturday, 29 April 2017 at 06:22:03 UTC, ParticlePeter wrote: On Saturday, 29 April 2017 at 01:49:56 UTC, Atila Neves wrote: On Friday, 28 April 2017 at 18:41:22 UTC, kinke wrote: [...] The worst part about that is mangling aside, the two declarations are identical to the compiler. Atila In this context, can anybody explain [1], in particular, in this case, one should extern( C++ ) void cppSArray( ref float[3] color ); instead of: extern( C++ ) void cppSArray( float* color ); Others and me in this discussion seem to agree that parameter (float color[3]) is equivalent to (float* color) in C++ world. [1] http://dlang.org/spec/interfaceToC.html#passing_d_array It's "just" the mangling. If it were `extern(C)` there'd be nothing to talk about. Atila O.k. got it, so both D variants work with the same C++ mangling, thanks.
C++ binding issues with C++ function returning a simple POD struct.
I am statically linking to ImGui [1] on Win 10 x64, quite successfully till this issue came up. The noticed error so far comes when an ImGui function returns an ImVec2, a simple POD struct of two float members. I can use this struct as argument to functions but when it is returned from a function I get a 0xC005: Access violation reading location 0x. I can even debug the process with Visual Studion, mixed d and c++ sources. The functions I tested return data from some internal global ImGui data, which I can fully examine, the crash happens on the return statement. Moreover, some functions have variations which return only one component from that ImVec2 POD, which do work as expected, e.g.: ImVec2 GetCursorPos(); // crash float GetCursorPosX(); // works float GetCursorPosY(); // works The latter do basically the same as the first one, but return ImVec.x or .y respectively. How could I further debug this? If somebody would be willing to look at the source, the binding is here [2]. [1] https://github.com/ocornut/imgui [2] https://github.com/ParticlePeter/imgui_lib
Re: C++ binding issues with C++ function returning a simple POD struct.
On Sunday, 21 May 2017 at 19:58:32 UTC, Stefan Koch wrote: On Sunday, 21 May 2017 at 19:33:06 UTC, ParticlePeter wrote: I am statically linking to ImGui [1] on Win 10 x64, quite successfully till this issue came up. The noticed error so far comes when an ImGui function returns an ImVec2, a simple POD struct of two float members. I can use this struct as argument to functions but when it is returned from a function I get a 0xC005: Access violation reading location 0x. I can even debug the process with Visual Studion, mixed d and c++ sources. The functions I tested return data from some internal global ImGui data, which I can fully examine, the crash happens on the return statement. Moreover, some functions have variations which return only one component from that ImVec2 POD, which do work as expected, e.g.: [...] are you aware of https://github.com/Extrawurst/DerelictImgui ? Yes I am, its (understandably) not being updated too regularly, it goes the route of creating a C binding, and a D binding on top, lot of work. We should be able to bind the C++ variant directly by now I think.
Re: C++ binding issues with C++ function returning a simple POD struct.
On Monday, 22 May 2017 at 01:27:22 UTC, Nicholas Wilson wrote: On Sunday, 21 May 2017 at 19:33:06 UTC, ParticlePeter wrote: I am statically linking to ImGui [1] on Win 10 x64, quite successfully till this issue came up. The noticed error so far comes when an ImGui function returns an ImVec2, a simple POD struct of two float members. I can use this struct as argument to functions but when it is returned from a function I get a 0xC005: Access violation reading location 0x. I can even debug the process with Visual Studion, mixed d and c++ sources. The functions I tested return data from some internal global ImGui data, which I can fully examine, the crash happens on the return statement. Moreover, some functions have variations which return only one component from that ImVec2 POD, which do work as expected, e.g.: ImVec2 GetCursorPos(); // crash float GetCursorPosX(); // works float GetCursorPosY(); // works The latter do basically the same as the first one, but return ImVec.x or .y respectively. How could I further debug this? If somebody would be willing to look at the source, the binding is here [2]. [1] https://github.com/ocornut/imgui [2] https://github.com/ParticlePeter/imgui_lib Probably because the D side is expecting to have the struct returned in a pointer allocated by the callee and then the C++ puts it in regs and BOOM. Thanks for your reply, but that would be wired. The function signature clearly tells me: I am returning a (copy of a) ImVec2 on the stack. How could D expect any kind of pointer in that case? And should that not be true for the variants returning float as well? Almost same signature. But I agree with enhanced fishiness happening in the interface. If you wrap the C++ side to return the struct by a pointer then use that in D, then it should work. I've hoped to avoid extra work other then translating the header, but now I fear it won't. I'll give it a try.
Re: C++ binding issues with C++ function returning a simple POD struct.
On Monday, 22 May 2017 at 01:39:04 UTC, evilrat wrote: On Monday, 22 May 2017 at 01:27:22 UTC, Nicholas Wilson wrote: Probably because the D side is expecting to have the struct returned in a pointer allocated by the callee and then the C++ puts it in regs and BOOM. If you wrap the C++ side to return the struct by a pointer then use that in D, then it should work. And this is actually D problem. In fact first bug report on this thing was dated back to 2014. Still not fixed. Thanks for your reply, do you have any links to some bug report of that issue? There is possible hacky workaround to try - put struct as pointer arg instead of return and make helper method to use it, like this HACK --- // extern(C++) of course void GetCursorPos(ImVec2* v); // helper ImVec2 GetCursorPos() { ImVec2 temp; GetCursorPos(&temp); return temp; } -- Actually, your example would work just fine, my problem and possible solution is the other way around :-). First I'll try to force a copy with a wrapper func and same sig.
Re: C++ binding issues with C++ function returning a simple POD struct.
On Monday, 22 May 2017 at 07:24:20 UTC, evilrat wrote: On Monday, 22 May 2017 at 06:33:37 UTC, ParticlePeter wrote: On Monday, 22 May 2017 at 01:39:04 UTC, evilrat wrote: And this is actually D problem. In fact first bug report on this thing was dated back to 2014. Still not fixed. Thanks for your reply, do you have any links to some bug report of that issue? Just search for "c++ struct" https://issues.dlang.org/buglist.cgi?quicksearch=c%2B%2B%20struct https://issues.dlang.org/show_bug.cgi?id=13207 https://issues.dlang.org/show_bug.cgi?id=16527 That's really old, and of essential requirement I would assume. Thanks, I will comment the bug. Actually, your example would work just fine, my problem and possible solution is the other way around :-). First I'll try to force a copy with a wrapper func and same sig. You mean from D to C++? Well, that sucks. No, no, this (other) way around :-), still C++ to D. It actually works btw: HACK --- // original C++ ImVec2 GetCursorPos(); // C++ helper void GetCursorPos(ImVec2& result) { result = GetCursorPos(); } // bind with extern(C++) void GetCursorPos(ref ImVec2 result); --
Re: C++ binding issues with C++ function returning a simple POD struct.
On Monday, 22 May 2017 at 08:25:45 UTC, evilrat wrote: On Monday, 22 May 2017 at 08:03:07 UTC, ParticlePeter wrote: No, no, this (other) way around :-), still C++ to D. It actually works btw: HACK --- // original C++ ImVec2 GetCursorPos(); // C++ helper void GetCursorPos(ImVec2& result) { result = GetCursorPos(); } // bind with extern(C++) void GetCursorPos(ref ImVec2 result); -- My proposed hack is purely D side though O_- Then I am not getting your hack, this function here, does not exist on the C++ side. HACK --- // extern(C++) of course void GetCursorPos(ImVec2* v); How is it supposed to work then if there is no definition?
Re: C++ binding issues with C++ function returning a simple POD struct.
On Monday, 22 May 2017 at 13:03:17 UTC, evilrat wrote: On Monday, 22 May 2017 at 11:25:31 UTC, ParticlePeter wrote: Then I am not getting your hack, this function here, does not exist on the C++ side. HACK --- // extern(C++) of course void GetCursorPos(ImVec2* v); How is it supposed to work then if there is no definition? you "forge" this signature insted of correct one, I suggest also wrap it some handy version in case this is "suddenly got working" version (PROPER_ABI) { extern(C++) ImVec2 GetCursorPos(); } else // hacky one { extern(C++) void GetCursorPos(ImVec2* v); ... put helper here to match excepted API, in case of C++ class add it to the end as 'final'... } This works because it matches name mangling on this one. And even if its not it is possible to hammer it in with pragma mangle. Never stop learning, that actually works! But only with the pragma mangle hammer. Nice, but again some more mangle pragmas. The better way seems to be Jerry's suggestion, works as well, see bellow.
Re: C++ binding issues with C++ function returning a simple POD struct.
On Monday, 22 May 2017 at 14:01:56 UTC, Jerry wrote: IIRC the problem is that it isn't a POD type. ImVec2 has its own default constructor. The problem now is that because it no longer is POD, Window's ABI handles it different and doesn't put the value in a register. Now with D is that you aren't allowed to specify your own default constructor, so there's no equivalent way for it to know that it isn't a POD. A way around this is to specify your own destructor or copy constructor in the D ImVec2. I forget what the rules are for it, but I think that should do it. Thanks, with any of them, ~this or this(this) (both can be empty), the functions work as expected, nice. Also replying your next post, extern(C++) is on for the whole module: https://github.com/ParticlePeter/imgui_lib/blob/master/source/imgui/types.d#L39 but I learned how to link to github lines from your post :-)
Slow compilation using a lib with template methods
Hello community, here is a post with multiple questions regarding compile times, or rather the optimization of the compile and link process. I work with VisualD and am interested in optimizing my projects with this utility, hence there will be a similar topic linking here, where I explain my solution an projects setup. One of the advertised advantages of D is compiler speed. Hence I think that I do some mistakes, as my ( hobby ) projects consist of a quite small code base. The more I learn about the language, the more features I use, the more my compile times increase. I guess my usage of these features, template in particular, are not well designed. Question 1) How can I profile my compile times ? I have some guesses, but they might be wrong. Question 2) Is there a good read about the intermediate files dmd creates (e.g. *.obj, *.def), and how they might be utilized to improve compile times ( incremental compilation? ) ? Next I need to explain some details about my codebase: I use a lib, lets call it MyLib, which consists of about 15 modules, each 300 lines of code in average. Most of the modules consist of one class and some of these classes have one or two template methods. I link this lib statically to another ( one ) lib I created from some DerelictOrg modules. Projects which use MyLib consist of about five modules which have aprox 200 lines of code in average. I would say this is a small codebase. I use MyLib all of these projects, and about the time I introduced template methods in MyLib I noticed a slow down in compiling and linking ( but I cannot tell for sure that the compile time is related to the template method, however lets assume it is ). Before that my projects used to build in five seconds max, now it is more like 30 seconds, and most of the time is spend in building MyLib. My guess is that MyLib is completely rebuild when I use it in a projects with differently typed calls to the template methods. Question 3) How smart is dmd in separating template from non-template code, in particular if both codeblocks are defined in the same module / class ? How can I assist dmd in determining and/or keeping files necessary for incremental compilation and linking ? One step deeper into MyLib. All the template methods have only one type parameter. I use arbitrary typed Arrays, generate some information about the type with compile time reflection ( primitive and struct types ), and call a non-template method which takes a void[] array and the extracted type information. That made me think about GOF Decorator pattern via UFCS. Unfortunately, I guess due to the restrictions mentioned in the docs for UFCS ( , I do not get it to work. Here is my approach: /// file LibModule.d module LibModule; struct TypeDescriptor { ... } class NonTemplate { void nonTemplateMethod( void[] array , TypeDescriptor typeDescriptor ) { ... } } void templateFunction(T)( NonTemplate nonTemplate , T[] array ) { auto typeDescriptor = ... /// mindblowing compile-time magic nonTemplate.nonTemplateMethod( array , typeDescriptor ); } ... /// Usage in file MainModule.d module MainModule; import LibModule; float[100] floatArray; auto nonTemplate = new NonTemplate; nonTemplate.templateFunction( floatArray ); ... Question 4) This does not work, how can I make it work ? It also did not work with a separate module TemplateModule for templateFunction, tried to import TemplateModule in LibModule as well as in MainModule. Question 5) Would this improve my compilation speed at all ? How would I compile and link only non template modules/classes in my lib and still be able to use templateFunction in the described UFCS way ? The following question is valid if only I get answer like: Not possible due to UFCS restrictions Specification of UFCS is presented with an example explaining the restrictions, which quite confuses me: Question 6) The example given as reasoning to UFCS restrictions, isn't that using symbol shadowing, which is deprecated ? How could Walter and Andrei be asked politely to loosen these restrictions, as the gain in functionality is significant compared to the ( unlikely ? ) example scenario ? Thanks in advance for any advice. Cheers, ParticlePeter
Visual D: Settings to Improve compil and link process
Hello Community, I thought there's a separate forum for VisualD. It did exist when VisualD was on DSource, so why not add it here as well? Or am I to blind to see? Anyway, this thread is an addition to my previous one in this forum: http://forum.dlang.org/thread/wkkuvzkzeupyfdpwe...@forum.dlang.org I noticed increasing compile times in my projects. As I use only VisualD I would like to understand how I would shorten compile time with this tool. Brief description to my setup, more details can be fond in the other post. I have one Lib, called MyLib consisting of around 15 modules, one class per module, zero to two template methods per class with one type parameter each. All my projects use MyLib, and call the template methods with different types. Most of the compile time of any of theses projects is spent in rebuilding MyLib. I am not sure why and where so much time is spent, but is there a way to profile my COMPILE time with VisualD? There are several VisualD project properties which I do not understand fully, but hope that they might help, namely: Configuration Properties - General - Files to clean Configuration Properties - Compiler - Output - Multiple Object Files Configuration Properties - Compiler - Output - Keep Path From Source File Could not cleaning some files improve compilation ? Could Multiple Object Files be used to separately compile non-template and template code blocks ? What does Keep Path From Source File do at all ? It is possible to remove the template methods from my classes, create free functions instead and use them in a UFCS way. Unfortunately I have not figured out UFCS properly, as my approaches do not work ( guess due to UFCS restrictions ). Anyway, would it help ( and is it possible at all ) to put the template functions in a separate module so that only the corresponding object file requires a recompile ? I am asking in the context of the Multiple Object Files setting. I tried to not build MyLib at all, and use the sources directly, but the project setup is kind of confusing. Again, there is a setting which I seem to not understand fully. So one project, lets say MyProject, has only its own module files added to the VisualD project. I want the compiler to also use the MyLib source files, but do not want to add them to MyProject, reasoning bellow. There is one entry in the project properties, which should make this behavior possible, but it doses not. Why ? Configuration Properties - Compiler - General - Additional Imports As far as I understand this setting, I am supposed to enter module search paths. But I do get linker errors when I do not add either MyLib.lib to the linker settings or all the source files of MyLib to MyProject. Reasoning why I do not want to do this: I have one solution file with the MyLib project and ten projects like MyProject using MyLib. I would need to add all the source files to all the projects, they would be reachable and editable at 11 different location within my solution file. This does not not seem to be a clean way to set it up. Any advice to any or all the thoughts and issues ? Thanks in advance. Cheers, ParticlePeter
Re: Visual D: Settings to Improve compil and link process
On Sunday, 6 July 2014 at 08:09:07 UTC, Rainer Schuetze wrote: On 05.07.2014 16:05, ParticlePeter wrote: ... It is possible to remove the template methods from my classes, create free functions instead and use them in a UFCS way. Unfortunately I have not figured out UFCS properly, as my approaches do not work ( guess due to UFCS restrictions ). That would not help, templates are instantiated and compiled when they are used, not when they are declared. Hence the idea of splitting the class. After the split, the class has only non-template methods and is part of MyLib. Template UFCS functions working with this class are in another module, and not part of the lib. With this approach MyLib does not need to recompile, only the UFCS functions module needs to recompile due to different instantiations. UFCS works now for me btw. I tried to not build MyLib at all, and use the sources directly, but the project setup is kind of confusing. Again, there is a setting which I seem to not understand fully. So one project, lets say MyProject, has only its own module files added to the VisualD project. I want the compiler to also use the MyLib source files, but do not want to add them to MyProject, reasoning bellow. There is one entry in the project properties, which should make this behavior possible, but it doses not. Why ? Configuration Properties - Compiler - General - Additional Imports As far as I understand this setting, I am supposed to enter module search paths. This is correct. > But I do get linker errors when I do not add either MyLib.lib to the linker settings or all the source files of MyLib to MyProject. You should add a dependency in the "Project -> Project Dependencies" dialog. This will ensure proper rebuilds and add the library to the command line of dependent projects. The idea is to NOT create a lib, but just use the source code from MyLib in MyProject as additional includes. No project dependencies. I was hoping that some .obj files are still available from last builds, and do not need to recompile, as they are up to date ( reason for question about not cleaning files ). The modules form MyProject do import the MyLib modules properly, I do not get compiler errors. However, the compiler should create Object files from MyLib modules, and the linker should link them. But he does not. On the other hand, when I add MyLib modules to MyProject ( Rightclick MyProject - add - existing item... MyLib source files ) then linking works. I do not understand why the later step is necessary.
Re: Visual D: Settings to Improve compil and link process
On Sunday, 6 July 2014 at 19:27:38 UTC, Rainer Schuetze wrote: Ok, that allows separate compilation of the class, but templates are still compiled with the rest of the program. I thought the templates were the part that cause the slow compilation. I had no chance to profile till now, but I don't think that the templates are slow. They just extract type information from primitive or struct arrays, and pass the type info and void array on. I have the impression that the whole rebuild-relink process of the library itself was taking so long, so I wanted ensure that the non-templte code ( the majority ) does not have to be compiled over and over again. These object files are in the library ;-) That means manual selection, though, as incremental builds to multiple object files don't work with dmd, and single file compilation is painfully slow. Not sure if I am getting this right, so when one object file has to be recompiled all other object files, even if up to date, would be recompiled ? The modules form MyProject do import the MyLib modules properly, I do not get compiler errors. However, the compiler should create Object files from MyLib modules, and the linker should link them. But he does not. On the other hand, when I add MyLib modules to MyProject ( Rightclick MyProject - add - existing item... MyLib source files ) then linking works. I do not understand why the later step is necessary. dmd does not compile imported modules, but rdmd does. Ähm ... not seeing the connection here either, why is this significant ? I feel that I could not explain my problem properly, so one example: Importing phobos modules. I do not have to define any import path or lib file in the project settings, I just need to import std.somthing. That's because the import path for phobos modules are stored in the dmd sc.ini file. When I want to import my modules which are somewhere on my hard-drive and not added to my project I need to tell the compiler where these modules can be found, using the additional import path project setting. That's fine, doing this. But result is, std.somthing works, my modules in a path known by the compiler don't work, giving me linker errors. Why ? ( I do not create a lib, I just want to import the module. )
[dimgui] building results in 16 warnings, no error BUT: Building .dub\lib\imgui_d.lib failed!
Hi, am still searching for the right place to ask library related questions, and was advised to ask them here. The dimgui library looks interesting for my projects ( https://github.com/d-gamedev-team/dimgui ), but I don't manage to get it built or run the examples. I have no clue what's going wrong, as I just get a "FAIL" message. I use Win 8.1, dmd 2.067 ( and 2.066.1 before that ). I tried to build with dub: FAIL .dub\build\library-debug-windows-x86-dmd_2067-C976DEDDFC09960A5E012C28B5036DF0\ imgui staticLibrary Error executing command run: dmd failed with exit code 1. as well as created visualD project files and build with VS2013: Building .dub\lib\imgui_d.lib failed! How to debug this ? Or anyone with dimgui experience ? Regards, ParticlePeter
Re: [dimgui] building results in 16 warnings, no error BUT: Building .dub\lib\imgui_d.lib failed!
On Saturday, 4 April 2015 at 21:29:57 UTC, Jacques Müller wrote: On Saturday, 4 April 2015 at 18:11:32 UTC, ParticlePeter wrote: Hi, am still searching for the right place to ask library related questions, and was advised to ask them here. The dimgui library looks interesting for my projects ( https://github.com/d-gamedev-team/dimgui ), but I don't manage to get it built or run the examples. I have no clue what's going wrong, as I just get a "FAIL" message. I use Win 8.1, dmd 2.067 ( and 2.066.1 before that ). I tried to build with dub: FAIL .dub\build\library-debug-windows-x86-dmd_2067-C976DEDDFC09960A5E012C28B5036DF0\ imgui staticLibrary Error executing command run: dmd failed with exit code 1. as well as created visualD project files and build with VS2013: Building .dub\lib\imgui_d.lib failed! How to debug this ? Or anyone with dimgui experience ? Regards, ParticlePeter By default DUB calls the compiler with the command line argument -w, which aborts the compilation if a warning is printed. Add the following line to your dub.json: "buildRequirements": ["allowWarnings"] http://code.dlang.org/package-format#build-requirements Thank you very much, that did the trick!
[DerelictOrg] Forum down ?
Hi, I think I have a bug report for DerelictGL3, but cannot find the related Forum ( http://dblog.aldacron.net/forum/index.php ), is it still in the process of being moved ? Regards, ParticlePeter
Re: [DerelictOrg] Forum down ?
Done On Tuesday, 7 April 2015 at 10:50:35 UTC, Namespace wrote: On Tuesday, 7 April 2015 at 10:48:38 UTC, ParticlePeter wrote: Hi, I think I have a bug report for DerelictGL3, but cannot find the related Forum ( http://dblog.aldacron.net/forum/index.php ), is it still in the process of being moved ? Regards, ParticlePeter Post it there: https://github.com/DerelictOrg/DerelictGL3/issues
dub run subPackage by default
Hello, I have a targetType sourceLibrary and demonstrate its usage through a subPackage. For the library itself 'dub run' is meaningless, but not for the subPackage. Is there a way to tell dub through dub.sdl or dub.json to build and run a specific subPackage by default, without having to call: 'dub run mySourceLib:myPackage' ? Why do I want this? Because I use Sublime Text build systems to execute a dub build/run. As far as I can see I would have to set up a build version for any package:subPackeg combination. - PP
Re: dub run subPackage by default
On Tuesday, 1 September 2020 at 14:45:43 UTC, Andre Pany wrote: On Tuesday, 1 September 2020 at 11:45:34 UTC, ParticlePeter wrote: [snip] I have a enhancement for dub in my mind, which would also solve your issue. Similiar to setup.py in python you would be able to define an entry point in dub.json. "entryPoints": {"foo":"subPackageName"} Command `dub install mypackage` would create a batch file/bash script with name foo containing the command `dub run mypackage:subPackageName -- %*`. Unfortunately I do not have any time to work on this. Kind regards Andre So, I take it that such a feature does not exist already. Your suggestion sounds nice, but why do you think it would be necessary to go through a batch file or script? I think it should be possible from within dub, to feed the chosen compiler backend with the proper commands to either build or run the appropriate entryPoint. - PP
BetterC issues with ErupteD Vulkan binding typedef handles
Hello, I am experimenting with betterC and Vulkan through Erupted [0] binding, but unfortunately I find myself hunting down these kind of errors: ..\ErupteD\source\erupted\types.d-mixin-77(77,1): Error: `TypeInfo` cannot be used with -betterC The issue is with Vulkan type handles. One such error occurs when a function's parameter list contains an optional slice of such handles, e.g.: void queueSubmit( VkQueuequeue, VkCommandBuffer[] command_buffers, VkSemaphore[] wait_semaphores = [], // error: TypeInfo required VkPipelineStageFlags[] wait_dest_stage_masks = [], // ok, not a handle VkSemaphore[] signal_semaphores = [] // error: TypeInfo required ) { .. } A possible workaround which I found is: VkSemaphore[] wait_semaphores = ( const VkSemaphore[] ).init, but this feels more like fighting a symptom instead of getting rid of the cause. I am wondering if there is a better way to translate these C typedefs to D: // from vulkan_core.h [1] // ... #define VK_DEFINE_HANDLE(object) typedef struct object##_T* object; #if !defined(VK_DEFINE_NON_DISPATCHABLE_HANDLE) #if defined(__LP64__) || defined(_WIN64) || (defined(__x86_64__) && !defined(__ILP32__) ) || defined(_M_X64) || defined(__ia64) || defined (_M_IA64) || defined(__aarch64__) || defined(__powerpc64__) #define VK_DEFINE_NON_DISPATCHABLE_HANDLE(object) typedef struct object##_T *object; #else #define VK_DEFINE_NON_DISPATCHABLE_HANDLE(object) typedef uint64_t object; #endif #endif // ... VK_DEFINE_HANDLE(VkQueue) VK_DEFINE_NON_DISPATCHABLE_HANDLE(VkSemaphore) // ... Correspondingly to the C typedefs: // from erupted/types.d [2] // ... enum VK_DEFINE_HANDLE( string name ) = "struct " ~ name ~ "_handle; alias " ~ name ~ " = " ~ name ~ "_handle*;"; version( X86_64 ) { alias VK_DEFINE_NON_DISPATCHABLE_HANDLE( string name ) = VK_DEFINE_HANDLE!name; enum VK_NULL_ND_HANDLE = null; } else { enum VK_DEFINE_NON_DISPATCHABLE_HANDLE( string name ) = "alias " ~ name ~ " = ulong;"; enum VK_NULL_ND_HANDLE = 0uL; } // ... mixin( VK_DEFINE_HANDLE!q{VkQueue} ); mixin( VK_DEFINE_NON_DISPATCHABLE_HANDLE!q{VkSemaphore} ); // ... I am running building for x64, would anyone know a smoother betterC approach to these typedefs? [0] https://github.com/ParticlePeter/ErupteD [1] https://github.com/KhronosGroup/Vulkan-Headers/blob/master/include/vulkan/vulkan_core.h [2] https://github.com/ParticlePeter/ErupteD/blob/master/source/erupted/types.d
Re: BetterC issues with ErupteD Vulkan binding typedef handles
On Sunday, 20 December 2020 at 15:52:39 UTC, Adam D. Ruppe wrote: On Sunday, 20 December 2020 at 15:45:59 UTC, ParticlePeter wrote: VkSemaphore[] wait_semaphores = [], // error: TypeInfo required does it still error if you just use = null? they work the same way but might avoid the annoying error. Wow, it does, that was unexpected and unexpectedly hassle free, thanks a lot :-)
Re: BetterC issues with ErupteD Vulkan binding typedef handles
[snip] Forgot to add another question. The mentioned error message is not too helpful in locating the real offended code. Is there a way to get more information or additional hints about the actual cause of the problem?
How to create DDoc for string mixin generated functions?
I am producing a bunch of functions/methods through string mixins. I also generated DDoc comments for those functions, in the hope that they would produce proper documentation, but they don't. So how can this be accomplished?
Anything like HPPTOD out there?
I would like to auto convert c++ header to d module. Is there some project aiming for this? I know of VisualD c++ to d conversion wizzard [1] and LLVM tooling based CPP2D [2], both of them aiming for whole cpp conversion. But I a searching for something lightweight like HTOD extended to C++. [1] https://rainers.github.io/visuald/visuald/CppConversion.html [2] https://github.com/lhamot/CPP2D
Re: How to create DDoc for string mixin generated functions?
On Tuesday, 26 November 2019 at 13:02:39 UTC, Jonathan M Davis wrote: On Monday, November 25, 2019 9:25:08 AM MST ParticlePeter via ... - Jonathan M Davis Thanks for that thorough explanation. In may case I use the string mixin to forward outer struct property calls to members of an inner struct. I'll try to refactor the string mixin as template mixin. From top of my head I see one complication. The parameter name to the property would change and that means its name in the doc comment should change as well. Any ideas how to solve that? Or would it be possible only with same param name for all the property instantiations?
Re: How to create DDoc for string mixin generated functions?
On Tuesday, 26 November 2019 at 19:41:26 UTC, Adam D. Ruppe wrote: On Tuesday, 26 November 2019 at 19:27:55 UTC, ParticlePeter wrote: In may case I use the string mixin to forward outer struct property calls to members of an inner struct. Did you try opDispatch btw? It might be simpler to implement and to document. No I didn't, I judged it being the least feasible to produce appropriate doc comments. How could this work?
Re: How to create DDoc for string mixin generated functions?
On Wednesday, 27 November 2019 at 15:14:21 UTC, ParticlePeter wrote: On Tuesday, 26 November 2019 at 19:41:26 UTC, Adam D. Ruppe wrote: On Tuesday, 26 November 2019 at 19:27:55 UTC, ParticlePeter wrote: In may case I use the string mixin to forward outer struct property calls to members of an inner struct. Did you try opDispatch btw? It might be simpler to implement and to document. No I didn't, I judged it being the least feasible to produce appropriate doc comments. How could this work? Maybe not asked precisely enough. Its clear how op dispatch works, but how could I create different documentation for different dispatch instantiations?
Re: How to create DDoc for string mixin generated functions?
On Thursday, 28 November 2019 at 14:00:56 UTC, Adam D. Ruppe wrote: On Wednesday, 27 November 2019 at 15:14:21 UTC, ParticlePeter wrote: I judged it being the least feasible to produce appropriate doc comments. How could this work? Just like: /// Forwards members to [Whatever] auto opDispatch.. and then the documentation shows that with the link so they can refer to the other thing easily enough. That's what I did on my thing for example http://dpldocs.info/experimental-docs/arsd.dom.ElementCollection.opDispatch.html (possible I could make my doc gen recognize the pattern and paste in generated docs too, but I personally like the link enough) That would not work nicely in my case. Firstly my inner structs are from foreign code (vulkan structs through erupted binding) for which I do not create documentation. Secondly, I am skipping some of the inner struct members. Basically I use a template to produce the string mixin. The template has an VarArg list accepting inner struct member names to be skipped. Hence it would be better to actually create individual doc comments for each forwarding property instantiation.
How to translate this to D: const char *const* someConstPtr;
Hi, const char *const* someConstPtr; Error: no identifier for declarator char* Error: declaration expected, not '*' How would I translate this properly to d? Cheers, PP
Re: How to translate this to D: const char *const* someConstPtr;
That was fast, thanks :-)
Convert C array pointer to a D slice without data copy
I get the point to an array from a c function, the data size from another function. The data should be only readable at the D side, but I would like to use it as a D slice without copying the data. Is this possible ?
Re: Convert C array pointer to a D slice without data copy
On Monday, 18 May 2015 at 09:23:26 UTC, tcak wrote: On Monday, 18 May 2015 at 09:18:33 UTC, ParticlePeter wrote: I get the point to an array from a c function, the data size from another function. The data should be only readable at the D side, but I would like to use it as a D slice without copying the data. Is this possible ? char* dataPtr; size_t dataLen; auto data = dataPtr[0 .. dataLen]; This doesn't do any copying. BUT I am not sure what GC would be doing about it. After you use it, you might want to set `data` to null in case of a problem. Thanks, works. Should be in the "Interfacing to C" Reference Article.
Derlict binding with out_of_link_module struct definitions
Wow, sometimes tough to find a good subject and issue description. I am working on a dynamic binding to the mantle32.dll following DerelictGL and DerelictCl. Functions in the mantle dll use structs as argument types. I would like to define those structs in a module derelict.mantle.types ( as in DerelictGL/CL ) and import them in module derelict.mantle.mantle. Module mantle does have the required hooks: import DerelictUtil; import some_other_module_which_needs_the_struct_definitions; // struct definitions required class DerelictMantleLoader : SharedLibLoader { ... } Unfortunatelly I get an optilink error telling me that the required structs are undefined. Putting the struct definitions back into module derelict.mantle.mantle does work, but this is not an option as it will lead to cross import issues later on. The structs are also required in module some_other_module_... and that module ( which should handle mantleExtensions ) must also be imported in module derelict.mantle.mantle. I tried using opaque structs in module mantle which also didn't work. How can I have those structs have defined in a different module than that which is linking to the dll ? Regards, ParticlePeter
Re: Derlict binding with out_of_link_module struct definitions
On Sunday, 7 June 2015 at 12:12:16 UTC, Rikki Cattermole wrote: On 7/06/2015 11:53 p.m., ParticlePeter wrote: Wow, sometimes tough to find a good subject and issue description. I am working on a dynamic binding to the mantle32.dll following DerelictGL and DerelictCl. Functions in the mantle dll use structs as argument types. I would like to define those structs in a module derelict.mantle.types ( as in DerelictGL/CL ) and import them in module derelict.mantle.mantle. Module mantle does have the required hooks: import DerelictUtil; import some_other_module_which_needs_the_struct_definitions; // struct definitions required class DerelictMantleLoader : SharedLibLoader { ... } Unfortunatelly I get an optilink error telling me that the required structs are undefined. Putting the struct definitions back into module derelict.mantle.mantle does work, but this is not an option as it will lead to cross import issues later on. The structs are also required in module some_other_module_... and that module ( which should handle mantleExtensions ) must also be imported in module derelict.mantle.mantle. I tried using opaque structs in module mantle which also didn't work. How can I have those structs have defined in a different module than that which is linking to the dll ? Regards, ParticlePeter Is the source code so far up somewhere with the errors being given? Nope, only a working version with all the stuff in one module, git DerelictMantle.
Re: Derlict binding with out_of_link_module struct definitions
On Sunday, 7 June 2015 at 12:28:37 UTC, ParticlePeter wrote: On Sunday, 7 June 2015 at 12:12:16 UTC, Rikki Cattermole wrote: On 7/06/2015 11:53 p.m., ParticlePeter wrote: Wow, sometimes tough to find a good subject and issue description. I am working on a dynamic binding to the mantle32.dll following DerelictGL and DerelictCl. Functions in the mantle dll use structs as argument types. I would like to define those structs in a module derelict.mantle.types ( as in DerelictGL/CL ) and import them in module derelict.mantle.mantle. Module mantle does have the required hooks: import DerelictUtil; import some_other_module_which_needs_the_struct_definitions; // struct definitions required class DerelictMantleLoader : SharedLibLoader { ... } Unfortunatelly I get an optilink error telling me that the required structs are undefined. Putting the struct definitions back into module derelict.mantle.mantle does work, but this is not an option as it will lead to cross import issues later on. The structs are also required in module some_other_module_... and that module ( which should handle mantleExtensions ) must also be imported in module derelict.mantle.mantle. I tried using opaque structs in module mantle which also didn't work. How can I have those structs have defined in a different module than that which is linking to the dll ? Regards, ParticlePeter Is the source code so far up somewhere with the errors being given? Nope, only a working version with all the stuff in one module, git DerelictMantle. I created a version tag-less branch with the described issue and linker problem: https://github.com/ParticlePeter/DerelictMantle/tree/ExtensionSupport A test project using DerelictMantle can be found here: https://github.com/ParticlePeter/deMantleDTriangle
Convert std.container.array to void[] and/or pass to OpenGL functions like glBuffer(Sub)Data
I use wrapper functions taking void[] arrays to forward them comfortably to mentioned OpenGL functions. This works with static and dynamic build in arrays, but I don't see a way how I could access (cast) the raw data of a std.container.array to forward it to these wrapper functions. std.array(Range)(Range r) does a copy which I would like to avoid. Any advice ?
Re: Convert std.container.array to void[] and/or pass to OpenGL functions like glBuffer(Sub)Data
On Wednesday, 17 June 2015 at 13:07:11 UTC, Alex Parrill wrote: On Wednesday, 17 June 2015 at 13:04:28 UTC, ParticlePeter wrote: I use wrapper functions taking void[] arrays to forward them comfortably to mentioned OpenGL functions. This works with static and dynamic build in arrays, but I don't see a way how I could access (cast) the raw data of a std.container.array to forward it to these wrapper functions. std.array(Range)(Range r) does a copy which I would like to avoid. Any advice ? Try slicing it: `array[]` That gives me a range, which I could convert to an array with std.array(Range)(Range r) which I want to avoid.
Re: Convert std.container.array to void[] and/or pass to OpenGL functions like glBuffer(Sub)Data
On Wednesday, 17 June 2015 at 13:31:21 UTC, Marc Schütz wrote: On Wednesday, 17 June 2015 at 13:04:28 UTC, ParticlePeter wrote: I use wrapper functions taking void[] arrays to forward them comfortably to mentioned OpenGL functions. This works with static and dynamic build in arrays, but I don't see a way how I could access (cast) the raw data of a std.container.array to forward it to these wrapper functions. std.array(Range)(Range r) does a copy which I would like to avoid. Any advice ? Would this work? (&arr.front)[0 .. arr.length] Unfortunately not: Error: Vector!(float, 3) delegate() pure nothrow @property ref @safe cannot be sliced with [] Fyi, its an std.container.array!( gl3n.Vector!(float, 3) ) array.
Re: Convert std.container.array to void[] and/or pass to OpenGL functions like glBuffer(Sub)Data
On Wednesday, 17 June 2015 at 13:58:09 UTC, Kagamin wrote: (&arr.front())[0 .. arr.length] ? Yes, this works, nice, thanks :-)
How to partially forward properties of struct array member to struct (disable length property) ?
I am working on a struct vector. The data is stored in a member static array and I want to be able to forward all array properties except length to vector. Reason is I have free functions f that take vector(s) as arguments, such that f(vector) and vector.f via UFCS is possible. Using alias array this in the case of length function/array property is problematic, as length(vector) obviously uses the free function but vector.length the array property. What would be the simplest way to disable the array.length property for the vector struct? I would prefer not to implement length as a vector member function and call it inside the free function as this is inconsistent with the other free funcs.
Re: How to partially forward properties of struct array member to struct (disable length property) ?
On Sunday, 6 September 2015 at 08:48:32 UTC, bioinfornatics wrote: On Sunday, 6 September 2015 at 07:34:36 UTC, ParticlePeter wrote: I am working on a struct vector. The data is stored in a member static array and I want to be able to forward all array properties except length to vector. Reason is I have free functions f that take vector(s) as arguments, such that f(vector) and vector.f via UFCS is possible. Using alias array this in the case of length function/array property is problematic, as length(vector) obviously uses the free function but vector.length the array property. What would be the simplest way to disable the array.length property for the vector struct? I would prefer not to implement length as a vector member function and call it inside the free function as this is inconsistent with the other free funcs. Hi, If you are looking for somethin like delegator from ruby: http://ruby-doc.org/stdlib-2.0.0/libdoc/forwardable/rdoc/Forwardable.html No, not this one. It is O.k. for vector to be implicitly converted to an array, and in such a case array property length should be used. Or by using mixin delegates: http://forum.dlang.org/post/jitn9v$20u4$1...@digitalmars.com I think this approach (in particular Jacobs suggestion) would be useful if I would like to forward array member properties for lot of different struct/classes but for now it is sufficient for the vector struct. I took a look at opIndex, opSlice, opIndexAssign and opIndexOpAssign but find the examples very confusing. Also its not clear for me which of these operators I have to implement to have full array functionality on the vector struct. In the end all that I want is "just" to disable access to array.length through vector and alias this array.
Re: How to partially forward properties of struct array member to struct (disable length property) ?
On Sunday, 6 September 2015 at 10:24:25 UTC, Marc Schütz wrote: Untested: struct Vector(T) { T[42] data; auto opDispatch(string func, Args...)(Args args) if(is(typeof(mixin("data."~func)(Args.init))) && func != "length") { return mixin("data."~func)(Args.init); } } Unfortunately not working, afaics the built in static array does not have the property opIndex et al. so they cannot be forwarded, right?
Confusion about dynamically and lexically scoped closures
Hi, the confusion starts here: http://dlang.org/function.html#closures End of paragraph bellow the last delegate example: "This combining of the environment and the function is called a dynamic closure." While according to https://en.wikipedia.org/wiki/Scope_%28computer_science%29 "Lexical scope vs. dynamic scope" I would call D delegates lexical closures, as the scope of the callee (scope where callee is defined) is used for free variable lookup, isn't that right? Confusion is enhanced with this Dlang wiki: http://wiki.dlang.org/Function_literals "This has brought up the specter of Dynamic Closures. The nested and/or anonymous functions can only access dynamic scope, which means scope that exists on the stack at the time of execution. This differs from lexical scope, which refers to the scope as indicated by the program text structure." So what's what now? Cheers, ParticlePeter
Re: Confusion about dynamically and lexically scoped closures
On Sunday, 8 November 2015 at 23:17:06 UTC, Jakob Ovrum wrote: The closures for delegates in D1 are never automatically copied to the heap, while in D2 this is done when it's determined that the delegate might outlive one of its upvalues. So, I think it's safe to say we have lexical closures in D2 but only dynamic closures in D1 and the language specification is out of date. Thanks, makes sens, I assumed a typo in the docs.
Re: Anyone using glad?
On Monday, 11 January 2016 at 00:46:38 UTC, Jason Jeffory wrote: ... OK, I'll give it a try. What about GLUT and WGL? Whats the difference between them all and glfw? Are all these just OS helpers to reduce the boilerplate code? These kind of questions are best clarified on the OpenGL wiki. https://www.opengl.org/wiki/Main_Page Especially these (Glad is explained there as well): https://www.opengl.org/wiki/Getting_Started https://www.opengl.org/wiki/OpenGL_Loading_Library https://www.opengl.org/wiki/Related_toolkits_and_APIs
How to declare an alias to a function literal type
I have a function type and variable and assign a function to it: void function( int i ) myFunc; myFunc = void function( int i ) { myCode; } How would I declare an alias for void function( int i ) such that the case above would work like this: // alias MF = void function( int i ); // not working // alias void function( int i ) MF; // not working MF myFunc; myFunc = MF { myCode }; Please, if possible, also show me where I should have found the answer (D Reference, Alis book, etc. )
Re: How to declare an alias to a function literal type
On Tuesday, 12 January 2016 at 15:57:03 UTC, Marc Schütz wrote: On Tuesday, 12 January 2016 at 15:41:02 UTC, ParticlePeter wrote: I have a function type and variable and assign a function to it: void function( int i ) myFunc; myFunc = void function( int i ) { myCode; } How would I declare an alias for void function( int i ) such that the case above would work like this: // alias MF = void function( int i ); // not working // alias void function( int i ) MF; // not working MF myFunc; myFunc = MF { myCode }; Please, if possible, also show me where I should have found the answer (D Reference, Alis book, etc. ) This works for me: alias MF = void function(int i); // works fine - what was your error? void main() { import std.stdio; MF myFunc; // you can also use the full `function(int i) { ... }` in the next line myFunc = (i) { writeln("i = ", i); }; myFunc(42); } Not what I wanted, I wanted the parameter to be part of the alias: myFunc = MF { ... } I want to pass such a function to another function: alias MF = void function(int i); void otherFunc( void function( int ) mf ); otherFunc( MF { ... } ); // Getting Error: found '{' when expecting ',' Actually, I do use only one param, and not int as well, hence I would like the parameter list to be part of the alias. Your example works though.
Re: How to declare an alias to a function literal type
On Tuesday, 12 January 2016 at 16:00:37 UTC, Daniel Kozak wrote: V Tue, 12 Jan 2016 15:41:02 + ParticlePeter via Digitalmars-d-learn napsáno: I have a function type and variable and assign a function to it: void function( int i ) myFunc; myFunc = void function( int i ) { myCode; } How would I declare an alias for void function( int i ) such that the case above would work like this: // alias MF = void function( int i ); // not working // alias void function( int i ) MF; // not working MF myFunc; myFunc = MF { myCode }; Please, if possible, also show me where I should have found the answer (D Reference, Alis book, etc. ) alias void MF(int i); That does not work: alias void MF(int i); MF mf; // Error: variable mf cannot be declared to be a function
Re: How to declare an alias to a function literal type
On Tuesday, 12 January 2016 at 16:22:48 UTC, ParticlePeter wrote: Actually, I do use only one param, and not int as well, hence I would like the parameter list to be part of the alias. Your example works though. This was confusing, lets start fresh: I have a function "otherFunc" which takes a function with lots of parameters as argument: void otherFunc( void function( ref int p1, float p2, ubyte p3, ... ) mf ); Side-note, I use the keyword function to signal that it is a function and not a delegate, thought it is a delegate when not specified. When I pass a parameter to otherFunc I use this syntax for an anonymous function parameter: otherFunc( void function( ref int p1, float p2, ubyte p3 ) { myCode; } ); I would like to alias the function signature above: alias MF = void function( ref int p1, float p2, ubyte p3 ); I can rewrite the definition of otherFunc like this: void otherFunc( MF mf ); But I cannot pass an anonymous function to otherFunc like this: otherFunc( MF { myCode; } ); Thats what I want. Any working example? Ali, I do not pass an existing named function as a pointer. I am also not sure about the lambdas, as I do not return anything, I just want to process data, would that work?
Re: How to declare an alias to a function literal type
On Tuesday, 12 January 2016 at 17:28:35 UTC, Marc Schütz wrote: On Tuesday, 12 January 2016 at 16:55:48 UTC, ParticlePeter wrote: [...] If I understand you correctly (not sure), you would like to write `MF` so that you don't need to specify the parameters in the lambda? That's not possible, because the code inside the lambda needs names for them if it wants to access them, but parameter names are _not_ part of the function type, and therefore the alias doesn't know about them. However, you don't need to specify the full parameter list in the lambda, the names and `ref` are enough: otherFunc( (ref a, ref b, ref c) { /* use a, b, c */ } ); This is already quite useful, thanks.
Re: How to declare an alias to a function literal type
On Tuesday, 12 January 2016 at 17:03:49 UTC, Ali Çehreli wrote: On 01/12/2016 08:55 AM, ParticlePeter wrote: > I have a function "otherFunc" which takes a function with lots of > parameters as argument: > > void otherFunc( void function( ref int p1, float p2, ubyte p3, ... ) mf ); Ok. > otherFunc( void function( ref int p1, float p2, ubyte p3 ) { myCode; } ); Ok. > alias MF = void function( ref int p1, float p2, ubyte p3 ); Ok. > I can rewrite the definition of otherFunc like this: > void otherFunc( MF mf ); That has the same problem of trying to do this for int: void foo(int i) { } void main() { foo(int 42); // <-- ERROR } But you can do this: foo(int(42));// (Relatively new syntax in D.) > But I cannot pass an anonymous function to otherFunc like this: > otherFunc( MF { myCode; } ); It works with the parentheses as it does for int: alias MF = void function( ref int p1, float p2, ubyte p3 ); void otherFunc( MF mf ) { } void main() { otherFunc(MF((ref int, float, ubyte){ }));// <-- Parens } O.K. so I conclude that writing: void main() { otherFunc(MF { }); } is not possible. At least not with alias, maybe with templates or mixins? In essence something like C #define as in: #define MF function( ref int p1, float p2, ubyte p3 ) Is there some such way?