Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for FormattableString #24

Closed
damageboy opened this issue Jun 18, 2018 · 5 comments
Closed

Add support for FormattableString #24

damageboy opened this issue Jun 18, 2018 · 5 comments

Comments

@damageboy
Copy link

I think it would be a nice addition to the arsenal of ZeroLog to support the somewhat unknown c# interpolated string feature called ForamttableString.

This would basically allow users to call ZeroLog with interpolated strings:

log.InfoInterpolated($"Tomorrow ({tomorrow}) will occur in {numberOfSecondsUntilTomorrow} seconds");

The theory behind this is that ZeroLog would get a FormattableString instance fed into the *Interpolated variants of the logging functions that would have to be added to ZeroLog and to the StringFormatter project.

The reason behind adding yet another overload for this purpose is that apprently the c# compiler has a feature/issue where having two functions that only differ by having a string/FormattableString parameter leads to the string variant being always selected by the compiler:

dotnet/roslyn#46

http://blog.engdahls.dk/2016/08/how-to-overload-string-and.html

Any thoughts about this?

@ltrzesniewski
Copy link
Member

ltrzesniewski commented Jun 18, 2018

The problem about FormattableString is that using it would necessarily allocate, while the ZeroLog API is built to avoid allocations at all costs.

We could perhaps support it without allocating by rewriting the code with a Fody addin (this would rewrite the FormattableString to regular Append calls at compile time).

@damageboy
Copy link
Author

@ltrzesniewski ah, thanks for the comment.
I didn't think about what the compiler actually does for preparing the FormattalbeString object before passing it to the function.

So in essence, the compiler is re-writing the code to allocate the FormattableString object at the call site?

@ltrzesniewski
Copy link
Member

Yes, it allocates the FormattableString, plus an object[] array to hold the values, and it needs to box the values when they're value types.

See here:

    public void M()
    {
        Log($"Hello {42}, world! {true}");
    }
    
    public static void Log(FormattableString value)
    {
    }

Gets compiled to:

    public void M()
    {
        object[] obj = new object[2];
        obj[0] = 42;
        obj[1] = true;
        Log(FormattableStringFactory.Create("Hello {0}, world! {1}", obj));
    }

    public static void Log(FormattableString value)
    {
    }

@damageboy
Copy link
Author

OK, I should have realized it would probably be awful...

@ltrzesniewski
Copy link
Member

Ok, it's maybe a tiny bit late, but interpolated string support is now available in v2, and does not allocate thanks to interpolated string handlers in C# 10. 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants