Re: Temporary file creation for unittests
On Friday, 18 May 2018 at 15:16:52 UTC, Russel Winder wrote: Hi, What's the current official position on how to create temporary files for use during a unittest. I found https://github.com/dlang/phobos/pull/5788 but it seems to be languishing in the "we have discussed all the issues that no-one will ever have a problem with" phase. What to do between now and when there is an LDC release that has the result of the merge? You could use std.file.deleteme, which is what the Phobos unittests use: https://github.com/dlang/phobos/blob/master/std/file.d#L117
Re: C style callbacks fix for member callbacks
I tried this. Your code crashes in windows dmd x86 x64. Hm. Works for me in a virtual machine. But I'm not surprised that it's fragile. It might be completely wrong, and it just happens to look alright on my machine.
Re: C style callbacks fix for member callbacks
On Sunday, 20 May 2018 at 23:05:47 UTC, ag0aep6g wrote: On 05/20/2018 06:48 PM, IntegratedDimensions wrote: alias callback = extern(C) int function(const(void) a, void *b, uint c, void* context); (I'm assuming that `a` is supposed to be a `const(void)*`.) Where context acts as this. I would like to assign a D method to this callback. class { callback c; /*extern(C) static*/ int foo(const(void) a, void *b, uint c, void* context); this() { c = cast(callback)&foo; } } Unless I'm misunderstanding it, the spec seems to say that the `this` pointer is passed as if it was an additional parameter past the last one [1]. But that doesn't seem to be true in the implementation. At least on Linux x86-64, `this` seems to be a hidden first parameter. So when a method is called as a `callback`, `a` becomes `this`, `b` becomes the first explicit parameter, `c` the second, and `context` the third. So this works: import std.stdio; alias Callback = extern(C) int function(const(void)* a, void* b, uint c, void* context); class C { int field = 43; extern(C) int foo(void* b, uint c, C this_) { const(void)* a = cast(void*) this; writeln(a, " ", b, " ", c, " ", this_.field); return 0; } } void main() { void* a = new int; void* b = new int; uint c = 42; auto obj = new C; Callback cb = cast(Callback) (&obj.foo).funcptr; cb(a, b, c, cast(void*) obj); writeln(a, " ", b, " ", c, " ", obj.field); /* For comparison. Should print the same. */ } This is all very hacky, of course. And I don't really know what I'm doing there. So obviously, I don't recommend doing this. But other than hacking it like that, I don't think you can pass a method as a `callback` directly. [1] https://dlang.org/spec/abi.html#parameters I tried this. Your code crashes in windows dmd x86 x64. It really shouldn't be hacky. The only difference is the "this" is implicit normally when in this case it is explicit and possibly in a different location than one expects.
Re: C style callbacks fix for member callbacks
On 05/20/2018 06:48 PM, IntegratedDimensions wrote: alias callback = extern(C) int function(const(void) a, void *b, uint c, void* context); (I'm assuming that `a` is supposed to be a `const(void)*`.) Where context acts as this. I would like to assign a D method to this callback. class { callback c; /*extern(C) static*/ int foo(const(void) a, void *b, uint c, void* context); this() { c = cast(callback)&foo; } } Unless I'm misunderstanding it, the spec seems to say that the `this` pointer is passed as if it was an additional parameter past the last one [1]. But that doesn't seem to be true in the implementation. At least on Linux x86-64, `this` seems to be a hidden first parameter. So when a method is called as a `callback`, `a` becomes `this`, `b` becomes the first explicit parameter, `c` the second, and `context` the third. So this works: import std.stdio; alias Callback = extern(C) int function(const(void)* a, void* b, uint c, void* context); class C { int field = 43; extern(C) int foo(void* b, uint c, C this_) { const(void)* a = cast(void*) this; writeln(a, " ", b, " ", c, " ", this_.field); return 0; } } void main() { void* a = new int; void* b = new int; uint c = 42; auto obj = new C; Callback cb = cast(Callback) (&obj.foo).funcptr; cb(a, b, c, cast(void*) obj); writeln(a, " ", b, " ", c, " ", obj.field); /* For comparison. Should print the same. */ } This is all very hacky, of course. And I don't really know what I'm doing there. So obviously, I don't recommend doing this. But other than hacking it like that, I don't think you can pass a method as a `callback` directly. [1] https://dlang.org/spec/abi.html#parameters
Re: Convert mixin to function call
It should be obvious that these are simplifications. I can't pass the variables directly as parameters as in the real case the names may only be partially specified.
Re: Convert mixin to function call
https://dpaste.dzfl.pl/fb49bf834cff import std.stdio; auto Q(string A)() { auto foo() { auto d = mixin(Z!(A)()); return d; } return foo()(); } auto X(string A, string N)() { return N~" = (() { int y = 4; return "~A~" + y + 5; })();"; } auto Z(string A)() { return "() { int y = 4; return "~A~" + y + 5; }"; } void main() { int x = 3; double y = 0; pragma(msg, (X!("x", "y")())); mixin(X!("x", "y")()); y = Q!("x")(); writeln(y); } using Q is more ideomatic but foo does not have access to x which is required(since Q should behave like a string mixin as far as scope is concerned).
Re: Convert mixin to function call
Also, one thing that would help would be able to create identifier names that are unique to avoid collisions. Does D have any ability to do such a thing?
Convert mixin to function call
I have a string mixin that returns a value or function that uses the mixed in scope. Currently I have to wrap the mixin in a delegate or local function as to be able to get the value: int x = 3; int y = 1; auto foo() { mixin(X!("x")); } This allows the the mixin to see the scope but keep the mixin variables local so they do not shadow anything unintentionally. auto X(string A)() { return "int y = 4; return "~A~" + y + 5;"; } foo will just return 8 but I cannot simply mixin within the main scope or the scope will return unintentionally. int x = 3; mixin(X!("x")) // <- hidden return Using foo works fine but adds a level of indirection I feel is unnecessary. The only thing I feel can work is to rewrite X so that it uses variables with random names to reduce shadowing issues and to allow X to take a variable name to put the result in: auto X(string A, string N)() { return "{ int _342sdfs = 4; "~N~" = "~A~" + _342sdfs + 5;}"; } which complicates things significantly. My hope is that D would inline the function call. Ideally I'd like to be able to sort of use the mixin as a function and simply call it: val = mixin(X!("x"))(); which I could do by internally using a delegate inside the mixin auto X(string A)() { return "(() { int y = 4; return "~A~" + y + 5; })"; } except that the mixin syntax does not allow one to use it in an expression. Remember that the mixin must be able to see the scope it is mixed in at so one can't wrap this in a template use it as far as I know? Although, one can do mixin(X!("val", "x")); which does the ugly work. It would be nice to be able to get this process to look as much like normal D code as possible. the idea is to try to write "local" string mixins that are messy and not "C'ish" looking in to "C'ish" looking code.
Re: auto & class members
On 2018-05-20 17:40:39 +, Robert M. Münch said: Hi Jonathan, great! This got me a step further. So I can declare my member now. But I get an implict cast error when I try: class a { ... myStream; } class b { typeof(a.myStream.filter!(x => x == myMessage)) mySubStream; } void myFunc() { a myA = new a(); b myB = new b(); myB.mySubstream = myA.myStream.filter!(x => x == myMessage); } This gives (unnecessary stuff stripped): Error: cannot implicitly convert expression filter(...) of type app.myFunc.filter!(x => x == myMessage) to app.b.filter!(x => x == myMessage) Answering myself: Using an alias helps. alias typeof(a.myStream.filter!(x => x == myMessage)) myMessageType; class b { myMessageType mySubStream; } void myFunc() { a myA = new a(); b myB = new b(); myB.mySubstream = cast(myMessageType)myA.myStream.filter!(x => x == myMessage); } But I still don't understand why I can't write things explicitly but have to use an alias for this. -- Robert M. Münch http://www.saphirion.com smarter | better | faster
Re: auto & class members
On 2018-05-20 14:49:59 +, Jonathan M Davis said: In cases like this, typeof is your friend. e.g. something like typeof(myStream.filter!(a => a == myMessage)) mySubStream; Hi Jonathan, great! This got me a step further. So I can declare my member now. But I get an implict cast error when I try: class a { ... myStream; } class b { typeof(a.myStream.filter!(x => x == myMessage)) mySubStream; } void myFunc() { a myA = new a(); b myB = new b(); myB.mySubstream = myA.myStream.filter!(x => x == myMessage); } This gives (unnecessary stuff stripped): Error: cannot implicitly convert expression filter(...) of type app.myFunc.filter!(x => x == myMessage) to app.b.filter!(x => x == myMessage) Why is myFunc now entering the game? I mean it's just the function containing the code. It seems that: typeof(myA.myStream.filter!(x => x == myMessage)) and typeof(a.myStream.filter!(x => x == myMessage)) are not the same. But inside class b I can't use a specific variable instance. And in myFunc, I can't use a class type but need the specific instance. Any further idea? though you might have trouble with the lambda being subtly different type even if you replace myMessage in it with something that will work in the scope that mySubStream is being declared. Not sure if the above problem is exactly what you mention here. This is all pretty tricky. However, that could be fixed by doing something like replacing the lambda with a free function or just creating a function that returns what you want to assign to mySubStream. Then you could just do something like typeof(myHelperFunc(myStream)) mySubStream; The exact solution can get a bit annoying in cases like this (it's arguably the biggest downside to auto returns), but typeof does provide a way out if you can get the expression to it to work. It is easier with local variables than member variables though, since you don't necessarily have everything you want to use in the expression that will give the variable its value available at the point that the variable is declared - but that's why a helper function can help, since it provides a way to encapsulate the expression and reuse it between the assignment and the declaration. Not sure I understand every aspect but it's getting clearer... Thanks so far. -- Robert M. Münch http://www.saphirion.com smarter | better | faster
Re: C style callbacks fix for member callbacks
On Sunday, 20 May 2018 at 08:40:57 UTC, MGW wrote: On Saturday, 19 May 2018 at 23:52:58 UTC, IntegratedDimensions wrote: I have a member callback that I want to use as a C callback. http://www.agner.org/optimize/calling_conventions.pdf https://www.youtube.com/watch?v=xhDS377mAc4 Sorry, I can't understand Russian(wish I could!). It also does not seem applicable for my problem. Although It is a useful idea here(using D in C++). alias callback = extern(C) int function(const(void) a, void *b, uint c, void* context); Where context acts as this. I would like to assign a D method to this callback. class { callback c; /*extern(C) static*/ int foo(const(void) a, void *b, uint c, void* context); this() { c = cast(callback)&foo; } }
Mysql query result access by field name
I have started learning D and I am enjoying it so far. How does one access the columns fields in a Mysql query results by the column name. Currently I have to use the method as shown in a couple of example by indexing array values (f being a struct variable): Data f; f.name = to!string(allrows[0][0]); f.surname = to!string(allrows[0][1]); f.title = to!string(allrows[0][2]); I am using the mysql-native package or DB connectivity. Regards
Re: auto & class members
On Sunday, May 20, 2018 16:30:10 Robert M. Münch via Digitalmars-d-learn wrote: > I use the D RX lib [1] and can create a filtered stream using the auto > keyword: > > struct a { > SubjectObject!myType myStream; > ??? mySubStream; > } > > void myfunc(){ > a myA = new a(); > > auto mySubStream = a.myStream.filter!(a => a == myMessage); > ... > } > > The problem is, that I don't find out what the type of mySubStream is, > which I would like to make a member of the struct, so that I can > reference it outside the function too. > > How can I find out the type of an auto? This here seems to be a pretty > complicated templated, nested type, whatever result. > > [1] https://github.com/lempiji/rx In cases like this, typeof is your friend. e.g. something like typeof(myStream.filter!(a => a == myMessage)) mySubStream; though you might have trouble with the lambda being subtly different type even if you replace myMessage in it with something that will work in the scope that mySubStream is being declared. However, that could be fixed by doing something like replacing the lambda with a free function or just creating a function that returns what you want to assign to mySubStream. Then you could just do something like typeof(myHelperFunc(myStream)) mySubStream; The exact solution can get a bit annoying in cases like this (it's arguably the biggest downside to auto returns), but typeof does provide a way out if you can get the expression to it to work. It is easier with local variables than member variables though, since you don't necessarily have everything you want to use in the expression that will give the variable its value available at the point that the variable is declared - but that's why a helper function can help, since it provides a way to encapsulate the expression and reuse it between the assignment and the declaration. - Jonathan M Davis
auto & class members
I use the D RX lib [1] and can create a filtered stream using the auto keyword: struct a { SubjectObject!myType myStream; ??? mySubStream; } void myfunc(){ a myA = new a(); auto mySubStream = a.myStream.filter!(a => a == myMessage); ... } The problem is, that I don't find out what the type of mySubStream is, which I would like to make a member of the struct, so that I can reference it outside the function too. How can I find out the type of an auto? This here seems to be a pretty complicated templated, nested type, whatever result. [1] https://github.com/lempiji/rx -- Robert M. Münch http://www.saphirion.com smarter | better | faster
Re: Can I infer the type from this?
On Sunday, 20 May 2018 at 03:16:39 UTC, Dr.No wrote: Oh, my bad: I totally forgot a crucial thing on question: I want this to work with a static member, for example, call myTemp like this myTemp!(C.a) I don't mind if I to pass the type as parameter somehow, like myTemp!(C, C.a) or myTemp!(C)(C.a) but I do need to pass a symbol as parameter, hence I'm using alias template parameter. Still not sure, if I get you right... ´´´ import std.stdio; void main() { auto res = myTemp!(C.s); } class C { static size_t s; } template myTemp(alias s) { enum myTemp = templateFunction!(typeof(s))(s.stringof); } int templateFunction(T)(string targetMembername) { static assert(is(T == size_t)); assert(targetMembername == "s"); return 42; } ´´´ In general, I'm not sure, how you want to get a type of a general symbol (this is what templateFunction should do, isn't it?) as it could be a template or a value parameter for example. However, I tried these cases, and typeof yields something reasonable then. In particular, it still did compile and yields something which could be checked against...
Re: C style callbacks fix for member callbacks
On Saturday, 19 May 2018 at 23:52:58 UTC, IntegratedDimensions wrote: I have a member callback that I want to use as a C callback. http://www.agner.org/optimize/calling_conventions.pdf https://www.youtube.com/watch?v=xhDS377mAc4
Re: Examples/tutorials for OpenGL games which works out-of-the-box on linux
On 20/05/2018 8:06 PM, Prokop Hapala wrote: Hi, I'm looking for examples of OpenGL games which I can use as templates for making my own stuff. But I'm quite discouraged that everything I found on github is probably outdated and fails to compile. Since I'm not very familiar with dub environment I don't feel able to correct the errors in dependences etc. e.g.: * https://github.com/Circular-Studios/Dash (depends on old version of vibe.d, when update the dub.json it gives this error: source/dash/components/mesh.d(8,19): Error: module `stream` is in file 'std/stream.d' which cannot be read Not a dub issue, the module was removed from Phobos. * https://github.com/d-gamedev-team/opengl-tutorials - depends on some libs like `glad-drey,glfw-drey,glwtf-dray` which are perhaps old versions of Derelict bindings, but I won't try to set it up Not registered on dub repo. I don't think it was ever completed. * https://github.com/Extrawurst/unecht - There is no setup/build readme, when I just hit `dub run` I get errors about missing parts No package file found in /home/prokop/git_SW/_Dlang/unecht/submodules/tharsis.prof/, expected one of dub.json/dub.sdl/package.json $ dub build --config=app Should work for the application. Tharsis.prof does have a dub file. Did you clone with the submodules (requiring sub modules is not ok for dub just an FYI)?
Examples/tutorials for OpenGL games which works out-of-the-box on linux
Hi, I'm looking for examples of OpenGL games which I can use as templates for making my own stuff. But I'm quite discouraged that everything I found on github is probably outdated and fails to compile. Since I'm not very familiar with dub environment I don't feel able to correct the errors in dependences etc. e.g.: * https://github.com/Circular-Studios/Dash (depends on old version of vibe.d, when update the dub.json it gives this error: source/dash/components/mesh.d(8,19): Error: module `stream` is in file 'std/stream.d' which cannot be read * https://github.com/d-gamedev-team/opengl-tutorials - depends on some libs like `glad-drey,glfw-drey,glwtf-dray` which are perhaps old versions of Derelict bindings, but I won't try to set it up * https://github.com/Extrawurst/unecht - There is no setup/build readme, when I just hit `dub run` I get errors about missing parts No package file found in /home/prokop/git_SW/_Dlang/unecht/submodules/tharsis.prof/, expected one of dub.json/dub.sdl/package.json