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? 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 ?
DMD: Is it possible change compile time errors to runtime errors in Dlang?
Is it possible change compile time errors to runtime errors in Dlang? If yes, how can I make it?
Re: akePureMalloc cannot be interpreted at compile time
On Thursday, 5 March 2020 at 20:13:11 UTC, Adnan wrote: auto d = some!(some!int(9)); My guess would be this line is causing your trouble. That is trying to pass an instance as a value at compile time, thus triggering ctfe. Since you use the `Array` object inside and that uses malloc, this call tries to run malloc at compile time triggering the error. It kinda looks like a typo anyway...
akePureMalloc cannot be interpreted at compile time
The following program produces an error message and it is not clear exactly what line causes this error: module maybe; @nogc: private import std.container : Array; struct MayBe(T) { Array!T data; this(T datum) { data.reserve(1); data.insert(datum); } T unwrap() const { return data[0]; } T unwrapOr(T alternateValue) const { return isSome() ? unwrap() : alternateValue; } bool isSome() const { return !data.empty(); } bool isNone() const { return !isSome(); } bool contains(T arg) const { return isSome() && unwrap() == arg; } T expect(lazy string msg) const { if (isNone()) { import core.stdc.stdio : puts; import core.stdc.stdlib : exit; puts(msg.ptr); exit(-1); } return unwrap(); } void nullify() { data.clear(); } void insert(T datum) { if (isNone()) data.insert(datum); else data.front = datum; } } MayBe!T flatten(T)(MayBe!(MayBe!T) arg) { return isSome() ? arg.unwrap() : some!T(); } MayBe!T some(T)(T arg) { return MayBe!T(arg); } MayBe!T none(T)() { return MayBe!T(); } unittest { alias MB = MayBe; MB!int a; assert(a.isNone); auto b = none!int(); assert(b.isNone); auto c = some!int(23); assert(c.contains(23)); c.nullify(); assert(a == c); a.insert(11); c.insert(11); assert(a == c); auto d = some!(some!int(9)); assert(d.flatten() == some!int(9)); } $ dub test Generating test runner configuration 'maybe-test-library' for 'library' (library). Performing "unittest" build using dmd for x86_64. maybe ~master: building configuration "maybe-test-library"... /snap/dmd/99/bin/../import/phobos/std/internal/memory.d(31,33): Error: fakePureMalloc cannot be interpreted at compile time, because it has no available source code dmd failed with exit code 1. And what is the meaning of this error message?
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: Implement this for free functions i would do something like this void dispatch(alias fn, ARGS...)(Handle handle, ARGS args) Why do you need an `alias fn` like that? My suggestion would be to just use the `opDispatch` magic method that gives you a string, then `__traits(getMember, obj, memberName)(args)` to call it. But if you specifically need the alias param that won't work as well. (You could still do `__traits(getMember, obj, __traits(identifier, fn))` though, so it isn't ruled out entirely, just not as nice. That is also more likely to break with overloads btw) struct Handle { private Whatever obj; auto opDispatch(string name, Args...)(Args args) { return __traits(getMember, obj, name)(args); } } And the usage would look like: auto size = f.getSize(wallpaperhandle); assuming the Handle knows how to store Whatever without being told what it is again at the call site (e.g. if you actually use an `interface` internally, or an encapsulated tagged union or whatever). If you do need to tell it what it is, a two-level function gives that opportunity: template opDispatch(string name) { auto opDispatch(T, Args...)(Args args) { auto obj = cast(T) o; // or whatever you do to convert return __traits(getMember, obj, name)(args); } } then the usage looks like auto size = f.getSize!Bitmap(wallpaperhandle); 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. But it depends on what exactly you are doing. Thanks for your reply:) I don't need an alias at all. I was trying to figure something out with opDispatch first but something like __traits(getMember, obj, name)(args); never occurred to me. Awesome! The handle knows whether or not it's valid and where to find the object and it only makes sense in the context of the factory that made it. The template opDispatch looks like what I was looking for :)
Re: How to dispatch a class function for an object accessed by handle?
On Thursday, 5 March 2020 at 14:46:24 UTC, Steven Schveighoffer wrote: On 3/5/20 9:24 AM, wjoe wrote: but how can I call fn in the context of an object instance? You could do it with delegates. But it's ugly: import std.stdio; class C { void foo() { writeln("Yup");} } void main() { alias f = C.foo; auto c = new C; void delegate() dg; dg.funcptr = dg.ptr = cast(void*)c; dg(); // prints "Yup" } I don't know of a way to call f with c aside from this. -Steve I have an ugly implementation with a one liner mixin and I don't like it. Your solution looks interesting but I think that's more code than my current solution. Thanks for your reply though.
Re: How to dispatch a class function for an object accessed by handle?
On Thursday, 5 March 2020 at 14:24:33 UTC, wjoe wrote: Implement this for free functions i would do something like this void dispatch(alias fn, ARGS...)(Handle handle, ARGS args) Why do you need an `alias fn` like that? My suggestion would be to just use the `opDispatch` magic method that gives you a string, then `__traits(getMember, obj, memberName)(args)` to call it. But if you specifically need the alias param that won't work as well. (You could still do `__traits(getMember, obj, __traits(identifier, fn))` though, so it isn't ruled out entirely, just not as nice. That is also more likely to break with overloads btw) struct Handle { private Whatever obj; auto opDispatch(string name, Args...)(Args args) { return __traits(getMember, obj, name)(args); } } And the usage would look like: auto size = f.getSize(wallpaperhandle); assuming the Handle knows how to store Whatever without being told what it is again at the call site (e.g. if you actually use an `interface` internally, or an encapsulated tagged union or whatever). If you do need to tell it what it is, a two-level function gives that opportunity: template opDispatch(string name) { auto opDispatch(T, Args...)(Args args) { auto obj = cast(T) o; // or whatever you do to convert return __traits(getMember, obj, name)(args); } } then the usage looks like auto size = f.getSize!Bitmap(wallpaperhandle); 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. But it depends on what exactly you are doing.
Re: spawn a function with object as arg?
On Thursday, 5 March 2020 at 18:10:11 UTC, Martin Brezel wrote: Unfortunately the documentation page for core.thread is currently not available. the official docs are lame, use my fork doc website instead: http://dpldocs.info/experimental-docs/core.thread.html
Re: spawn a function with object as arg?
On Thursday, 5 March 2020 at 03:04:10 UTC, Adam D. Ruppe wrote: You can also not use `spawn` and instead give `new Thread` a try from core.thread. Thanks for the hint, I didn't notice core.thread at all. I will definitely try it out. Unfortunately the documentation page for core.thread is currently not available.
Re: Converting Lua source to D
On Thursday, 5 March 2020 at 07:44:21 UTC, Jesse Phillips wrote: I am making an attempt convert Lua to D. This is less about the conversion and more about exploring the tooling to make it happen. I have chosen to do this file by file and attempting to start with linint. I wanted to make use of dpp, however I hit a segmentation fault and reduced dependency. https://github.com/JesseKPhillips/lua/blob/dpp/init/linit.d I wasn't able to get a core dump for debugging. Anyone willing to give some advice or solution? I have gone through a number of compilers and better. I have implemented a build pipeline but didn't incorporate the D portion at this time. 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, _debug}, {null, null} ]; to static const luaL_Reg[] loadedlibs = [ ... {LUA_DBLIBNAME, _debug} ];
Re: How to dispatch a class function for an object accessed by handle?
On 3/5/20 9:24 AM, wjoe wrote: but how can I call fn in the context of an object instance? You could do it with delegates. But it's ugly: import std.stdio; class C { void foo() { writeln("Yup");} } void main() { alias f = C.foo; auto c = new C; void delegate() dg; dg.funcptr = dg.ptr = cast(void*)c; dg(); // prints "Yup" } I don't know of a way to call f with c aside from this. -Steve
How to dispatch a class function for an object accessed by handle?
Consider a Factory that creates instances of various different resource object instances, all of which have a common interface, and returns a handle to them. class Factory { struct Handle{} Handle create(R: Resource, ARGS...)(ARGS args) { auto r = new R(args); //... return handle; } } auto f = new Factory; auto wallpaperhandle = f.create!Bitmap(...); In order to be able do something with these resources there's a function to get the resource instance from a handle: auto get(R: Resource)(Handle h) {...}; auto bitmap = f.get!Bitmap(wallpaperhandle); auto size = bitmap.getSize(); if (condition) bitmap.scaleByFactor(2); I don't like this approach because it exposes the instance directly. What I want instead is something like this auto size = f.dispatch!(Bitmap, Bitmap.getSize)(wallpaperhandle); if (condition) f.dispatch!(Bitmap, Bitmap.scaleByFactor)(wallpaperhandle, 2); Implement this for free functions i would do something like this void dispatch(alias fn, ARGS...)(Handle handle, ARGS args) { fn(handle, args); } but how can I call fn in the context of an object instance? class Factory { auto ref dispatch(R: Resource, alias fn, ARGS ...)(Handle handle, ARGS args) { auto r = get!R(handle); assert(stuff is valid); return r.fn(args); // How can I call class function 'fn' using r ? } } mixin(something) comes to mind, but are there better options ?
Re: converting to/from char[]/string
On Thursday, 5 March 2020 at 13:31:14 UTC, Adam D. Ruppe wrote: On Thursday, 5 March 2020 at 11:03:30 UTC, mark wrote: I want to use the Porter stemming algorithm. There's a D implementation here: https://tartarus.org/martin/PorterStemmer/d.txt I think I (or ketmar and I stole it from him) ported that very same file before: https://github.com/adamdruppe/adrdox/blob/master/stemmer.d By just adding `const` where appropriate it becomes compatible with string and you can slice to take care of the size thing. https://github.com/adamdruppe/adrdox/blob/master/stemmer.d#L512 is that stem function as a const slice I thought the problem was using char[] rather than dchar[], but evidently not. I downloaded yours and it "just works": I didn't have to change anything. (dscanner gives a couple of const/immutable hints which I'll fix, but still.) Might be good to ask to add yours to https://tartarus.org/martin/PorterStemmer/ since it works and the old one doesn't. Thank you!
Re: converting to/from char[]/string
On Thursday, 5 March 2020 at 11:03:30 UTC, mark wrote: I want to use the Porter stemming algorithm. There's a D implementation here: https://tartarus.org/martin/PorterStemmer/d.txt I think I (or ketmar and I stole it from him) ported that very same file before: https://github.com/adamdruppe/adrdox/blob/master/stemmer.d By just adding `const` where appropriate it becomes compatible with string and you can slice to take care of the size thing. https://github.com/adamdruppe/adrdox/blob/master/stemmer.d#L512 is that stem function as a const slice
Re: converting to/from char[]/string
I suspect the problem is using .length rather than some other size property.
Re: converting to/from char[]/string
I changed int to size_t and used const(char[]) etc. as suggested. It ran but crashed. Each crash was a range violation, so for each one I put in a guard so instead of if ( ... m_b[m_k]) I used if (m_k < m_b.length && ... m_b[m_k) I did this kind of fix in three places. The result is that it does some but not all the stemming! Anyway, I'll compare it with the Python version and see if I can spot the problem(s). Thanks.
Re: converting to/from char[]/string
On Thursday, 5 March 2020 at 11:31:43 UTC, mark wrote: I've now got Martin Porter's own Java version, so I'll have a go at porting that to D myself. I don't think that's necessary, the errors seem easy to fix. src/porterstemmer.d(197,13): Error: cannot implicitly convert expression s.length of type ulong to int src/porterstemmer.d(222,9): Error: cannot implicitly convert expression cast(ulong)this.m_j + s.length of type ulong to int These errors are probably because the code was only compiled on 32-bit targets where .length is of type `uint`, but you are compiling on 64-bit where .length is of type `ulong`. A quick fix is to simply cast the result like `cast(int) s.length` and `cast(int) (this.m_j + s.length)`, though a proper fix would be to change the types of variables to `long`, `size_t`, `auto` or `const` (depending on which is most appropriate). src/porterstemmer.d(259,12): Error: function porterstemmer.PorterStemmer.ends(char[] s) is not callable using argument types (string) src/porterstemmer.d(259,12):cannot pass argument "sses" of type string to parameter char[] s These errors are because `string` is `immutable(char)[]`, meaning the characters may not be modified, while the function accepts a `char[]` which is allowed to mutate the characters. I don't think the functions actually do that, so you can simply change `char[]` into `const(char)[]` so a string can be passed to those functions.
Re: converting to/from char[]/string
On Thursday, 5 March 2020 at 11:12:24 UTC, drug wrote: On 3/5/20 2:03 PM, mark wrote: [snip] Your code and errors seem to be not related. OK, it is probably that the D stemmer is 19 years old! I've now got Martin Porter's own Java version, so I'll have a go at porting that to D myself.
Re: converting to/from char[]/string
On 3/5/20 2:03 PM, mark wrote: I want to use the Porter stemming algorithm. There's a D implementation here: https://tartarus.org/martin/PorterStemmer/d.txt The main public function's signature is: char[] stem(char[] p, int i, int j) But I work entirely in terms of strings (containing individual words), so I want to add another function with this signature: string stem(string word) I've tried this without success: public string stem(string word) { import std.conv: to; char[] chars = word.to!char[]; int end = chars.length.to!int; > return stem(chars, 0, end).to!string; } Here are just a few of the errors: src/porterstemmer.d(197,13): Error: cannot implicitly convert expression s.length of type ulong to int src/porterstemmer.d(222,9): Error: cannot implicitly convert expression cast(ulong)this.m_j + s.length of type ulong to int src/porterstemmer.d(259,12): Error: function porterstemmer.PorterStemmer.ends(char[] s) is not callable using argument types (string) src/porterstemmer.d(259,12): cannot pass argument "sses" of type string to parameter char[] s Your code and errors seem to be not related.
converting to/from char[]/string
I want to use the Porter stemming algorithm. There's a D implementation here: https://tartarus.org/martin/PorterStemmer/d.txt The main public function's signature is: char[] stem(char[] p, int i, int j) But I work entirely in terms of strings (containing individual words), so I want to add another function with this signature: string stem(string word) I've tried this without success: public string stem(string word) { import std.conv: to; char[] chars = word.to!char[]; int end = chars.length.to!int; return stem(chars, 0, end).to!string; } Here are just a few of the errors: src/porterstemmer.d(197,13): Error: cannot implicitly convert expression s.length of type ulong to int src/porterstemmer.d(222,9): Error: cannot implicitly convert expression cast(ulong)this.m_j + s.length of type ulong to int src/porterstemmer.d(259,12): Error: function porterstemmer.PorterStemmer.ends(char[] s) is not callable using argument types (string) src/porterstemmer.d(259,12):cannot pass argument "sses" of type string to parameter char[] s
Re: What does assigning void mean?
On Thursday, 5 March 2020 at 08:35:52 UTC, drug wrote: On 3/5/20 10:47 AM, mark wrote: In Adam Ruppe's D Cookbook there're these lines in a ref counting example: RefCountedObject o = void; // What does this mean/do? o.data = new Implementation(); o.data.refcount = 1; I don't understand the first line; could someone explain please? In D all vars are initialized by default. If you use assigning void then the var won't be initialized. Thanks, I had read it (in "Learning D" I think), but had already forgotten.
Re: What does assigning void mean?
On Thursday, 5 March 2020 at 08:35:52 UTC, drug wrote: On 3/5/20 10:47 AM, mark wrote: In Adam Ruppe's D Cookbook there're these lines in a ref counting example: RefCountedObject o = void; // What does this mean/do? o.data = new Implementation(); o.data.refcount = 1; I don't understand the first line; could someone explain please? In D all vars are initialized by default. If you use assigning void then the var won't be initialized. To expand a bit on this: You probably don't want to initialize things with = void - it can lead to hard-to-track bugs and unexpected behavior. The reasons it's there is almost entirely as an optimization - if you know the variable will be initialized elsewhere void initialization ensure things won't be initialized twice when once is enough, and this can be faster. The other use case is when for whatever reason there is no valid default value, but you still want an instance. Probably in order to fill it with data from somewhere else. This would apply e.g. to structs with @disabled parameterless constructors whose contents you are reading from disk. In short, when you know you need to void initialize something, that's when you're ready to use it. Kinda like goto. -- Simen
Re: What does assigning void mean?
On 3/5/20 10:47 AM, mark wrote: In Adam Ruppe's D Cookbook there're these lines in a ref counting example: RefCountedObject o = void; // What does this mean/do? o.data = new Implementation(); o.data.refcount = 1; I don't understand the first line; could someone explain please? In D all vars are initialized by default. If you use assigning void then the var won't be initialized.