Re: How to call destroy() in @nogc?
On Tuesday, 24 May 2022 at 02:29:38 UTC, cc wrote: ```d import core.memory; import core.stdc.stdlib : malloc, free; import core.lifetime : emplace; [...] FWIW your code will compile if you add `extern(C++)` to `Foo`
How to call destroy() in @nogc?
```d import core.memory; import core.stdc.stdlib : malloc, free; import core.lifetime : emplace; T NEW(T, Args...)(auto ref Args args) /*@nogc*/ if (is(T == class)) { enum size = __traits(classInstanceSize, T); void* mem = malloc(size); scope(failure) free(mem); return mem !is null ? emplace!T(mem[0..size], args) : null; } void FREE(T)(ref T obj) @nogc if (is(T == class)) { auto mem = cast(void*) obj; scope(exit) free(mem); destroy(obj); obj = null; } class Foo { ~this() @nogc {} } void main() { auto foo = NEW!Foo; FREE(foo); } ``` ``` Error: `@nogc` function `nogctest.FREE!(Foo).FREE` cannot call non-@nogc function `object.destroy!(true, Foo).destroy` ``` Is this not currently possible? Found this thread: https://forum.dlang.org/thread/zanuuhzmqxljadcex...@forum.dlang.org?page=1 is it still unresolved?
Re: Does D programming language have work steal queue?
On Monday, 23 May 2022 at 23:07:00 UTC, zoujiaqing wrote: On Sunday, 22 May 2022 at 23:34:19 UTC, mw wrote: On Sunday, 22 May 2022 at 21:07:19 UTC, zoujiaqing wrote: Does D language have task steal queue? The requirements are high-performance, lock-free, and thread-safe. I have a C's liblfds D wrapper: https://github.com/mw66/liblfdsd right now only bmm and bss queue are wrapped. It's not in dub yet, but I have been using it for a while, feel free to give it a try, or even send PRs :-) BTW, some very simple performance comparison with other D queues are here: https://github.com/mw66/liblfdsd/tree/master/comparison
Re: Does D programming language have work steal queue?
On Sunday, 22 May 2022 at 22:37:43 UTC, Stefan Koch wrote: On Sunday, 22 May 2022 at 21:07:19 UTC, zoujiaqing wrote: Does D language have task steal queue? The requirements are high-performance, lock-free, and thread-safe. I have one called fluffy: https://github.com/UplinkCoder/fluffy I am not 100% sure about the performance I did try to make it reasonable but in the absence of anything else it might be jumping off point for you. Thanks ;)
Re: Does D programming language have work steal queue?
On Sunday, 22 May 2022 at 23:34:19 UTC, mw wrote: On Sunday, 22 May 2022 at 21:07:19 UTC, zoujiaqing wrote: Does D language have task steal queue? The requirements are high-performance, lock-free, and thread-safe. I have a C's liblfds D wrapper: https://github.com/mw66/liblfdsd right now only bmm and bss queue are wrapped. It's not in dub yet, but I have been using it for a while, feel free to give it a try, or even send PRs :-) Thanks :) I will try it.
Re: mixin template
On 5/23/22 08:14, Vindex wrote: > Why? Why can't I have two constructors when I use mixin? And there is an example in Phobos: https://dlang.org/library/std/exception/basic_exception_ctors.html The documentation there mentions the following bug: https://issues.dlang.org/show_bug.cgi?id=11500 Ali
Re: mixin template
On Monday, 23 May 2022 at 15:14:53 UTC, Vindex wrote: I have this code: ``` import std.array, std.exception, std.stdio; mixin template RealizeException() { this(string msg, string file = __FILE__, size_t line = __LINE__) { super(msg, file, line); } } class WrongUsage : Exception { mixin RealizeException; this(string[] messages, string file = __FILE__, size_t line = __LINE__) { auto msg = std.array.join(messages, "\n"); super(msg, file, line); } } void main() { throw new WrongUsage("Error message."); } ``` ... and this error: ``` mixin_exception.d(19): Error: constructor `mixin_exception.WrongUsage.this(string[] messages, string file = __FILE__, ulong line = cast(ulong)__LINE__)` is not callable using argument types `(string)` mixin_exception.d(19):cannot pass argument `"Error message."` of type `string` to parameter `string[] messages` Failed: ["/usr/bin/dmd", "-v", "-o-", "mixin_exception.d", "-I."] ``` Why? Why can't I have two constructors when I use mixin? If I replace mixin to real code, I have no problem: ``` class WrongUsage : Exception { this(string msg, string file = __FILE__, size_t line = __LINE__) { super(msg, file, line); } this(string[] messages, string file = __FILE__, size_t line = __LINE__) { auto msg = std.array.join(messages, "\n"); super(msg, file, line); } } ``` mixin template create scope, for example if it was normal function (no ctor), you can do this: ```d mixin template RealizeException() { static void foo(string){} } class WrongUsage{ mixin RealizeException x; alias foo = x.foo; static void foo(string[]){} } void main() { WrongUsage.foo("Error message."); } ``` But for ctor this doesn't work...
mixin template
I have this code: ``` import std.array, std.exception, std.stdio; mixin template RealizeException() { this(string msg, string file = __FILE__, size_t line = __LINE__) { super(msg, file, line); } } class WrongUsage : Exception { mixin RealizeException; this(string[] messages, string file = __FILE__, size_t line = __LINE__) { auto msg = std.array.join(messages, "\n"); super(msg, file, line); } } void main() { throw new WrongUsage("Error message."); } ``` ... and this error: ``` mixin_exception.d(19): Error: constructor `mixin_exception.WrongUsage.this(string[] messages, string file = __FILE__, ulong line = cast(ulong)__LINE__)` is not callable using argument types `(string)` mixin_exception.d(19):cannot pass argument `"Error message."` of type `string` to parameter `string[] messages` Failed: ["/usr/bin/dmd", "-v", "-o-", "mixin_exception.d", "-I."] ``` Why? Why can't I have two constructors when I use mixin? If I replace mixin to real code, I have no problem: ``` class WrongUsage : Exception { this(string msg, string file = __FILE__, size_t line = __LINE__) { super(msg, file, line); } this(string[] messages, string file = __FILE__, size_t line = __LINE__) { auto msg = std.array.join(messages, "\n"); super(msg, file, line); } } ```
Re: How are delegate attributes in fn signature inferred?
On Monday, 23 May 2022 at 13:53:02 UTC, Adam D Ruppe wrote: On Monday, 23 May 2022 at 13:44:53 UTC, wjoe wrote: [...] You can actually make this work with `construct!(int[])` rather than plain `construct`. This is a (really annoying) deficiency in dmd's implementation. (that sdc solved btw proving it can be done just dmd never bothered) [...] I see. I figured the issue was an attribute mismatch. Thanks for the explanation. Very much appreciated!
Re: How are delegate attributes in fn signature inferred?
On Monday, 23 May 2022 at 13:44:53 UTC, wjoe wrote: i.construct((ulong i) {return cast(int)(i+i);}).print; You can actually make this work with `construct!(int[])` rather than plain `construct`. This is a (really annoying) deficiency in dmd's implementation. (that sdc solved btw proving it can be done just dmd never bothered) Where's **pure nothrow @nogc @safe** coming from? That's because it is a delegate literal, so it automatically figured out the tightest thing that works (this is also why it came as `function` instead of `delegate`, since it doesn't use any local variables from the enclosing function, it doesn't need the delegate pointer either). But all those implicitly convert away so it doesn't really matter. The type system allows this, just the crappy implementation can't handle inferring that R when it is mentioned both as R r *and* ElementType!R - the stupid compiler sees ElementType!R and bails out. Thus why you have to help it by telling the type when instantiating it i.construct!(int[])((ulong i) {return cast(int)(i+i);}).print;
How are delegate attributes in fn signature inferred?
Hello, Consider this example: ```d module foo; import std.stdio; import std.algorithm; import std.traits; import std.range; void print(R)(R r) { static assert(isIterable!R); r.each!writeln; } auto construct(R)(R r, ElementType!R delegate(ulong i) fn) { static assert(isIterable!R && hasAssignableElements!R); ulong i = 1; r.each!((ref e) => e = fn(i)); return r; } unittest { int[] i; i.length = 4; i.construct((ulong i) {return cast(int)(i+i);}).print; } ``` ```shell dmd -unittest -main foo.d Error: template 'foo.construct' cannot deduce function from argument types '!()(int[], int function(ulong i) pure nothrow @nogc @safe)', candidates are: 'construct(R)(R r, ElementType!R delegate(ulong i) fn)' ``` Where's **pure nothrow @nogc @safe** coming from? Also, why is *(ulong i) {return cast(int)(i+i);}* passed as a function? The error message for passing a delegate is the same except with *function* substituted for *delegate*.
Re: Allocate a string via the GC
On Monday, 23 May 2022 at 12:20:11 UTC, JG wrote: I am writing an interpreter and I needed access to a string via a pointer of type void* I ended up wrapping it in a struct since I needed another value anyway. Seems odd that one can't do it in a less unusual way. OK yeah, that's the main use case I'd think of, and that's also exactly why I think doing the struct is the best thing anyway since you can bundle whatever you need in there instead of just a single value.
Re: Allocate a string via the GC
On Monday, 23 May 2022 at 12:20:11 UTC, JG wrote: On Monday, 23 May 2022 at 11:39:22 UTC, Adam D Ruppe wrote: On Monday, 23 May 2022 at 09:38:07 UTC, JG wrote: Hi, Is there any more standard way to achieve something to the effect of: ```d import std.experimental.allocator; string* name = theAllocator.make!string; ``` Why do you want that? Easiest way I know of is to just wrap it in a struct, then `new that_struct`, which is also a better way for all the use cases I know but those use cases are pretty rare so there's probably a better way to do what you're trying to do. I am writing an interpreter and I needed access to a string via a pointer of type void* I ended up wrapping it in a struct since I needed another value anyway. Seems odd that one can't do it in a less unusual way. Thanks. You can take the address of the string. .ptr should do it, BUT I think it might not always work, someone can correct me on this, but I believe it depends on where the memory for the string lives whether it works or not?
Re: Allocate a string via the GC
On Monday, 23 May 2022 at 11:39:22 UTC, Adam D Ruppe wrote: On Monday, 23 May 2022 at 09:38:07 UTC, JG wrote: Hi, Is there any more standard way to achieve something to the effect of: ```d import std.experimental.allocator; string* name = theAllocator.make!string; ``` Why do you want that? Easiest way I know of is to just wrap it in a struct, then `new that_struct`, which is also a better way for all the use cases I know but those use cases are pretty rare so there's probably a better way to do what you're trying to do. I am writing an interpreter and I needed access to a string via a pointer of type void* I ended up wrapping it in a struct since I needed another value anyway. Seems odd that one can't do it in a less unusual way. Thanks.
Re: Allocate a string via the GC
On Monday, 23 May 2022 at 12:17:56 UTC, bauss wrote: On Monday, 23 May 2022 at 11:39:22 UTC, Adam D Ruppe wrote: On Monday, 23 May 2022 at 09:38:07 UTC, JG wrote: Hi, Is there any more standard way to achieve something to the effect of: ```d import std.experimental.allocator; string* name = theAllocator.make!string; ``` Why do you want that? Easiest way I know of is to just wrap it in a struct, then `new that_struct`, which is also a better way for all the use cases I know but those use cases are pretty rare so there's probably a better way to do what you're trying to do. My guess is @nogc in which case your solution doesn't work. Same with Ferhat's examples. Oops wait, I just saw that it says "via the GC" in the title. I completely missed that until I had of course sent my other message.
Re: Allocate a string via the GC
On Monday, 23 May 2022 at 11:39:22 UTC, Adam D Ruppe wrote: On Monday, 23 May 2022 at 09:38:07 UTC, JG wrote: Hi, Is there any more standard way to achieve something to the effect of: ```d import std.experimental.allocator; string* name = theAllocator.make!string; ``` Why do you want that? Easiest way I know of is to just wrap it in a struct, then `new that_struct`, which is also a better way for all the use cases I know but those use cases are pretty rare so there's probably a better way to do what you're trying to do. My guess is @nogc in which case your solution doesn't work. Same with Ferhat's examples.
Re: Allocate a string via the GC
On Monday, 23 May 2022 at 09:38:07 UTC, JG wrote: Hi, Is there any more standard way to achieve something to the effect of: ```d import std.experimental.allocator; string* name = theAllocator.make!string; ``` Pointers are not used for strings in d. string is an alias for immutable(char)[]. So, you can do: ```d import std.exception : assumeUnique; // makes char[] immutable string str = (new char[15]).assumeUnique; str = "hmm. my string!"; ``` a slice in d (e.g. char[]) is roughly considered as a struct with a pointer and length. I also suspect that your question is not that simple, maybe you really need a string pointer, and I couldn't understand what you are actually asking. I am sorry if those are not what you were looking for the answers to. Here is how you GC allocate a pointer to a string. ```d string* strptr = new string[1].ptr; // Since slices are reference types just use this instead: string[] strptr = new string[1]; ``` Maybe you need a pointer to the first char of a string then: ```d string str; // allocated somehow immutable(char)* chr = str.ptr; ```
Re: Allocate a string via the GC
On Monday, 23 May 2022 at 09:38:07 UTC, JG wrote: Hi, Is there any more standard way to achieve something to the effect of: ```d import std.experimental.allocator; string* name = theAllocator.make!string; ``` Why do you want that? Easiest way I know of is to just wrap it in a struct, then `new that_struct`, which is also a better way for all the use cases I know but those use cases are pretty rare so there's probably a better way to do what you're trying to do.
Allocate a string via the GC
Hi, Is there any more standard way to achieve something to the effect of: ```d import std.experimental.allocator; string* name = theAllocator.make!string; ```
Re: Odd construct idea. Splitting arguments inside a parameter list.
On Monday, 23 May 2022 at 08:53:27 UTC, user1234 wrote: On Monday, 23 May 2022 at 08:52:12 UTC, vit wrote: On Monday, 23 May 2022 at 08:34:21 UTC, Chris Katko wrote: D struct pair { float x,y; } [...] This work too: ```d myFunction(taco, p.tupleof, burrito); ``` and you can pass a std.typecons.Tuple as well, it will expand x y well actually std Tuple is a struct but that works without excplicit `.tupleof`.
Re: Odd construct idea. Splitting arguments inside a parameter list.
On Monday, 23 May 2022 at 08:52:12 UTC, vit wrote: On Monday, 23 May 2022 at 08:34:21 UTC, Chris Katko wrote: D struct pair { float x,y; } [...] This work too: ```d myFunction(taco, p.tupleof, burrito); ``` and you can pass a std.typecons.Tuple as well, it will expand x y
Re: Odd construct idea. Splitting arguments inside a parameter list.
On Monday, 23 May 2022 at 08:34:21 UTC, Chris Katko wrote: D struct pair { float x,y; } [...] This work too: ```d myFunction(taco, p.tupleof, burrito); ```
Re: Odd construct idea. Splitting arguments inside a parameter list.
On Monday, 23 May 2022 at 08:34:21 UTC, Chris Katko wrote: D I'm curious if you can pass a struct of values (a 'tuple'?) with the right subfields, as if those fields occupied a function signature. (As I write this and try to explain it, it probably sounds impossible.) Right now you can use `.tupleof`: ```d myFunction(taco, p.tupleof, burrito); ```
Odd construct idea. Splitting arguments inside a parameter list.
D struct pair { float x,y; } myFunction(float taco, float x, float y, float burrito) { // stuff } myfunction(_taco, _x, _y, _burrito); // call function // But can we do this? pair p; myfunction(_taco, p; _burrito); // p becomes (x,y) and satisfies the two floats in the signature I don't know if I need this but I'm curious if it's a template possibility. Though under-the-hood it could violate some necessary assumption about function signature matching. I'm curious if you can pass a struct of values (a 'tuple'?) with the right subfields, as if those fields occupied a function signature. (As I write this and try to explain it, it probably sounds impossible.) I have an existing API that uses X/Y coordinates, but I like using packed/named tuples (right term?) for related arguments. pos(x,y) vs velocity(x,y) for example make it super easy to tell which x belongs to which construct. Worst case I could just write wrapper functions for like 60+ functions. But it's still an interesting "can D do this" idea that popped into my head tonight. I'm always curious about what's possible. - Note that it doesn't necessarily have the struct fields match the called function argument names. I'm talking about calling with a struct with two floats (of any name), fulfilling two float requirement of a function signature. Is there a way for a tuple/array/some-sort-of-combined object to fulfill two separate function arguments? Of course we could always just do: D pair p; myFunction(taco, p.x, p.y, burrito);