Re: Challenge Tuples

2024-05-01 Thread Andrey Zherikov via Digitalmars-d-learn
On Friday, 26 April 2024 at 13:25:34 UTC, Salih Dincer wrote: You have a 5-item data tuples as Tuple(1, 2, 3, [1, 3], 5) and implement the sum (total = 15) with the least codes using the sum() function of the language you are coding... Let's start with D: ```d import std.typecons : tuple;

Re: Is a shorter statement possible in this case?

2023-11-05 Thread Andrey Zherikov via Digitalmars-d-learn
On Sunday, 5 November 2023 at 18:36:40 UTC, Ctn-Dev wrote: Is there a more concise way of getting the same result? Try this: ```d switch(range.map!(_ => (_== "Two" ? 2 : _=="One" ? 1 : 0)).fold!((a,b) => a | b)(0)) { case 3: writeln("both"); break; case 2: writein("only two"); break; case

Re: DUB: Is it possible to set release as a default build for a dub package?

2023-11-03 Thread Andrey Zherikov via Digitalmars-d-learn
On Friday, 3 November 2023 at 19:21:42 UTC, BoQsc wrote: However I would want to try to enforce this behaviour from the `dub.json` or `dub.sdl` file. IMHO this is not something that should be enforced from package build configuration, however there can be a way to configure it on system

Re: Keyword "package" prevents from importing a package module "package.d"

2023-11-03 Thread Andrey Zherikov via Digitalmars-d-learn
On Friday, 3 November 2023 at 18:04:58 UTC, Jonathan M Davis wrote: - Jonathan M Davis Thanks a lot for detailed explanation!

Re: Keyword "package" prevents from importing a package module "package.d"

2023-11-03 Thread Andrey Zherikov via Digitalmars-d-learn
On Friday, 3 November 2023 at 00:52:18 UTC, H. S. Teoh wrote: Supposedly you can do this: /* Original: */ // pkg/mymodule.d module mymodule; ... // code here // main.d import mymodule; void main() { ... } /* Split */ //

Re: Keyword "package" prevents from importing a package module "package.d"

2023-11-02 Thread Andrey Zherikov via Digitalmars-d-learn
On Thursday, 2 November 2023 at 19:43:01 UTC, Adam D Ruppe wrote: On Thursday, 2 November 2023 at 19:30:58 UTC, Jonathan M Davis wrote: The entire reason that it was added to the language was to be able to split up existing modules without breaking code. And it does that well. No, it doesn't

Re: Key and value with ranges

2023-10-03 Thread Andrey Zherikov via Digitalmars-d-learn
On Tuesday, 3 October 2023 at 19:57:06 UTC, christian.koestlin wrote: On Tuesday, 3 October 2023 at 01:55:43 UTC, Andrey Zherikov wrote: On Monday, 2 October 2023 at 18:46:14 UTC, christian.koestlin wrote: [...] Slightly improved: ```d import std; [...] Thanks .. the thing with ref result

Re: Key and value with ranges

2023-10-02 Thread Andrey Zherikov via Digitalmars-d-learn
On Monday, 2 October 2023 at 18:46:14 UTC, christian.koestlin wrote: On Monday, 2 October 2023 at 02:47:37 UTC, Joel wrote: How can I improve this code? Like avoiding using foreach. You could fold into a hash that counts the occurrences like that: ```d import std.uni : toLower; import

Re: Function template as template parameter

2022-12-11 Thread Andrey Zherikov via Digitalmars-d-learn
On Sunday, 11 December 2022 at 16:24:30 UTC, Ali Çehreli wrote: On 12/11/22 05:54, Salih Dincer wrote: > On Sunday, 11 December 2022 at 09:43:34 UTC, Andrey Zherikov wrote: >> Note that callback parameter must be compile-time parameter as it >> represents a member of a type during introspection

Function template as template parameter

2022-12-11 Thread Andrey Zherikov via Digitalmars-d-learn
I have this (very simplified) code: ```d void foo(alias callback)() { callback!5; } static void print(int i)() { writeln(i); } foo!print; ``` Is there a way to merge two last lines into one of a form like `foo!(...something...)`? Note that callback parameter must be compile-time

Re: Is defining get/set methods for every field overkill?

2022-11-19 Thread Andrey Zherikov via Digitalmars-d-learn
On Saturday, 19 November 2022 at 10:17:19 UTC, []() {}() wrote: On Saturday, 19 November 2022 at 09:51:09 UTC, []() {}() wrote: ... no. in C# it breaks ABI. that is why its not acceptable in my team. of course that's not the reason we don't use public member variables in classes. the

Re: Is defining get/set methods for every field overkill?

2022-11-19 Thread Andrey Zherikov via Digitalmars-d-learn
On Saturday, 19 November 2022 at 09:49:18 UTC, thebluepandabear wrote: I am too dumb to know what ABI is https://en.wikipedia.org/wiki/Application_binary_interface

Re: Is defining get/set methods for every field overkill?

2022-11-19 Thread Andrey Zherikov via Digitalmars-d-learn
On Saturday, 19 November 2022 at 09:51:09 UTC, []() {}() wrote: no. in C# it breaks ABI. that is why its not acceptable in my team. Every company works differently. Yours are using .net and (I guess) ships dlls so you must care about ABI with existing software. In my company we use static

Re: Is defining get/set methods for every field overkill?

2022-11-19 Thread Andrey Zherikov via Digitalmars-d-learn
On Saturday, 19 November 2022 at 09:12:26 UTC, thebluepandabear wrote: That's the point many people have given here which is not convincing him, even though it is quite great. I think we all know the answer here  IMHO you are both right :) You are speaking about API compatibility, `[]()

Re: Is defining get/set methods for every field overkill?

2022-11-19 Thread Andrey Zherikov via Digitalmars-d-learn
On Saturday, 19 November 2022 at 09:12:26 UTC, thebluepandabear wrote: That's the point many people have given here which is not convincing him, even though it is quite great. I think we all know the answer here  IMHO you are both right :) You are speaking about API compatibility, `[]()

Re: Is defining get/set methods for every field overkill?

2022-11-19 Thread Andrey Zherikov via Digitalmars-d-learn
On Saturday, 19 November 2022 at 09:05:42 UTC, []() {}() wrote: Once you refactor your class (creating actual private member variables and creating getter/setter methods, you are almost certainly going to break binary compatability, and when you send your update compiler library to your

Re: Is defining get/set methods for every field overkill?

2022-11-19 Thread Andrey Zherikov via Digitalmars-d-learn
On Saturday, 19 November 2022 at 04:13:33 UTC, []() {}() wrote: oh. so i get it now. you have to refactor your class (change member variable names and also do it in all places where they are used througout the class. then add new methods, overloading them in this way and that way, all because

Re: ImportC linking issue

2022-11-13 Thread Andrey Zherikov via Digitalmars-d-learn
On Saturday, 12 November 2022 at 10:02:12 UTC, confuzzled wrote: Why would I have to search for libcrypto, libminizip, libexpat, and more that I haven't even figured out what library they are? I thought those dependencies would already be linked into libxlsxio_read.a which is a statically

Re: Is "auto t=T();" not the same as "T t;"?

2022-10-26 Thread Andrey Zherikov via Digitalmars-d-learn
On Wednesday, 26 October 2022 at 04:40:17 UTC, Salih Dincer wrote: On Tuesday, 25 October 2022 at 13:51:30 UTC, Andrey Zherikov wrote: Does the second piece of code shows a bug or my expectation is not correct (and why if so)? As a result, if this is a bug, Andrey has the right to report it.

Re: Is "auto t=T();" not the same as "T t;"?

2022-10-25 Thread Andrey Zherikov via Digitalmars-d-learn
On Tuesday, 25 October 2022 at 14:53:50 UTC, Adam D Ruppe wrote: On Tuesday, 25 October 2022 at 13:51:30 UTC, Andrey Zherikov wrote: A[] a = [A.init]; This is a problem - this is referring to a static array instance, shared across all copies of B. You almost certainly don't want this.

Re: Is "auto t=T();" not the same as "T t;"?

2022-10-25 Thread Andrey Zherikov via Digitalmars-d-learn
On Tuesday, 25 October 2022 at 14:53:50 UTC, Adam D Ruppe wrote: But just don't do this. Only basic values and immutable strings are good to initialize this way. With the array or class objects, you're liable to get some shared thing. If you change this to be initialized in a constructor

Is "auto t=T();" not the same as "T t;"?

2022-10-25 Thread Andrey Zherikov via Digitalmars-d-learn
I have the following types (simplified version of my code): ```d struct A { int[] i; } struct B { A[] a = [A.init]; } ``` This code: ```d auto b1 = B.init; b1.a[0].i ~= 1; b1.a ~= A.init; b1.a[0].i ~= 11; b1.a[1].i ~= 12; b1.writeln; auto b2 = B();

Re: Hipreme's #2 Tip of the day - Reducing .di files dependency

2022-10-25 Thread Andrey Zherikov via Digitalmars-d-learn
On Sunday, 23 October 2022 at 20:12:46 UTC, Hipreme wrote: This will greatly reduce the number of import and dependencies you need if you ever need to distribute a library. Could you elaborate more on the benefit? Does it reduce compilation time for dependency? If so then how much?

Re: rotate left an array

2022-10-03 Thread Andrey Zherikov via Digitalmars-d-learn
On Monday, 3 October 2022 at 18:09:05 UTC, Fausto wrote: Hello all, I am trying to rotate left an array. I found a very basic way, and I am not sure if there is something clever than this :) maybe using slices... the external for represents how many times you are rotating (in this case 2).

Re: How I can pass the WndProc as a parameter?

2022-09-10 Thread Andrey Zherikov via Digitalmars-d-learn
On Saturday, 10 September 2022 at 10:39:12 UTC, Injeckt wrote: server.d(29): Error: function `server.WndProc(void* hwnd, uint message, uint wParam, int lParam)` is not callable using argument types `()` I think you need to get address of a function: ```d wndclass.lpfnWndProc = ```

Re: Validate static asserts

2022-09-09 Thread Andrey Zherikov via Digitalmars-d-learn
On Friday, 9 September 2022 at 15:22:30 UTC, Ali Çehreli wrote: I added and removed '&& false' to every 'static assert' condition manually one by one. :/ It's not CI-friendly :( Perhaps a new compiler switch can compile every 'static assert' with an automatic 'false' and dump all their text

Validate static asserts

2022-09-09 Thread Andrey Zherikov via Digitalmars-d-learn
I have bunch of `static assert(, )` in my code and would like to validate that specific code triggers specific assert by checking what `` is thrown. Right now I do `static assert(!__traits(compiles, { }));` but since `` might not compile due to many different reasons, I might not be testing

Re: typeof(func!0) != typeof(func!0())

2022-08-22 Thread Andrey Zherikov via Digitalmars-d-learn
On Monday, 22 August 2022 at 17:40:59 UTC, Andrey Zherikov wrote: I'm providing a struct (`U` in examples above) with some API and I'm looking for this struct as an UDA (through `hasUDA`/`getUDAs`). And actually I already have a mix of member-functions and free-functions in my API so it's

Re: typeof(func!0) != typeof(func!0())

2022-08-22 Thread Andrey Zherikov via Digitalmars-d-learn
On Monday, 22 August 2022 at 16:42:50 UTC, Paul Backus wrote: It's probably not worth completely changing your API design just to work around this issue. Also, even if you do this, it is still possible for a user to run into a same problem with a member function of one of their own types; for

Re: typeof(func!0) != typeof(func!0())

2022-08-22 Thread Andrey Zherikov via Digitalmars-d-learn
On Monday, 22 August 2022 at 15:20:46 UTC, Paul Backus wrote: On Monday, 22 August 2022 at 14:43:24 UTC, Andrey Zherikov wrote: But the question is still opened: why is `typeof(U().func!0)` not the same as `typeof(U().func!0())`? Probably because if it were the same, it would be completely

Re: typeof(func!0) != typeof(func!0())

2022-08-22 Thread Andrey Zherikov via Digitalmars-d-learn
On Monday, 22 August 2022 at 15:15:22 UTC, Paul Backus wrote: My first instinct is to say that this is the user's mistake. UDAs are not evaluated like normal expressions, and anyone using UDAs is going to have to learn that sooner or later. My feeling was that UDA expression is just an

Re: typeof(func!0) != typeof(func!0())

2022-08-22 Thread Andrey Zherikov via Digitalmars-d-learn
But the question is still opened: why is `typeof(U().func!0)` not the same as `typeof(U().func!0())`? There is also inconsistency within UFCS - type depends on whether function is a member or standalone (even if I replace `auto ref` with `ref U`): ```d struct U { ref U func(int i)() {

Re: typeof(func!0) != typeof(func!0())

2022-08-22 Thread Andrey Zherikov via Digitalmars-d-learn
On Monday, 22 August 2022 at 12:04:09 UTC, Paul Backus wrote: On Monday, 22 August 2022 at 11:24:59 UTC, Andrey Zherikov wrote: On Monday, 22 August 2022 at 06:01:11 UTC, JG wrote: Why not just change to: alias type = typeof(U().func!0()); This is user's code and `U().func!0` is legit

Re: typeof(func!0) != typeof(func!0())

2022-08-22 Thread Andrey Zherikov via Digitalmars-d-learn
On Monday, 22 August 2022 at 06:01:11 UTC, JG wrote: Why not just change to: alias type = typeof(U().func!0()); This is user's code and `U().func!0` is legit syntax.

Re: typeof(func!0) != typeof(func!0())

2022-08-22 Thread Andrey Zherikov via Digitalmars-d-learn
On Monday, 22 August 2022 at 05:25:50 UTC, Ali Çehreli wrote: This is where the @property keyword makes a difference: @property auto ref func(int i)() { return this; } Now U().func!0 will be a call in your expression. Is original `U().func!0` not a call? But I think

typeof(func!0) != typeof(func!0())

2022-08-21 Thread Andrey Zherikov via Digitalmars-d-learn
I have this simple code: ```d struct U { auto ref func(int i)() { return this; } } void main() { { alias type = typeof(U().func!0); pragma(msg, type); // pure nothrow @nogc ref @safe U() return pragma(msg, is(type : U)); // false auto u =

Re: My programs issues

2022-08-10 Thread Andrey Zherikov via Digitalmars-d-learn
On Wednesday, 10 August 2022 at 14:08:59 UTC, pascal111 wrote: This version has modern features, it's 1) functional 2) goto-less. There is nothing modern ;-D

Re: My programs issues

2022-08-10 Thread Andrey Zherikov via Digitalmars-d-learn
On Wednesday, 10 August 2022 at 13:34:53 UTC, pascal111 wrote: So, the program will be like this: Is this clearer? ```d import std.stdio; import std.algorithm; import std.range; double getNumber() { double x; write("Enter a number: "); readf(" %s\n", ); writeln; return

Re: A look inside "filter" function defintion

2022-08-02 Thread Andrey Zherikov via Digitalmars-d-learn
On Monday, 1 August 2022 at 23:35:13 UTC, pascal111 wrote: I think this line needs explanation: '''D return FilterResult!(unaryFun!predicate, Range)(range); ''' Create an object of type `FilterResult!(unaryFun!predicate, Range)` by passing `range` to its ctor, then return that object.

Re: Breaking ";" rule with lambda functions

2022-08-02 Thread Andrey Zherikov via Digitalmars-d-learn
On Monday, 1 August 2022 at 14:15:31 UTC, pascal111 wrote: We all know the strange syntax of lambda function within filter algorithm like "auto r = chain(a, b).filter!(a => a > 0);". TBH I don't find lambda syntax strange - it's pretty nice and there are two forms (unlike in C++): short one

Re: Is this a violation of const?

2022-07-29 Thread Andrey Zherikov via Digitalmars-d-learn
On Friday, 29 July 2022 at 22:16:26 UTC, H. S. Teoh wrote: This totally makes sense. Thanks for explanation!

Re: A converting problem in using "among" with arrays

2022-07-29 Thread Andrey Zherikov via Digitalmars-d-learn
On Friday, 29 July 2022 at 22:09:47 UTC, pascal111 wrote: I next code, we have a data type problem "54.among(to!uint[10](y)).writeln;": module main; import std.stdio; import std.string; import std.conv; import dcollect; import std.math; import std.algorithm; int main(string[] args) {

Is this a violation of const?

2022-07-29 Thread Andrey Zherikov via Digitalmars-d-learn
In the example below `func` changes its `const*` argument. Does this violates D's constness? ```d import std; struct S { string s; void delegate(string s) update; } void func(const S* s) { writeln(*s); s.update("func"); writeln(*s); } void main() { auto s =

Re: Bug in dmd?

2022-06-16 Thread Andrey Zherikov via Digitalmars-d-learn
On Wednesday, 15 June 2022 at 16:53:13 UTC, mw wrote: Create a simple test case, file bug at: https://issues.dlang.org/ I tried. No luck.

Re: Bug in dmd?

2022-06-15 Thread Andrey Zherikov via Digitalmars-d-learn
On Wednesday, 15 June 2022 at 09:24:35 UTC, Dukc wrote: Now when I think of it, perhaps the fact that private `printHelp` has the same name as the public `printHelp` is somehow confusing dmd. If you try to rename the private `printHelp` and it's call in the public one to something else, you

Re: Bug in dmd?

2022-06-15 Thread Andrey Zherikov via Digitalmars-d-learn
On Wednesday, 15 June 2022 at 09:21:28 UTC, user1234 wrote: On Tuesday, 14 June 2022 at 13:39:12 UTC, Andrey Zherikov wrote: I have [pretty simple code in my library](https://github.com/andrey- [Line (2)

Re: Bug in dmd?

2022-06-15 Thread Andrey Zherikov via Digitalmars-d-learn
On Wednesday, 15 June 2022 at 07:38:36 UTC, JG wrote: I tried to reproduce it but wasn't able (I guess it is some interplay with the rest of your code): I tried this first but got the same result as you. I think my small library overheats DMD's template engine.

Re: Bug in dmd?

2022-06-14 Thread Andrey Zherikov via Digitalmars-d-learn
On Tuesday, 14 June 2022 at 21:44:48 UTC, Dukc wrote: No idea. The functions seems indeed to be exactly the same, so I assume this is a DMD bug. It cannot be a bug in `std.sumtype`, since that would trigger in both of the templates. This definitely triggers some bug in DMD because this

Bug in dmd?

2022-06-14 Thread Andrey Zherikov via Digitalmars-d-learn
I have [pretty simple code in my library](https://github.com/andrey-zherikov/argparse/blob/bug/source/argparse/help.d#L27-L47): ```d alias CC = SumType!(AA,BB); struct AA {} struct BB { CC[] c; } private void ppp(T, Output)(auto ref Output output, in CommandArguments!T cmd, in Config

Re: Templatized delegates

2022-05-31 Thread Andrey Zherikov via Digitalmars-d-learn
On Tuesday, 31 May 2022 at 23:15:24 UTC, Andrey Zherikov wrote: On Tuesday, 31 May 2022 at 21:53:17 UTC, Paul Backus wrote: If you want compile-time polymorphism, three's no other way. Yes, I want compile-time polymorphism. In case if `S.doSomething` is NOT template function then the

Re: Templatized delegates

2022-05-31 Thread Andrey Zherikov via Digitalmars-d-learn
On Tuesday, 31 May 2022 at 22:34:41 UTC, Christian Köstlin wrote: Would it help to not create the delegate in R's constructor, but feed the delegate into it (and create the delegate outside). This would also resemble your description (R is a delegate holder) more. That's possible but the

Re: Templatized delegates

2022-05-31 Thread Andrey Zherikov via Digitalmars-d-learn
On Tuesday, 31 May 2022 at 21:53:17 UTC, Paul Backus wrote: If you want compile-time polymorphism, three's no other way. Yes, I want compile-time polymorphism. ```d import std.stdio; struct S { // function that should be called from delegate void doSomething(T)(T value) {

Templatized delegates

2022-05-31 Thread Andrey Zherikov via Digitalmars-d-learn
I have tightly coupled code which I'd like to decouple but I'm a bit stuck. For simplicity, I reduced the amount of code to something simple to understand. So I have a struct `S` that has templated member function that does something. On the other side I have delegate holder `R` - this

Re: How to fix "typesafe variadic function parameter"?

2022-05-24 Thread Andrey Zherikov via Digitalmars-d-learn
On Tuesday, 24 May 2022 at 22:51:50 UTC, Adam Ruppe wrote: On Tuesday, 24 May 2022 at 22:46:55 UTC, Andrey Zherikov wrote: return S(s); return S(s.dup); The variadic lives in a temporary array that expires at the end of the function. So copying it out to the GC lets it live on.

How to fix "typesafe variadic function parameter"?

2022-05-24 Thread Andrey Zherikov via Digitalmars-d-learn
How to fix this? ```d struct S { string[] s; } auto foo(string[] s...) // Error: typesafe variadic function parameter `s` of type `string[]` cannot be marked `return` { return S(s); } void main() { import std.stdio: writeln; writeln(foo("Hello D")); } ```

Re: Is T.init allowed?

2022-04-29 Thread Andrey Zherikov via Digitalmars-d-learn
On Friday, 29 April 2022 at 12:05:35 UTC, Dennis wrote: On Friday, 29 April 2022 at 11:30:49 UTC, Andrey Zherikov wrote: Is it a compiler issue so this shouldn't be allowed? Members called `init` are in the process of being deprecated, see: https://github.com/dlang/dmd/pull/12512 That's

Is T.init allowed?

2022-04-29 Thread Andrey Zherikov via Digitalmars-d-learn
Seems compiler allows custom nested type with `init` name: ```d struct T { struct init {} int b = 2; } void main() { T t; writeln(t);// T(2) //writeln(T.init); // Error: cannot pass type `init` as a function argument } ``` Is it a compiler issue so this shouldn't

Re: Calling template member function?

2022-04-19 Thread Andrey Zherikov via Digitalmars-d-learn
On Tuesday, 19 April 2022 at 20:29:01 UTC, Steven Schveighoffer wrote: You can work around the dual context, if you are OK with passing the second context explicitly. The easiest way is to move the member function to a UFCS function. an example: ```d struct X { int x; void

Re: Calling template member function?

2022-04-19 Thread Andrey Zherikov via Digitalmars-d-learn
On Tuesday, 19 April 2022 at 19:07:37 UTC, Ali Çehreli wrote: On 4/19/22 11:18, Andrey Zherikov wrote: > Is there a way/workaround to achieve the desired behavior? Can you describe the goal a little more. For example, I assume you really need a more useful lambda although the example you

Re: Calling template member function?

2022-04-19 Thread Andrey Zherikov via Digitalmars-d-learn
I get the same error even with just `struct`: ```d struct S { static void f_new(alias func)() { func(); } } void f_new(alias func)() { func(); } void f(FUNC)(FUNC func) { f_new!(() => func()); // works // S.f_new!(() => func()); // doesn't work } ```

Re: Calling template member function?

2022-04-19 Thread Andrey Zherikov via Digitalmars-d-learn
On Tuesday, 19 April 2022 at 18:18:26 UTC, Andrey Zherikov wrote: Is there a way/workaround to achieve the desired behavior? Those two bugs pointed above were reported in 2017 and 2011 (!) which makes me think that they won't be fixed any time soon (I'm not sure that they are actually the same

Re: Calling template member function?

2022-04-19 Thread Andrey Zherikov via Digitalmars-d-learn
On Tuesday, 19 April 2022 at 16:38:42 UTC, Steven Schveighoffer wrote: On 4/19/22 11:46 AM, Paul Backus wrote: If you remove `static` from `f_new`, you get an error message talking about this explicitly: Interesting that `static` does anything there, since it's a no-op. -Steve I put

Calling template member function?

2022-04-19 Thread Andrey Zherikov via Digitalmars-d-learn
I want to migrate my library API from standalone function that takes delegate as argument to a template member function that takes delegate as a template parameter but compiler errors out. Here is code example: ```d import std.stdio; template T(P) { static void f_new(alias func)() {

Re: How to unit-test behavior under "version"?

2022-03-30 Thread Andrey Zherikov via Digitalmars-d-learn
On Wednesday, 30 March 2022 at 04:15:24 UTC, Paul Backus wrote: Probably the easiest way is to split it into two functions ```d void f() { version (foo) fVersionFoo(); else fDefault(); } void fVersionFoo() { /* ... */ } void fDefault() { /* ... */ } ``` Then

Re: How to unit-test behavior under "version"?

2022-03-29 Thread Andrey Zherikov via Digitalmars-d-learn
On Tuesday, 29 March 2022 at 20:20:46 UTC, Steven Schveighoffer wrote: So keep in mind that versions are *module-wide*, and usually *compilation-wide*. Which means that you won't be testing multiple version configurations in the same build. ... In the case where version is going to *affect

How to unit-test behavior under "version"?

2022-03-29 Thread Andrey Zherikov via Digitalmars-d-learn
I have a function below (just an example). What's the recommended way to unit-test it for all `version` cases? ```d void f() { import std.stdio: writeln; version(foo) writeln("foo"); else writeln("no foo"); } ```

Re: Template with default parameter

2022-03-11 Thread Andrey Zherikov via Digitalmars-d-learn
On Friday, 11 March 2022 at 12:53:56 UTC, Adam D Ruppe wrote: On Friday, 11 March 2022 at 12:01:27 UTC, Andrey Zherikov wrote: There is some inconsistency between templates and template functions as this works as I want to: Yeah, functions have the special feature of implicit instantiation

Re: Template with default parameter

2022-03-11 Thread Andrey Zherikov via Digitalmars-d-learn
There is some inconsistency between templates and template functions as this works as I want to: ```d import std.stdio; void f(int i=3)() { writeln(i); } void main() { f!1; // 1 f!(); // 3 f;// 3 } ```

Re: Template with default parameter

2022-03-11 Thread Andrey Zherikov via Digitalmars-d-learn
On Friday, 11 March 2022 at 07:06:15 UTC, bauss wrote: Create an alias for T!() is the best you can do. Ex. ``` alias t = T!(); ``` There isn't really any better method as far as I know. I'd like to preserve the name (`t` != `T`) but `alias T = T!()` gives me `Error: declaration `T` is

Template with default parameter

2022-03-10 Thread Andrey Zherikov via Digitalmars-d-learn
I have simple template: ```d template T(int i=3) { mixin template M(int m) { enum t = i; } } { mixin T!1.M!1; pragma(msg, t); // 1 } { mixin T!().M!1; pragma(msg, t); // 3 } { mixin T.M!1; // Error: identifier `M` of `T.M` is not

Re: How to check that a member function is generated by compiler?

2022-02-25 Thread Andrey Zherikov via Digitalmars-d-learn
On Friday, 25 February 2022 at 16:24:46 UTC, Paul Backus wrote: On Friday, 25 February 2022 at 14:25:22 UTC, Andrey Zherikov wrote: Another interesting observation - is there any explanation why `typeof` returns different results for generated `opAssign`? [...] If I move `foreach` loop

Re: How to check that a member function is generated by compiler?

2022-02-25 Thread Andrey Zherikov via Digitalmars-d-learn
On Friday, 25 February 2022 at 05:25:14 UTC, Ali Çehreli wrote: On 2/24/22 20:44, Andrey Zherikov wrote: How can I check that `opAssign` is generated by compiler and doesn't exist in the original code? I think this one: https://dlang.org/phobos/std_traits.html#hasElaborateAssign Ali

How to check that a member function is generated by compiler?

2022-02-24 Thread Andrey Zherikov via Digitalmars-d-learn
This code ```d import std.sumtype: SumType; struct A { SumType!int b; } static foreach(sym; __traits(allMembers, A)) pragma(msg,sym); ``` prints ``` b opAssign ``` How can I check that `opAssign` is generated by compiler and doesn't exist in the original code?

Re: keyword as struct field

2022-02-20 Thread Andrey Zherikov via Digitalmars-d-learn
On Sunday, 20 February 2022 at 11:08:55 UTC, partypooper wrote: Hello, I'm new to D. Title is self described, is it possible to use keyword as struct field? Maybe it is XYproblem, so here I will describe a little what I need. I'm parsing some json currently with

Re: Member function forwarding

2022-02-17 Thread Andrey Zherikov via Digitalmars-d-learn
On Thursday, 17 February 2022 at 20:59:43 UTC, Andrey Zherikov wrote: Another question: does `auto foo(ARGS...)(ARGS args) { return a.foo(args); }` correctly forward `ref`, `const` etc. arguments? Actually the answer is `NO`. I have to do `auto foo(ARGS...)(auto ref ARGS args) { return

Member function forwarding

2022-02-17 Thread Andrey Zherikov via Digitalmars-d-learn
I have a struct that has struct data member and I'd like to forward some (not all) functions from internal data member. Some thing like this: ```d struct A { void foo() const { writeln("A.foo"); } } struct B { A a; auto foo(ARGS...)(ARGS args) { return a.foo(args); } // alias

Re: Template sequence parameter and default value

2022-01-27 Thread Andrey Zherikov via Digitalmars-d-learn
On Thursday, 27 January 2022 at 03:19:59 UTC, Jaime wrote: You can accomplish this by heading off the template sequence parameter with several default template parameters. If you need them all under one name, you can recombine them in the function body with std.meta.AliasSeq, the effective

Template sequence parameter and default value

2022-01-26 Thread Andrey Zherikov via Digitalmars-d-learn
What is the best way to emulate a default value for template sequence parameter of a function? I want something like this: ```d void foo(MODULES... = __MODULE__)() {} // these should work: foo!(module1, module2); foo!(module1); foo(); // this should use current module (__MODULE__)

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: Any workaround for "closures are not yet supported in CTFE"?

2021-12-07 Thread Andrey Zherikov via Digitalmars-d-learn
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 allocation). Note the type of 'dg' is changed

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

2021-12-07 Thread Andrey Zherikov via Digitalmars-d-learn
I have a struct `A` that stores delegates. The problem is that I'm getting "`Error: closures are not yet supported in CTFE`" if I create an compile-time constant value of type `A`: ```d struct A { void delegate()[] dg; } auto createDelegate(string s) { return { s.writeln; }; //

Re: need `this` on trying to use class method with parallelism Task! or task!

2021-11-14 Thread Andrey Zherikov via Digitalmars-d-learn
On Sunday, 14 November 2021 at 16:55:24 UTC, Alexey wrote: Remove "!"

Re: need `this` on trying to use class method with parallelism Task! or task!

2021-11-14 Thread Andrey Zherikov via Digitalmars-d-learn
On Sunday, 14 November 2021 at 14:41:21 UTC, Alexey wrote: On Sunday, 14 November 2021 at 14:24:00 UTC, Andrey Zherikov wrote: On Sunday, 14 November 2021 at 12:01:36 UTC, Alexey wrote: You just need two changes: - make `threadFunc` function static: `static void threadFunc()` - use `task`

Re: need `this` on trying to use class method with parallelism Task! or task!

2021-11-14 Thread Andrey Zherikov via Digitalmars-d-learn
On Sunday, 14 November 2021 at 12:01:36 UTC, Alexey wrote: You just need two changes: - make `threadFunc` function static: `static void threadFunc()` - use `task` instead of `Task`: `auto t1 = task!threadFunc;`

Re: How to get the location (file, line) of UDA without parens?

2021-11-14 Thread Andrey Zherikov via Digitalmars-d-learn
On Sunday, 14 November 2021 at 07:06:25 UTC, user1234 wrote: On Sunday, 14 November 2021 at 05:12:58 UTC, Andrey Zherikov wrote: Here is my code: [...] `W()` (2) works as expected but is it possible to achieve the same without parenthesis so `getAttributes` trait returns `tuple(L("app.d",

How to get the location (file, line) of UDA without parens?

2021-11-13 Thread Andrey Zherikov via Digitalmars-d-learn
Here is my code: ```d struct L { string file; size_t line; } auto W(string f = __FILE__,size_t l= __LINE__)() { return L(f,l); } struct A { @W // (1): line# 16 { int a; int b; } @W() // (2): line# 21 { int c; int d; } } void main() {

Re: How to return a reference to structs?

2021-11-06 Thread Andrey Zherikov via Digitalmars-d-learn
On Saturday, 6 November 2021 at 13:57:47 UTC, Ali Çehreli wrote: Have you considered std.range.indexed, which should at least cover the case of accessing: https://dlang.org/phobos/std_range.html#indexed I don't need to iterate over whole collection, but subset of it. Can you give a little

Re: How to return a reference to structs?

2021-11-06 Thread Andrey Zherikov via Digitalmars-d-learn
On Saturday, 6 November 2021 at 04:28:05 UTC, Ali Çehreli wrote: On 11/5/21 5:43 PM, Andrey Zherikov wrote: In case others want to work, here are the modules that need to be imported: import std.algorithm; import std.range; import std.stdio; > struct A {} > struct B > { > A[] ar =

Re: How to return a reference to structs?

2021-11-05 Thread Andrey Zherikov via Digitalmars-d-learn
On Friday, 5 November 2021 at 20:13:02 UTC, Andrey Zherikov wrote: On Friday, 5 November 2021 at 19:49:50 UTC, Ali Çehreli wrote: Indexes are typed as size_t, which is ulong for 64-bit and uint for 32-bit. If idx elements were indeed indexes, it should have been typed as size_t: size_t[] idx

Re: How to return a reference to structs?

2021-11-05 Thread Andrey Zherikov via Digitalmars-d-learn
On Friday, 5 November 2021 at 19:49:50 UTC, Ali Çehreli wrote: Indexes are typed as size_t, which is ulong for 64-bit and uint for 32-bit. If idx elements were indeed indexes, it should have been typed as size_t: size_t[] idx = [1,3,4]; // <-- Here Ah, good point. Thanks!

Re: How to return a reference to structs?

2021-11-04 Thread Andrey Zherikov via Digitalmars-d-learn
On Thursday, 4 November 2021 at 13:03:54 UTC, Paul Backus wrote: On Thursday, 4 November 2021 at 11:26:30 UTC, Andrey Zherikov wrote: I have the following code example: ```d struct A{} A[5] a; ulong[] idx = [1,3,4]; auto get() { return idx.map!(_ => a[_]); } foreach(i; 0 .. a.length)

Re: How do you declare manifest constants?

2021-11-04 Thread Andrey Zherikov via Digitalmars-d-learn
On Thursday, 4 November 2021 at 17:09:31 UTC, Steven Schveighoffer wrote: D doesn't have any equivalent for this. Is it possible to add this feature having `-C VERSION="1.2.3"` (`-D` is already used) to be equal to `enum VERSION="1.2.3"` in global namespace? The closest you can get is to

How do you declare manifest constants?

2021-11-04 Thread Andrey Zherikov via Digitalmars-d-learn
I want to embed some info from build system - version info, for example. I can easily do this with C++ compiler by `-DVERSION="1.2.3"` but there is no such an option in dmd. So I'm wondering how do people workaround this? I see only one way: generate a file and add it to the build (file might

Re: How to return a reference to structs?

2021-11-04 Thread Andrey Zherikov via Digitalmars-d-learn
On Thursday, 4 November 2021 at 13:03:54 UTC, Paul Backus wrote: Have the lambda return by reference: ```d auto get() { return idx.map!(ref (i) => a[i]); } ``` That works, thanks!

How to return a reference to structs?

2021-11-04 Thread Andrey Zherikov via Digitalmars-d-learn
I have the following code example: ```d struct A{} A[5] a; ulong[] idx = [1,3,4]; auto get() { return idx.map!(_ => a[_]); } foreach(i; 0 .. a.length) write([i], " "); writeln; foreach(ref b; get()) write(, " "); writeln; ``` How can I change `get` function so it returns the

Re: Does associative array change the location of values?

2021-10-30 Thread Andrey Zherikov via Digitalmars-d-learn
On Saturday, 30 October 2021 at 00:52:23 UTC, Imperatorn wrote: On Saturday, 30 October 2021 at 00:49:04 UTC, Stanislav Blinov wrote: On Friday, 29 October 2021 at 21:00:48 UTC, Steven Schveighoffer wrote: This is incorrect, the buckets are each heap allocated. Just the array of bucket

Re: Does associative array change the location of values?

2021-10-29 Thread Andrey Zherikov via Digitalmars-d-learn
On Friday, 29 October 2021 at 17:58:24 UTC, Paul Backus wrote: No, the AA does not guarantee that the value will remain in the same location. Inserting or removing *any* keys could cause the AA to resize, which may change the locations of all of its values. However, you do not have to worry

Does associative array change the location of values?

2021-10-29 Thread Andrey Zherikov via Digitalmars-d-learn
I want to have a pointer to a value in an associative array. Does AA guarantee that the value will remain at the same address all the time unless I remove the corresponding key? I couldn't find any guarantees similar to C++ iterator invalidation in D Language Reference.

Re: Cannot access frame pointer of a struct with member function

2021-05-09 Thread Andrey Zherikov via Digitalmars-d-learn
On Sunday, 9 May 2021 at 17:53:07 UTC, SealabJaster wrote: On Sunday, 9 May 2021 at 17:37:40 UTC, Andrey Zherikov wrote: Is this a bug? My best guess is that, since the struct doesn't have any member functions the compiler has decided that the struct doesn't need any access to the main

Cannot access frame pointer of a struct with member function

2021-05-09 Thread Andrey Zherikov via Digitalmars-d-learn
Compilation of this code: ```d auto foo(T)() { return T(); // Error: cannot access frame pointer of `onlineapp.main.T` } void main() { struct T { int a=1; void argsFunc(int a) {} // (1) } pragma(msg,foo!T()); } ``` fails with this error: ```

  1   2   >