Re: Converting Lua source to D
On Thursday, 5 March 2020 at 16:54:35 UTC, AB wrote: I am only guessing, but I think the problem is line 87. Arrays and slices in D contain a length field and thus do not need to be null terminated. The foreach at line 96 iterates on all valid indices and thus in the last iteration you call luaL_requiref(L, null, null, 1). Try changing static const luaL_Reg[] loadedlibs = [ ... {LUA_DBLIBNAME, &luaopen_debug}, {null, null} ]; to static const luaL_Reg[] loadedlibs = [ ... {LUA_DBLIBNAME, &luaopen_debug} ]; I knew I was blind, thank you. Segfault is gone. Now I should look at getting the CI up and Test failure fixed.
Re: DMD: Is it possible change compile time errors to runtime errors in Dlang?
On 3/6/20 5:59 AM, Marcone wrote:> On Friday, 6 March 2020 at 05:31:57 UTC, Mathias Lang wrote: > I'm coming from Python The fact that errors are delayed until run time is a Python deficiency. We accept that deficiency because we also accept claimed benefits that Python brings. If you want to skip the compilation step, you unfortunately can not, but you can combine compilation and execution steps with 'rdmd', which may make it look like the errors are caught at run time: https://dlang.org/rdmd.html Ali
Re: How to dispatch a class function for an object accessed by handle?
On Friday, 6 March 2020 at 15:19:39 UTC, Adam D. Ruppe wrote: On Friday, 6 March 2020 at 15:05:56 UTC, wjoe wrote: But didn't like the string part and that's when I introduced the alias fn because I figured maybe it's possible to do something like: factory.dispatch!(Bitmap.load)(handle, path); and get the Bitmap part from that alias and hence save the duplicate Bitmap type in factory.dispatch!(Bitmap, Bitmap.load)(...); ooh you can do that. on the alias, use __traits(identifier) to get thef ucntion name and __traits(parent) to get the class name! then it reduces to what we already wrote. Awesome! I read the docs up and down but I couldn't figure it out. Thank you!
Re: DMD: Is it possible change compile time errors to runtime errors in Dlang?
On Friday, 6 March 2020 at 04:56:28 UTC, Marcone wrote: Is it possible change compile time errors to runtime errors in Dlang? no If yes, how can I make it? if you deactivate all the errors emitted during the semantic then there are very good chance that the compiler crashes while generating code.
Re: Nice readable code with initializer
On Thursday, 5 March 2020 at 07:01:27 UTC, Виталий Фадеев wrote: On Thursday, 5 March 2020 at 06:48:53 UTC, Виталий Фадеев wrote: Searching for beauty code implementation. Goal is: Create new object with "custom initializer". "custom initializer" - is function, executed in _ctor, in object scope. Main Goal is: "clean readable beauty code". Like this and better: class DataGrid : Base { this() { super(); CR!VBoxLayout({ sizeWidthMode = SIZE_MODE.PARENT; sizeHeightMode = SIZE_MODE.PARENT; CR!GridLayout({ id = "grid"; sizeWidthMode = SIZE_MODE.PARENT; sizeHeightMode = SIZE_MODE.FIXED; h = 400; }); CR!HCenterLayout({ CR!(Pager!TextButton) ({ id = "pager"; }); }); }); } } class Base { T CR( T, F )( F func ) { auto c = new T(); c.Apply( func ); return c; } void Apply( F )( F func ) { func(); } } In code above created tree and configured nodes. Trouble in code above is: "custom initializer" - executed in DataGrid context. How to execute "custom initializer" in VBoxLayout context and keep readable beauty ? Conceptual question. How to implement next pseudo-code in D ? VBoxLayout sizeWidthMode = SIZE_MODE.PARENT sizeHeightMode = SIZE_MODE.PARENT GridLayout id = "grid" sizeWidthMode = SIZE_MODE.PARENT sizeHeightMode = SIZE_MODE.FIXED h = 400 HCenterLayout Pager!TextButton id = "pager" Thank! Now using like this: with ( CR!VBoxLayout ) { sizeWidthMode = SIZE_MODE.PARENT; sizeHeightMode = SIZE_MODE.PARENT; with ( CR!GridLayout ) { id = "grid"; sizeWidthMode = SIZE_MODE.PARENT; sizeHeightMode = SIZE_MODE.FIXED; h = 400; } with ( CR!HCenterLayout ) { with ( CR!( Pager!TextButton ) ) { id = "pager"; } } }
Re: How to dispatch a class function for an object accessed by handle?
On Friday, 6 March 2020 at 15:05:56 UTC, wjoe wrote: But didn't like the string part and that's when I introduced the alias fn because I figured maybe it's possible to do something like: factory.dispatch!(Bitmap.load)(handle, path); and get the Bitmap part from that alias and hence save the duplicate Bitmap type in factory.dispatch!(Bitmap, Bitmap.load)(...); ooh you can do that. on the alias, use __traits(identifier) to get thef ucntion name and __traits(parent) to get the class name! then it reduces to what we already wrote.
Re: How to dispatch a class function for an object accessed by handle?
On Friday, 6 March 2020 at 14:14:04 UTC, Adam D. Ruppe wrote: On Friday, 6 March 2020 at 14:05:55 UTC, Steven Schveighoffer wrote: Adam's way doesn't work either, because the call doesn't use the alias, but just instantiates opDispatch with the new name!' oh yikes, how did I not notice that?! so yeah just kinda screwed. I'd probably suggest at tis point having the opDispatch be a trivial implementation that just forwards to another named method. struct A { template opDispatch(string name) { auto opDispatch(T, Args...)(Args args) { return other_name!(name, T, Args)(args); } } auto other_name(string name, T, Args...)(Args args) { // real implementation } } and then to test it externally you do a.other_name!("whatever", Bitmap)(args, here); This isn't the worst thing to use and since it's just for testing it's fine. I came up with a similar interface like a.other_name!("whatever", Bitmap)(args, here); after discarding .opDispatch() and I called the thing .dispatch(T, string fn, ARGS...)(...). But didn't like the string part and that's when I introduced the alias fn because I figured maybe it's possible to do something like: factory.dispatch!(Bitmap.load)(handle, path); and get the Bitmap part from that alias and hence save the duplicate Bitmap type in factory.dispatch!(Bitmap, Bitmap.load)(...); Anyways thanks for your help.
Re: How to dispatch a class function for an object accessed by handle?
On Friday, 6 March 2020 at 13:55:25 UTC, Steven Schveighoffer wrote: On 3/6/20 6:51 AM, wjoe wrote: On Thursday, 5 March 2020 at 18:33:41 UTC, Adam D. Ruppe wrote: On Thursday, 5 March 2020 at 14:24:33 UTC, wjoe wrote: [...] template opDispatch(string name) { auto opDispatch(T, Args...)(Args args) { ... } } [...] NOTE: opDispatch suppresses internal compile errors, it will just say "no such property whatever". you can explicitly instantiate with `f.opDispatch!"whatever` to help see better errors. Follow-up question: Calling f.whatever!SomeResource(...); works no problem. However I can't figure out how to call a function by explicitly instantiating opDispatch. Since f.opDispatch!"load"(handle, "wallpaper.png"); doesn't compile, I refreshed my memory about the shortcut syntax and the eponymous syntax and the way I read it is that this is a template of a template. So I tried this: f.opDispatch!"load".opDispatch!Bitmap(handle, "path/to/wallpaper.png"); This doesn't work, because an eponymous template does not provide access to the internals of the template. But this doesn't compile either and errors out with: Error: Cannot resolve type for f.opDispatch(T, ARGS...)(ResourceHandle handle, ARGS args) I don't understand this error message. Which type can't be resolved? Is there a way to look at output of what the compiler generates for f.whatever!SomeResource(...); ? You can use -vcg-ast, but this isn't necessarily going to be compilable code. D doesn't allow chained instantiation (i.e. (A!B)!C), so you need to use either a mixin or a helper: import std.meta; enum fname = "load"; Instantiate!(f.opDispatch!fname, Bitmap)("path/to/wallpaper.png") or mixin("f." ~ fname ~ "!(Bitmap)(...);"); I'm assuming fname is given to you as a compile-time string and that's why you'd need to run opDispatch manually. -Steve I tried Instantiate this morning after I found a reply you made to someone else who was trying to chain instantiate. But for the reason you stated it didn't work. Funny you mention: mixin("f." ~ fname ~ "!(Bitmap)(...);"); Because that's like my initial implementation :) As for the the command line switch. My plan wasn't to copy paste. Sometimes I have a hard time to comprehend results because it's like I've put water, flour and eggs on the table. Then I'm presented with a loaf of bread and I'm baffled. Then I want to understand what happened between putting the ingredients on the table and the ready baked loaf. Anyways, thanks for your answers.
Re: How to dispatch a class function for an object accessed by handle?
On 3/6/20 9:14 AM, Adam D. Ruppe wrote: On Friday, 6 March 2020 at 14:05:55 UTC, Steven Schveighoffer wrote: Adam's way doesn't work either, because the call doesn't use the alias, but just instantiates opDispatch with the new name!' oh yikes, how did I not notice that?! so yeah just kinda screwed. I'd probably suggest at tis point having the opDispatch be a trivial implementation that just forwards to another named method. struct A { template opDispatch(string name) { auto opDispatch(T, Args...)(Args args) { return other_name!(name, T, Args)(args); } } Do this instead, I think this will work and avoids an extra call (and having to do the argument plumbing that inevitably comes with this kind of wrapping): template opDispatch(string name) { alias opdispatch(T) = other_name!(name, T); } template other_name(string name, T) { auto other_name(Args...)(Args args) { // real implementation } } -Steve
Re: How to dispatch a class function for an object accessed by handle?
On 3/6/20 9:42 AM, Steven Schveighoffer wrote: alias opdispatch(T) = other_name!(name, T); And obviously, this should be opDispatch with a capital D ! -Steve
Re: How to dispatch a class function for an object accessed by handle?
On Friday, 6 March 2020 at 14:05:55 UTC, Steven Schveighoffer wrote: Adam's way doesn't work either, because the call doesn't use the alias, but just instantiates opDispatch with the new name!' oh yikes, how did I not notice that?! so yeah just kinda screwed. I'd probably suggest at tis point having the opDispatch be a trivial implementation that just forwards to another named method. struct A { template opDispatch(string name) { auto opDispatch(T, Args...)(Args args) { return other_name!(name, T, Args)(args); } } auto other_name(string name, T, Args...)(Args args) { // real implementation } } and then to test it externally you do a.other_name!("whatever", Bitmap)(args, here);
Re: How to dispatch a class function for an object accessed by handle?
On 3/6/20 8:55 AM, Steven Schveighoffer wrote: Instantiate!(f.opDispatch!fname, Bitmap)("path/to/wallpaper.png") I realized, this doesn't work. Because f.opDispatch is a `this` call, but is not called that way in this case. Adam's way doesn't work either, because the call doesn't use the alias, but just instantiates opDispatch with the new name! I think the only solution might be the mixin. -Steve
Re: DMD: Is it possible change compile time errors to runtime errors in Dlang?
On 3/6/20 8:59 AM, Marcone wrote: On Friday, 6 March 2020 at 05:31:57 UTC, Mathias Lang wrote: On Friday, 6 March 2020 at 04:56:28 UTC, Marcone wrote: Is it possible change compile time errors to runtime errors in Dlang? If yes, how can I make it? No it's not possible, D is a statically typed language. Why would you want errors that can be caught at compile time to happen at runtimes ? I'm coming from Python Perhaps you can share an example of your trouble, and we can explain why it's better to have compiler errors in that case. -Steve
Re: How to dispatch a class function for an object accessed by handle?
On 3/6/20 6:51 AM, wjoe wrote: On Thursday, 5 March 2020 at 18:33:41 UTC, Adam D. Ruppe wrote: On Thursday, 5 March 2020 at 14:24:33 UTC, wjoe wrote: [...] template opDispatch(string name) { auto opDispatch(T, Args...)(Args args) { ... } } [...] NOTE: opDispatch suppresses internal compile errors, it will just say "no such property whatever". you can explicitly instantiate with `f.opDispatch!"whatever` to help see better errors. Follow-up question: Calling f.whatever!SomeResource(...); works no problem. However I can't figure out how to call a function by explicitly instantiating opDispatch. Since f.opDispatch!"load"(handle, "wallpaper.png"); doesn't compile, I refreshed my memory about the shortcut syntax and the eponymous syntax and the way I read it is that this is a template of a template. So I tried this: f.opDispatch!"load".opDispatch!Bitmap(handle, "path/to/wallpaper.png"); This doesn't work, because an eponymous template does not provide access to the internals of the template. But this doesn't compile either and errors out with: Error: Cannot resolve type for f.opDispatch(T, ARGS...)(ResourceHandle handle, ARGS args) I don't understand this error message. Which type can't be resolved? Is there a way to look at output of what the compiler generates for f.whatever!SomeResource(...); ? You can use -vcg-ast, but this isn't necessarily going to be compilable code. D doesn't allow chained instantiation (i.e. (A!B)!C), so you need to use either a mixin or a helper: import std.meta; enum fname = "load"; Instantiate!(f.opDispatch!fname, Bitmap)("path/to/wallpaper.png") or mixin("f." ~ fname ~ "!(Bitmap)(...);"); I'm assuming fname is given to you as a compile-time string and that's why you'd need to run opDispatch manually. -Steve
Re: DMD: Is it possible change compile time errors to runtime errors in Dlang?
On Friday, 6 March 2020 at 05:31:57 UTC, Mathias Lang wrote: On Friday, 6 March 2020 at 04:56:28 UTC, Marcone wrote: Is it possible change compile time errors to runtime errors in Dlang? If yes, how can I make it? No it's not possible, D is a statically typed language. Why would you want errors that can be caught at compile time to happen at runtimes ? I'm coming from Python
Re: How to dispatch a class function for an object accessed by handle?
On Friday, 6 March 2020 at 11:51:54 UTC, wjoe wrote: I don't understand this error message. Which type can't be resolved? I don't know. It works if you rename the inner one but it doesn't like eponymous templates like this. I suspect either the spec subtly doesn't allow it or a compiler bug. I think the type it is referring to is the `this` type. You can work around with an alias: // test rig import std.stdio; struct A { template opDispatch(string name) { auto opDispatch(T, Args...)(Args args) { writeln(name, ".", T.stringof, "(", args, ")"); } } } // workaround void main() { A a; alias helper = a.opDispatch!("foo"); a.helper!(int)(5, "omg"); } So the helper does one level, then the next level is done on the next line to avoid the stupid "multiple ! not allowed". You need to specify the `a` again to avoid `need this for...` due to how aliases are kinda weird. Huge hassle to use but if just doing it temporarily to debug it can be livable. Is there a way to look at output of what the compiler generates for f.whatever!SomeResource(...); ? -vcg-ast or something like to dmd but i never use it since there's TONS of spam in a file called `yourfile.d.cg`
Re: Safe cast
On Friday, 6 March 2020 at 13:03:22 UTC, drug wrote: Here x will be null. You can use `enforce(x !is null);` if you want exception. or since enforce returns it thing, just do B b = enforce(cast(B) x); you can also check easily in if statements: if(auto b = cast(B) x) { // x was a b, use b in here } else { // x was not b, try something else }
Re: Safe cast
It's too complex On 3/6/20 3:45 PM, Виталий Фадеев wrote: On Friday, 6 March 2020 at 12:35:29 UTC, Виталий Фадеев wrote: Searching info for object casting with checking class type at runtime. Like this: class A { // } class B { int bVar; } unittest { A a = new A(); A x = cast( A )a; // ok A x = cast( B )a; // ok, but unsafe Here x will be null. You can use `enforce(x !is null);` if you want exception. A x = safeCast( B )a; // throw exception A x = safeCast( A )a; // ok }
Re: Safe cast
On Friday, 6 March 2020 at 12:35:29 UTC, Виталий Фадеев wrote: Searching info for object casting with checking class type at runtime. Like this: class A { // } class B { int bVar; } unittest { A a = new A(); A x = cast( A )a; // ok A x = cast( B )a; // ok, but unsafe A x = safeCast( B )a; // throw exception A x = safeCast( A )a; // ok } Searching some like next: T safeCast( CLS )( CLS o ) { ... // checking type of o ... // may be check ClassInfo... return T; } Has function like a safeCast() ? Or other solution ? ... I using now next code: import std.stdio; class Base {} class A : Base {} class B : Base {} T safeCast( T, CLS )( CLS o ) { if ( typeid( o ) == typeid( T ) ) return cast(T)o; else throw new Exception( "casting error" ); } void main() { Base a = new A(); A x1 = cast( A )a; // ok B x2 = cast( B )a; // ok, but unsafe B x3 = safeCast!B( a ); // throw exception A x4 = safeCast!A( a ); // ok }
Safe cast
Searching info for object casting with checking class type at runtime. Like this: class A { // } class B { int bVar; } unittest { A a = new A(); A x = cast( A )a; // ok A x = cast( B )a; // ok, but unsafe A x = safeCast( B )a; // throw exception A x = safeCast( A )a; // ok } Searching some like next: T safeCast( CLS )( CLS o ) { ... // checking type of o ... // may be check ClassInfo... return T; } Has function like a safeCast() ? Or other solution ? ...
Re: How to dispatch a class function for an object accessed by handle?
On Thursday, 5 March 2020 at 18:33:41 UTC, Adam D. Ruppe wrote: On Thursday, 5 March 2020 at 14:24:33 UTC, wjoe wrote: [...] template opDispatch(string name) { auto opDispatch(T, Args...)(Args args) { ... } } [...] NOTE: opDispatch suppresses internal compile errors, it will just say "no such property whatever". you can explicitly instantiate with `f.opDispatch!"whatever` to help see better errors. Follow-up question: Calling f.whatever!SomeResource(...); works no problem. However I can't figure out how to call a function by explicitly instantiating opDispatch. Since f.opDispatch!"load"(handle, "wallpaper.png"); doesn't compile, I refreshed my memory about the shortcut syntax and the eponymous syntax and the way I read it is that this is a template of a template. So I tried this: f.opDispatch!"load".opDispatch!Bitmap(handle, "path/to/wallpaper.png"); But this doesn't compile either and errors out with: Error: Cannot resolve type for f.opDispatch(T, ARGS...)(ResourceHandle handle, ARGS args) I don't understand this error message. Which type can't be resolved? Is there a way to look at output of what the compiler generates for f.whatever!SomeResource(...); ?