std.experimental.logger.Logger writeLogMsg is @safe?

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Feb 22 15:03:38 PST 2016


On Monday, February 22, 2016 22:22:01 Minas Mina via Digitalmars-d-learn wrote:
> I'm trying to inherit from Logger, and I my custom logger to
> print to stdout using writeln(). But I can't because writeLogMsg
> is @safe, whereas writeln() is @system.
>
> Why is writeLogMsg @safe? This is too restrictive.

Short answer:

Use @trusted on the function overrides.

Long answer:

Well, if the logger isn't @safe, then it can't be easily used in @safe code,
which would be a bit of a disaster, and from what I know of the logger API
(though I'm not super familiar with it), there's really no reason why it
shouldn't be @safe given that it's passing strings along, and derived
classes just need to print those strings. So, derived classes that want to
be @safe just need to be able to operate on strings safely. And if you can't
guarantee that that's @safe, then you have a problem.

Now, obviously writeln isn't @safe (and maybe it should be inferred as such
most of the time, but that's a separate issue), but what you can do so long
as you can guarantee that using writeln is actually @safe (which it should
be if all you're feeding it is strings), then you can mark your overrides
as @trusted, and that should work with inheritance.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list