Re: Return values from auto function

2020-11-07 Thread Andrey Zherikov via Digitalmars-d-learn
On Saturday, 7 November 2020 at 01:50:15 UTC, Jesse Phillips wrote: On Friday, 6 November 2020 at 15:06:18 UTC, Andrey Zherikov wrote: On Friday, 6 November 2020 at 14:58:40 UTC, Jesse Phillips wrote: On Friday, 6 November 2020 at 14:20:40 UTC, Andrey Zherikov wrote: This issue seems hit the

Re: Return values from auto function

2020-11-06 Thread Andrey Zherikov via Digitalmars-d-learn
On Friday, 6 November 2020 at 14:58:40 UTC, Jesse Phillips wrote: On Friday, 6 November 2020 at 14:20:40 UTC, Andrey Zherikov wrote: This issue seems hit the inability to implicitly convert custom types. May be it makes more sense to ask in a separate thread. The return type must be the same

Implicit conversion to templatized type

2020-11-06 Thread Andrey Zherikov via Digitalmars-d-learn
There is a way to implicitly convert non-template user type, for example: struct S2 { @property S1 s1() { return S1(); } alias s1 this; } struct S1 {} S1 f() { return S2(); } // implicit conversion from S2 to S1 But how can I achieve the

Re: Return values from auto function

2020-11-06 Thread Andrey Zherikov via Digitalmars-d-learn
On Friday, 6 November 2020 at 12:03:01 UTC, Paul Backus wrote: You can't. Both return values have to have the same type, which means the failure function has to be able to return more than one type, which means it has to be a template. This issue seems hit the inability to implicitly convert

Re: Return values from auto function

2020-11-06 Thread Andrey Zherikov via Digitalmars-d-learn
On Friday, 6 November 2020 at 13:59:58 UTC, gbram wrote: On Friday, 6 November 2020 at 12:03:01 UTC, Paul Backus wrote: On Friday, 6 November 2020 at 10:51:20 UTC, Andrey Zherikov wrote: How can I make the original code compilable without templatizing `failure` function? You can't. Both

Return values from auto function

2020-11-06 Thread Andrey Zherikov via Digitalmars-d-learn
I have auto function 'f' that might return either an error (with some text) or a result (with some value). The problem is that the type of the error is not the same as the type of result so compilation fails. Here is my code: -- struct Result(T) { struct Success {

Re: Recommended way to use __FILE__/__LINE__ etc.

2020-10-22 Thread Andrey Zherikov via Digitalmars-d-learn
Thanks for all your answers!

Recommended way to use __FILE__/__LINE__ etc.

2020-10-22 Thread Andrey Zherikov via Digitalmars-d-learn
There are two ways how __FILE__, __LINE__ etc. can se used in function parameters: as regular parameters and as template parameters: void rt(string file=__FILE__, int line=__LINE__, string func=__FUNCTION__) { writeln(file,"(",line,"): ",func); } void ct(string file=__FILE__, int

Re: Is there a way to force emitting of stack frame for a specific function?

2020-10-08 Thread Andrey Zherikov via Digitalmars-d-learn
On Thursday, 8 October 2020 at 13:13:16 UTC, Imperatorn wrote: Can you share some code? You don't want to use try-catch? Here is minimal example that illustrates this: = import std.stdio; void do_assert() { assert(false); } void stackFrame() { //pragma(inline, false);

Is there a way to force emitting of stack frame for a specific function?

2020-10-08 Thread Andrey Zherikov via Digitalmars-d-learn
I'm trying to implement some mix of native and custom stack trace dump for exception logging: I want to print custom text instead of some stack frames. To clarify more: I have a function that I want to substitute with other text in stack dump and this function has to be in stack dump to make

Re: How to convert member function to free function?

2020-09-18 Thread Andrey Zherikov via Digitalmars-d-learn
On Friday, 18 September 2020 at 18:20:41 UTC, Andrey Zherikov wrote: How can I rewrite foo() function as a free-function that won't cause struct copying? I found solution: struct S { int i = -1; this(int n) {i=n;writeln(," ",i," ",__PRETTY_FUNCTION__);} this(ref return

Re: How to convert member function to free function?

2020-09-18 Thread Andrey Zherikov via Digitalmars-d-learn
On Friday, 18 September 2020 at 18:43:38 UTC, H. S. Teoh wrote: Why can't you return by ref, which would also avoid the copying? ref S foo(return ref S s) { return s; } Compiler errors out: onlineapp.d(9): Error: function onlineapp.foo(return ref S s) is not callable using argument

How to convert member function to free function?

2020-09-18 Thread Andrey Zherikov via Digitalmars-d-learn
How can I rewrite foo() function as a free-function that won't cause struct copying? My simplified code is: struct S { ref S foo() return { return this; } } void main() { S().foo().foo().foo(); } If I write it like "auto foo(S s) { return s; }" then

Re: Why is dtor called for lazy parameter?

2020-09-18 Thread Andrey Zherikov via Digitalmars-d-learn
On Friday, 18 September 2020 at 13:28:39 UTC, Simen Kjærås wrote: Indeed. As we can see from the output, first do_lazy() is called from test.main, then create() is called (this happens inside do_lazy, as s is lazy). When create() returns, the scoped!S you created goes out of scope and is

Re: Why is dtor called for lazy parameter?

2020-09-18 Thread Andrey Zherikov via Digitalmars-d-learn
On Friday, 18 September 2020 at 11:31:39 UTC, Simen Kjærås wrote: On Friday, 18 September 2020 at 10:43:47 UTC, Andrey Zherikov wrote: Why is dtor called before returning from do_lazy function - see (1)? It seems cause uninitialized parameter in do_something call after it in (2)-(3). As ikod

Re: Why is dtor called for lazy parameter?

2020-09-18 Thread Andrey Zherikov via Digitalmars-d-learn
On Friday, 18 September 2020 at 10:54:41 UTC, ikod wrote: On Friday, 18 September 2020 at 10:43:47 UTC, Andrey Zherikov wrote: I have this code: == class S ... auto create() { writeln("-> ",__PRETTY_FUNCTION__); scope(exit) writeln("<- ",__PRETTY_FUNCTION__); return

Why is dtor called for lazy parameter?

2020-09-18 Thread Andrey Zherikov via Digitalmars-d-learn
I have this code: == class S { int i = -1; this(int n) { i = n; writeln(i," ",__PRETTY_FUNCTION__); } ~this() { writeln(i," ",__PRETTY_FUNCTION__); } } auto create() { writeln("-> ",__PRETTY_FUNCTION__); scope(exit) writeln("<- ",__PRETTY_FUNCTION__); return

Re: Bug in import(...) on Windows?

2020-09-02 Thread Andrey Zherikov via Digitalmars-d-learn
On Wednesday, 2 September 2020 at 20:55:34 UTC, Andrey Zherikov wrote: I think the issue is here: https://github.com/dlang/dmd/blob/master/src/dmd/root/filename.d#L736-L748 Yes, issue is there. This change (removal of "c == '\\' || ") fixes it: diff --git a/src/dmd/root/filename.d

Re: Bug in import(...) on Windows?

2020-09-02 Thread Andrey Zherikov via Digitalmars-d-learn
On Wednesday, 2 September 2020 at 19:13:47 UTC, Adam D. Ruppe wrote: On Wednesday, 2 September 2020 at 18:40:55 UTC, Andrey Zherikov wrote: If I provide -Jfoo to dmd, doesn't it mean my consent to use the contents of directory foo? Yeah, but dmd has been inconsistent on platforms about if it

Re: Bug in import(...) on Windows?

2020-09-02 Thread Andrey Zherikov via Digitalmars-d-learn
On Wednesday, 2 September 2020 at 20:23:15 UTC, Steven Schveighoffer wrote: What I'm wondering is if it needs to be ./file instead of .\file. Can you hard code that and see if it works? This actually works: pragma(msg, import("file")); pragma(msg, buildPath(".", "file"));

Re: Bug in import(...) on Windows?

2020-09-02 Thread Andrey Zherikov via Digitalmars-d-learn
On Wednesday, 2 September 2020 at 17:47:47 UTC, Adam D. Ruppe wrote: On Wednesday, 2 September 2020 at 17:39:04 UTC, Andrey Zherikov wrote: Is this a bug in dmd? I think it is an old bug filed (I can't find it though) about inconsistent platform behavior but it is allowed by spec for the

Re: How to create compile-time container?

2020-09-02 Thread Andrey Zherikov via Digitalmars-d-learn
On Wednesday, 2 September 2020 at 17:40:55 UTC, Andrey Zherikov wrote: On Wednesday, 2 September 2020 at 14:38:30 UTC, Steven Schveighoffer wrote: Here is what I would do instead: Thanks, I'll try it. This actually works! Thanks a lot!

Re: How to create compile-time container?

2020-09-02 Thread Andrey Zherikov via Digitalmars-d-learn
On Wednesday, 2 September 2020 at 14:38:30 UTC, Steven Schveighoffer wrote: Here is what I would do instead: Thanks, I'll try it.

Re: Bug in import(...) on Windows?

2020-09-02 Thread Andrey Zherikov via Digitalmars-d-learn
Adding some verbosity: pragma(msg, import("file")); pragma(msg, buildPath(".", "file")); pragma(msg, import(buildPath(".", "file"))); Result on Ubuntu: === hello ./file hello === Result on Windows: === hello .\file parser.d(47): Error: file ".\\file" cannot be found

Bug in import(...) on Windows?

2020-09-02 Thread Andrey Zherikov via Digitalmars-d-learn
My code (test.d): === void main() { import std.path: buildPath; pragma(msg, import("file")); pragma(msg, import(buildPath(".", "file"))); } === Content of file "file" is one line with "hello" text. Running command: dmd -J. -run test.d Result on Ubuntu: === hello hello

Re: How to create compile-time container?

2020-09-02 Thread Andrey Zherikov via Digitalmars-d-learn
On Tuesday, 1 September 2020 at 19:38:29 UTC, Steven Schveighoffer wrote: On 9/1/20 3:09 PM, Andrey Zherikov wrote: Unfortunately this won't work if there is a function 'bar' in different module that calls 'foo': You should post a full example you expect to work or not work, then we can

Re: How to create compile-time container?

2020-09-01 Thread Andrey Zherikov via Digitalmars-d-learn
On Tuesday, 1 September 2020 at 18:19:55 UTC, Andrey Zherikov wrote: The thing I'm trying to implement is: I have a function foo(string s)() and some "state"; this function should override this "state" (using "s" param) for all code within this function (note that code can execute other

Re: How to create compile-time container?

2020-09-01 Thread Andrey Zherikov via Digitalmars-d-learn
On Tuesday, 1 September 2020 at 18:57:30 UTC, Steven Schveighoffer wrote: string overrideState(string s) { // work your magic here, it's normal D code! } void foo(string s)() { mixin(overrideState(s)); } Unfortunately this won't work if there is a function 'bar' in different module

Re: How to create compile-time container?

2020-09-01 Thread Andrey Zherikov via Digitalmars-d-learn
On Monday, 31 August 2020 at 20:44:16 UTC, Adam D. Ruppe wrote: On Monday, 31 August 2020 at 20:39:10 UTC, Andrey Zherikov wrote: How can I do that? You can use a normal string[] BUT it is only allowed to be modified inside its own function. Then you assign that function to an enum or

How to create compile-time container?

2020-08-31 Thread Andrey Zherikov via Digitalmars-d-learn
On a low level, I want something like this code to work: string[] arr; // this can be any suitable type arr ~= "a";// data is compile-time constant enum f = arr[0]; // fails with "Error: variable arr cannot be read at compile time" enum b = arr[$-1]; // fails

Re: __FILE__ and __LINE__ in case of import expression

2020-08-23 Thread Andrey Zherikov via Digitalmars-d-learn
On Friday, 21 August 2020 at 22:34:49 UTC, Adam D. Ruppe wrote: On Friday, 21 August 2020 at 22:12:48 UTC, Steven Schveighoffer wrote: Who does that though? An incompetent coder: http://dpldocs.info/experimental-docs/source/arsd.cgi.d.html#L5713

Re: __FILE__ and __LINE__ in case of import expression

2020-08-23 Thread Andrey Zherikov via Digitalmars-d-learn
On Saturday, 22 August 2020 at 03:43:10 UTC, Steven Schveighoffer wrote: On 8/21/20 6:34 PM, Adam D. Ruppe wrote: On Friday, 21 August 2020 at 22:12:48 UTC, Steven Schveighoffer wrote: And honestly, if it says the source is "mixin-50, line 1", I think people will get it. I could probably

Re: __FILE__ and __LINE__ in case of import expression

2020-08-21 Thread Andrey Zherikov via Digitalmars-d-learn
On Friday, 21 August 2020 at 20:44:27 UTC, Andrey Zherikov wrote: Thanks for this link! I can use "#line" to fix line number but not file name: file: 'foo.d-mixin-1', line: '6', module: 'test', function: 'test.main', pretty function: 'int test.main(string[] args)', file full path:

Re: __FILE__ and __LINE__ in case of import expression

2020-08-21 Thread Andrey Zherikov via Digitalmars-d-learn
On Friday, 21 August 2020 at 15:34:49 UTC, Steven Schveighoffer wrote: On 8/21/20 10:01 AM, Andrey Zherikov wrote: How can I get __FILE__ and __LINE__ values correct in case of import expression? ... So the output from line #16 (1) is correct although from line #17 (2) is not: file name

Re: __FILE__ and __LINE__ in case of import expression

2020-08-21 Thread Andrey Zherikov via Digitalmars-d-learn
On Friday, 21 August 2020 at 15:27:14 UTC, Adam D. Ruppe wrote: On Friday, 21 August 2020 at 14:01:24 UTC, Andrey Zherikov wrote: mixin(import("foo.d")); // line #17(2) Why are you doing this? This kind of thing is almost never an ideal solution in D. See, the compiler just sees a

__FILE__ and __LINE__ in case of import expression

2020-08-21 Thread Andrey Zherikov via Digitalmars-d-learn
How can I get __FILE__ and __LINE__ values correct in case of import expression? Below is my test code. / test.d: module test; import std.stdio; void test(string file = __FILE__, size_t line = __LINE__, string mod = __MODULE__, string func =

How to write correct multi-threaded code?

2019-10-16 Thread Andrey Zherikov via Digitalmars-d-learn
I'm trying to write multi-threaded code that uses mutexes, condition variables and atomics but I've got confused how to do this correctly. Everything I found so far include a lot of casting to/from shared even for the objects that are supposed to be shared (like mutex and condition variable).

Is it a bug in std.getopt.config.stopOnFirstNonOption?

2019-08-16 Thread Andrey Zherikov via Digitalmars-d-learn
Here is the code I have which doesn't work: ``` string[] foo; string[] bar; auto args = ["app", "--bar", "bar", "--foo", "foo"]; // (1) import std.getopt; getopt(args, std.getopt.config.stopOnFirstNonOption, // (2) "foo", , "bar", ); ``` The error I see:

Re: Module static constructor doesn't work?

2019-08-08 Thread Andrey Zherikov via Digitalmars-d-learn
On Thursday, 8 August 2019 at 16:04:33 UTC, a11e99z wrote: On Thursday, 8 August 2019 at 14:55:37 UTC, Andrey Zherikov wrote: I have the following code: // main.d int main() { import std.stdio; writeln("hello"); return 0; } But if I create library from lib.d first and then link it

Module static constructor doesn't work?

2019-08-08 Thread Andrey Zherikov via Digitalmars-d-learn
I have the following code: // lib1/lib.d module lib; import std.stdio; static this() { writeln("+" ~ __FILE__); } static ~this() { writeln("-" ~ __FILE__); } // main.d int main() { import std.stdio; writeln("hello"); return 0; } So if I compile lib.d and main.d together

Re: Is there a way to adjust lookup paths for modules during compilation?

2019-07-31 Thread Andrey Zherikov via Digitalmars-d-learn
On Wednesday, 31 July 2019 at 20:16:01 UTC, H. S. Teoh wrote: On Wed, Jul 31, 2019 at 08:09:29PM +, Andrey Zherikov via Digitalmars-d-learn wrote: I found a function that seems could be used for adding import paths: dmd.frontend.addImport (https://dlang.org/phobos/dmd_frontend.html

Re: Is there a way to adjust lookup paths for modules during compilation?

2019-07-31 Thread Andrey Zherikov via Digitalmars-d-learn
I found a function that seems could be used for adding import paths: dmd.frontend.addImport (https://dlang.org/phobos/dmd_frontend.html#.addImport). But it's not clear how can I use it: dmd directory is not added to lookup by default and trying adding it gives errors: dmd.exe -i

Re: Is there a way to adjust lookup paths for modules during compilation?

2019-07-31 Thread Andrey Zherikov via Digitalmars-d-learn
On Wednesday, 31 July 2019 at 17:59:00 UTC, bauss wrote: Your best bet is actually not relying on CTFE since IO is very restricted at CTFE in D. Ex. you can only import files that are specified by you. The best thing you can do is have a file that lists every file you need to be able to read

Is there a way to adjust lookup paths for modules during compilation?

2019-07-31 Thread Andrey Zherikov via Digitalmars-d-learn
I want my program to add some directories into module lookup process (like adding -I dmd options). List of directories is known at compile time but the choice of what exact directories to add depends on `-version` parameter. Is there a way to achieve this in code? I can actually do this by

Re: How to check that import module will succeed?

2019-07-26 Thread Andrey Zherikov via Digitalmars-d-learn
On Friday, 26 July 2019 at 14:19:05 UTC, Paul Backus wrote: version(HasStdio) import std.stdio; else pragma(msg, "std.stdio is not available"); Then configure your build system to pass `-version=HasStdio` to dmd (or the equivalent flag to ldc or gdc) when std.stdio is available. I

Re: How to check that import module will succeed?

2019-07-26 Thread Andrey Zherikov via Digitalmars-d-learn
On Friday, 26 July 2019 at 14:14:11 UTC, Anonymouse wrote: I use __traits(compiles, __traits(identifier, moduleName)) now instead and it seems to work. I couldn't find "identifier" docs on https://dlang.org/phobos/std_traits.html. But I tried and it doesn't work - I think because is unknown

Re: How to check that import module will succeed?

2019-07-26 Thread Andrey Zherikov via Digitalmars-d-learn
On Friday, 26 July 2019 at 06:24:18 UTC, evilrat wrote: On Friday, 26 July 2019 at 03:42:58 UTC, Andrey Zherikov wrote: Is there a way to check whether some module, say "foo", is available for import before doing "import foo"? I did some really retarded utility like this in the past, worked

How to check that import module will succeed?

2019-07-25 Thread Andrey Zherikov via Digitalmars-d-learn
Is there a way to check whether some module, say "foo", is available for import before doing "import foo"? I want to create a function that imports module if it's available or does something else otherwise. So I think the code should look something like this: mixin template my_import(alias