digitalmars.D.learn - __FILE__
- workman (24/24) Jul 26 2021 file test.d:
- Adam D Ruppe (3/4) Jul 26 2021 Why do you have that [0..$] there? It is probably breaking the
- Stefan Koch (10/14) Jul 26 2021 Correct.
- Steven Schveighoffer (7/20) Aug 04 2021 This might be how it is implemented, but it shouldn't be. `__FILE__`
- =?UTF-8?Q?Ali_=c3=87ehreli?= (19/22) Aug 04 2021 Whaaaaat??? I just checked and it works! :)
- Steven Schveighoffer (13/15) Aug 04 2021 No, the default parameters are used directly as if they were typed in at...
- Mathias LANG (3/27) Aug 04 2021 It's a known bug: https://issues.dlang.org/show_bug.cgi?id=18919
file test.d: ------------- module test; import abc; void doTest(){ log!"test"(); } ----------- file abc.d: ------------- module abc; import test; void log(string fmt, int line = __LINE__, string path = __FILE__[0..$], A...)(A a) { import core.stdc.stdio; printf("[%s:%d] \n", path.ptr, line); } extern(C) int main() { doTest(); return 0; } ------------- retult: [abc.d:4] expect: [test.d:4]
Jul 26 2021
On Monday, 26 July 2021 at 11:43:56 UTC, workman wrote:__FILE__[0..$]Why do you have that [0..$] there? It is probably breaking the __FILE__ magic.
Jul 26 2021
On Monday, 26 July 2021 at 12:01:23 UTC, Adam D Ruppe wrote:On Monday, 26 July 2021 at 11:43:56 UTC, workman wrote:Correct. The compiler has to evaluate the default argument as constant expression in order to use it as default value.. Therefore it evaluates (__FILE__)[0 ..$]you first eval __FILE__ at CTFE within the module you are defining the function in. And then you slice it from zero to length. On the other hand if you use x = __FILE__ it recognizes that as a special constant expression which can be put into the callsite directly.__FILE__[0..$]Why do you have that [0..$] there? It is probably breaking the __FILE__ magic.
Jul 26 2021
On 7/26/21 1:05 PM, Stefan Koch wrote:On Monday, 26 July 2021 at 12:01:23 UTC, Adam D Ruppe wrote:This is not true, you can use runtime calls as default values.On Monday, 26 July 2021 at 11:43:56 UTC, workman wrote:Correct. The compiler has to evaluate the default argument as constant expression in order to use it as default value..__FILE__[0..$]Why do you have that [0..$] there? It is probably breaking the __FILE__ magic.Therefore it evaluates (__FILE__)[0 ..$]you first eval __FILE__ at CTFE within the module you are defining the function in. And then you slice it from zero to length.This might be how it is implemented, but it shouldn't be. `__FILE__` should be usable inside any expression as a default value, which should expand to the call-site for the function (as usual). But this has nothing to do with CTFE as far as I know. -Steve
Aug 04 2021
On 8/4/21 7:17 PM, Steven Schveighoffer wrote:Whaaaaat??? I just checked and it works! :) string bar() { import std.process; return ("BAR" in environment) ? environment["BAR"] : null; } string foo(string s = bar()) { return s; } void main() { import std.stdio; writeln(foo("hello world")); writeln(foo()); } Run e.g. with $ BAR=whaaat ./deneme I wonder whether this feature is thanks to 'lazy' parameters, which are actually delegates. AliThe compiler has to evaluate the default argument as constant expression in order to use it as default value..This is not true, you can use runtime calls as default values.
Aug 04 2021
On 8/4/21 10:27 PM, Ali Çehreli wrote:I wonder whether this feature is thanks to 'lazy' parameters, which are actually delegates.No, the default parameters are used directly as if they were typed in at the call site (more or less, obviously the `__FILE__` example is weird). So: ```d writeln(foo()); ``` is just like you did: ```d writeln(foo(bar())); ``` There are no lazy parameters involved. -Steve
Aug 04 2021
On Monday, 26 July 2021 at 11:43:56 UTC, workman wrote:file test.d: ------------- module test; import abc; void doTest(){ log!"test"(); } ----------- file abc.d: ------------- module abc; import test; void log(string fmt, int line = __LINE__, string path = __FILE__[0..$], A...)(A a) { import core.stdc.stdio; printf("[%s:%d] \n", path.ptr, line); } extern(C) int main() { doTest(); return 0; } ------------- retult: [abc.d:4] expect: [test.d:4]It's a known bug: https://issues.dlang.org/show_bug.cgi?id=18919 If you remove the slicing from `__FILE__`, it'll work as expected.
Aug 04 2021