[Issue 20847] confusing compiler error message when compiling PosixTimeZone.getTimeZone

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu May 21 03:55:41 UTC 2020


https://issues.dlang.org/show_bug.cgi?id=20847

RazvanN <razvan.nitu1305 at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |razvan.nitu1305 at gmail.com
         Resolution|---                         |INVALID

--- Comment #2 from RazvanN <razvan.nitu1305 at gmail.com> ---
This is not a bug. Initializers of class members need to be evaluable at
compile time so the compiler tries to interpret the body of function
getTimeZone at compile time. Since lstat/GetFileAttributesW are not D functions
that have available code, you end up with the (correct) error that is currently
outputted. You can call those functions at runtime because you the object code
is linked and you have no problems, but interpretation needs the source code.

Fortunately, there is a standard way of doing this, using a class static
constructor [1] that were created for exactly this purpose:

import std.datetime.timezone;

class T {
    enum NEW_YORK_TIMEZONE = "America/New_York";
    public static const PosixTimeZone _timeZoneInfo;

    static this()
    {
        _timeZoneInfo = PosixTimeZone.getTimeZone(NEW_YORK_TIMEZONE);
    }
}

Closing this as invalid.

[1] https://dlang.org/spec/class.html#static-constructor

--


More information about the Digitalmars-d-bugs mailing list