Re: Any workaround for "closures are not yet supported in CTFE"?

2021-12-08 Thread Timon Gehr via Digitalmars-d-learn
On 08.12.21 03:05, Andrey Zherikov wrote: On Tuesday, 7 December 2021 at 18:50:04 UTC, Ali Çehreli wrote: I don't know whether the workaround works with your program but that delegate is the equivalent of the following struct (the struct should be faster because there is no dynamic context

Re: Any workaround for "closures are not yet supported in CTFE"?

2021-12-08 Thread Petar via Digitalmars-d-learn
On Wednesday, 8 December 2021 at 07:55:55 UTC, Timon Gehr wrote: On 08.12.21 03:05, Andrey Zherikov wrote: On Tuesday, 7 December 2021 at 18:50:04 UTC, Ali Çehreli wrote: I don't know whether the workaround works with your program but that delegate is the equivalent of the following struct

Re: Struct fields and properties as alias members

2021-12-08 Thread Ben Jones via Digitalmars-d-learn
On Wednesday, 8 December 2021 at 17:44:47 UTC, Adam D Ruppe wrote: On Wednesday, 8 December 2021 at 17:19:32 UTC, Ben Jones wrote: Gotcha, thanks. I tried using `S.field` before and that didn't work, `__traits(child)` was the key. Since I reuse the field a few times I tried to alias the

Re: Struct fields and properties as alias members

2021-12-08 Thread Adam D Ruppe via Digitalmars-d-learn
On Wednesday, 8 December 2021 at 18:07:32 UTC, Ben Jones wrote: Since I reuse the field a few times I tried to alias the result: `alias theProp = __traits(child, s, field);` yeah won't work since the alias again drops the `this`, this is the same thing as the template param. You could make

Re: Struct fields and properties as alias members

2021-12-08 Thread Ben Jones via Digitalmars-d-learn
On Wednesday, 8 December 2021 at 18:23:25 UTC, Adam D Ruppe wrote: On Wednesday, 8 December 2021 at 18:07:32 UTC, Ben Jones wrote: I went with the mixin approach, which seems to work fine except that I couldn't declare the mixin inside a function, so the definition is pretty far away from

Re: How to loop through characters of a string in D language?

2021-12-08 Thread Adam D Ruppe via Digitalmars-d-learn
On Wednesday, 8 December 2021 at 11:23:45 UTC, BoQsc wrote: The string example to loop/iterate: foreach(ch; a) { } does the individual chars of the string you can also foreach(dchar ch; a) { } to decode the utf 8

Re: How to loop through characters of a string in D language?

2021-12-08 Thread BoQsc via Digitalmars-d-learn
On Wednesday, 8 December 2021 at 11:35:39 UTC, Biotronic wrote: On Wednesday, 8 December 2021 at 11:23:45 UTC, BoQsc wrote: Let's say I want to skip characters and build a new string. The string example to loop/iterate: ``` import std.stdio; void main() { string a="abc;def;ab"; } ```

How to loop through characters of a string in D language?

2021-12-08 Thread BoQsc via Digitalmars-d-learn
Let's say I want to skip characters and build a new string. The string example to loop/iterate: ``` import std.stdio; void main() { string a="abc;def;ab"; } ``` The character I want to skip: `;` Expected result: ``` abcdefab ```

Re: Any workaround for "closures are not yet supported in CTFE"?

2021-12-08 Thread Stanislav Blinov via Digitalmars-d-learn
On Wednesday, 8 December 2021 at 08:07:59 UTC, Petar Kirov [ZombineDev] wrote: ```d interface ICallable { void opCall() const; } alias Action = void delegate(); struct A { Action[] dg; } ``` At this point why not just call a spade a spade and store an array of ICallables directly?

Re: How to loop through characters of a string in D language?

2021-12-08 Thread BoQsc via Digitalmars-d-learn
On Wednesday, 8 December 2021 at 12:49:39 UTC, Adam D Ruppe wrote: On Wednesday, 8 December 2021 at 11:23:45 UTC, BoQsc wrote: The string example to loop/iterate: foreach(ch; a) { } does the individual chars of the string you can also foreach(dchar ch; a) { } to decode the utf 8 Thanks

Re: How to loop through characters of a string in D language?

2021-12-08 Thread bauss via Digitalmars-d-learn
On Wednesday, 8 December 2021 at 11:23:45 UTC, BoQsc wrote: Let's say I want to skip characters and build a new string. The string example to loop/iterate: ``` import std.stdio; void main() { string a="abc;def;ab"; } ``` The character I want to skip: `;` Expected result: ``` abcdefab

Re: How to loop through characters of a string in D language?

2021-12-08 Thread BoQsc via Digitalmars-d-learn
On Wednesday, 8 December 2021 at 14:16:16 UTC, bauss wrote: On Wednesday, 8 December 2021 at 11:23:45 UTC, BoQsc wrote: Let's say I want to skip characters and build a new string. The string example to loop/iterate: ``` import std.stdio; void main() { string a="abc;def;ab"; } ``` The

Re: How to loop through characters of a string in D language?

2021-12-08 Thread Biotronic via Digitalmars-d-learn
On Wednesday, 8 December 2021 at 11:23:45 UTC, BoQsc wrote: Let's say I want to skip characters and build a new string. The string example to loop/iterate: ``` import std.stdio; void main() { string a="abc;def;ab"; } ``` The character I want to skip: `;` Expected result: ``` abcdefab

Re: Any workaround for "closures are not yet supported in CTFE"?

2021-12-08 Thread Timon Gehr via Digitalmars-d-learn
On 12/8/21 9:07 AM, Petar Kirov [ZombineDev] wrote: On Wednesday, 8 December 2021 at 07:55:55 UTC, Timon Gehr wrote: On 08.12.21 03:05, Andrey Zherikov wrote: On Tuesday, 7 December 2021 at 18:50:04 UTC, Ali Çehreli wrote: I don't know whether the workaround works with your program but that

Re: Struct fields and properties as alias members

2021-12-08 Thread H. S. Teoh via Digitalmars-d-learn
On Wed, Dec 08, 2021 at 05:19:32PM +, Ben Jones via Digitalmars-d-learn wrote: > I'm trying to use a property member of a struct as a template alias > parameter and I don't really understand how to fix the error message > I'm seeing (I also tried the simpler case of a plain struct member, >

Re: Struct fields and properties as alias members

2021-12-08 Thread Ben Jones via Digitalmars-d-learn
On Wednesday, 8 December 2021 at 17:29:19 UTC, H. S. Teoh wrote: On Wed, Dec 08, 2021 at 05:19:32PM +, Ben Jones via Digitalmars-d-learn wrote: I'm trying to use a property member of a struct as a template alias parameter and I don't really understand how to fix the error message I'm

Struct fields and properties as alias members

2021-12-08 Thread Ben Jones via Digitalmars-d-learn
I'm trying to use a property member of a struct as a template alias parameter and I don't really understand how to fix the error message I'm seeing (I also tried the simpler case of a plain struct member, and that didn't work either). Is what I'm trying to do possible? It seems like maybe

Re: Struct fields and properties as alias members

2021-12-08 Thread Ben Jones via Digitalmars-d-learn
On Wednesday, 8 December 2021 at 17:19:32 UTC, Ben Jones wrote: I considered just having a `ref int` parameter, but I didn't think that would work if I was actually calling a property function, rather than just modifying a struct member. I also tried to use a template mixin, but couldn't

Re: Struct fields and properties as alias members

2021-12-08 Thread H. S. Teoh via Digitalmars-d-learn
On Wed, Dec 08, 2021 at 05:21:49PM +, Ben Jones via Digitalmars-d-learn wrote: [...] > I considered just having a `ref int` parameter, but I didn't think > that would work if I was actually calling a property function, rather > than just modifying a struct member. [...] Why not pass the

Re: Struct fields and properties as alias members

2021-12-08 Thread Adam D Ruppe via Digitalmars-d-learn
On Wednesday, 8 December 2021 at 17:19:32 UTC, Ben Jones wrote: It seems like maybe I'd have to pass s as a runtime parameter to get it to work? yes, you do. the alias param passes the abstract idea of the variable instead of the variable: void main(){ S s; func!(s.a)(); See, you

Re: How to loop through characters of a string in D language?

2021-12-08 Thread forkit via Digitalmars-d-learn
On Wednesday, 8 December 2021 at 22:35:35 UTC, Stanislav Blinov wrote: You're passing a literal. Try passing a runtime value (e.g. a command line argument). Also, -O2 -release :) Uless, of course, your goal is to look at debug code. but this will change nothing. the compilation cost of

Re: Any workaround for "closures are not yet supported in CTFE"?

2021-12-08 Thread Petar via Digitalmars-d-learn
On Wednesday, 8 December 2021 at 17:05:49 UTC, Timon Gehr wrote: On 12/8/21 9:07 AM, Petar Kirov [ZombineDev] wrote: [...] Nice, so the error message is lying. Closure support deserves way more love in the compiler. I'm quite surprised that that hack worked, given that various very similar

T... args!

2021-12-08 Thread Salih Dincer via Digitalmars-d-learn
Hi all, Is this not a contradiction? : and 3.1415 aren't string: ```d void foo(string...)(string args) { foreach(ref str; args) { str.writeln('\t', typeof(str).stringof); } } foo("Pi", "number", ':', 3.1415); /* Pi string number string : char 3,1415 double

Re: T... args!

2021-12-08 Thread Salih Dincer via Digitalmars-d-learn
On Wednesday, 8 December 2021 at 23:47:07 UTC, Adam Ruppe wrote: On Wednesday, 8 December 2021 at 23:43:48 UTC, Salih Dincer wrote: I think you meant to say void foo(string[] args...) {} Not exactly... ```d alias str = immutable(char)[]; void foo(str...)(str args) { foreach(ref a; args)

Re: Any workaround for "closures are not yet supported in CTFE"?

2021-12-08 Thread Petar via Digitalmars-d-learn
On Wednesday, 8 December 2021 at 12:17:42 UTC, Stanislav Blinov wrote: On Wednesday, 8 December 2021 at 08:07:59 UTC, Petar Kirov [ZombineDev] wrote: ```d interface ICallable { void opCall() const; } alias Action = void delegate(); struct A { Action[] dg; } ``` At this point why

Re: How to loop through characters of a string in D language?

2021-12-08 Thread forkit via Digitalmars-d-learn
On Wednesday, 8 December 2021 at 14:27:22 UTC, BoQsc wrote: On Wednesday, 8 December 2021 at 14:16:16 UTC, bauss wrote: On Wednesday, 8 December 2021 at 11:23:45 UTC, BoQsc wrote: Let's say I want to skip characters and build a new string. The string example to loop/iterate: ``` import

Re: How to loop through characters of a string in D language?

2021-12-08 Thread Stanislav Blinov via Digitalmars-d-learn
On Wednesday, 8 December 2021 at 22:18:23 UTC, forkit wrote: It's also worth noting the differences in compiler output, as well as the time taken to compile, these two approaches: (1) string str = "abc;def;ab".filter!(c => c != ';').to!string; (2) string str = "abc;def;ab".replace(";", "");

Re: How to loop through characters of a string in D language?

2021-12-08 Thread forkit via Digitalmars-d-learn
On Wednesday, 8 December 2021 at 22:55:02 UTC, forkit wrote: On Wednesday, 8 December 2021 at 22:35:35 UTC, Stanislav Blinov wrote: You're passing a literal. Try passing a runtime value (e.g. a command line argument). Also, -O2 -release :) Uless, of course, your goal is to look at debug

Re: T... args!

2021-12-08 Thread Adam Ruppe via Digitalmars-d-learn
On Wednesday, 8 December 2021 at 23:43:48 UTC, Salih Dincer wrote: Is this not a contradiction? : and 3.1415 aren't string: ```d void foo(string...)(string args) { `string...` there is a user-defined identifier representing a mix of types. string isn't special, yo can declare your own

Re: How to loop through characters of a string in D language?

2021-12-08 Thread Salih Dincer via Digitalmars-d-learn
On Wednesday, 8 December 2021 at 11:23:45 UTC, BoQsc wrote: Let's say I want to skip characters and build a new string. The string example to loop/iterate: ``` import std.stdio; void main() { string a="abc;def;ab"; } ``` The character I want to skip: `;` Expected result: ``` abcdefab

Re: Any workaround for "closures are not yet supported in CTFE"?

2021-12-08 Thread Andrey Zherikov via Digitalmars-d-learn
On Wednesday, 8 December 2021 at 17:05:49 UTC, Timon Gehr wrote: ```d import std.stdio, std.traits, core.lifetime; auto partiallyApply(alias fun,C...)(C context){ return class(move(context)){ C context; this(C context) { foreach(i,ref c;this.context) c=move(context[i]); }

Re: C++ bindings: const(T) dropped in return types of templates ?

2021-12-08 Thread frame via Digitalmars-d-learn
On Monday, 6 December 2021 at 20:31:47 UTC, Jan wrote: So am I missing something, or did the compiler somehow forget about the const-ness? Sounds like a bug to me, eg this one: https://issues.dlang.org/show_bug.cgi?id=20685