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

std.numeric: clean import & CustomFloat printing fix #2729

Merged
merged 1 commit into from Nov 15, 2014
Merged

std.numeric: clean import & CustomFloat printing fix #2729

merged 1 commit into from Nov 15, 2014

Conversation

9il
Copy link
Member

@9il 9il commented Nov 15, 2014

Blocker for #2728

static assert(__traits(compiles, findRoot!real((x)=>cast(double)x, real.init, real.init)));
static assert(__traits(compiles, findRoot((real x)=>cast(double)x, real.init, real.init)));
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this removed?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh nevermind, I see that you just moved it.

@quickfur
Copy link
Member

LGTM

@quickfur
Copy link
Member

Auto-merge toggled on

@@ -619,7 +617,16 @@ public:
}

/// ditto
string toString() { return to!string(get!real); }
template toString()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is toString a template nested inside a template? It almost certainly isn't worth complicating the code like this just to maybe avoid one single import from happening that is already inside a template instance. Moreover, both of the imports are for std.format. Thus, you only gain something once the compiler implements lazy semantic analysis. But then, the whole thing will probably superfluous superfluous anyway, with CustomFloat being a template. Also note that template instances aren't free either.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. CustomFloat can be used without printing it. So template toString() is useful to do not import std.format.
  2. void toString()(scop ... compiles and void toString(scop ... not compiles with unittests! I don't know why.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The correct thing to do for 2. is not to randomly add parentheses and then hope that the problem goes away. If this turns out to be a genuine compiler bug, there should at the very least be a comment explaining why the code looks so messy.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. Do we need reduced example?

@quickfur
Copy link
Member

Auto-merge toggled off

1 similar comment
@dnadlinger
Copy link
Member

Auto-merge toggled off

template toString()
{
import std.format : FormatSpec;
void toString()(scope void delegate(const(char)[]) sink, FormatSpec!char fmt)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, just noticed the extraneous () here. If this is already inside a template block, toString shouldn't take any compile-time parameters, since toString()(...) is the same as template toString() { toString(...) ...}.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without extra () code doesn't compile!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the error message?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unittes fails. std.format can not understand that this struct has toString.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

core.exception.AssertError@/Users/ilya/GitHub/phobos/std/numeric.d(671): unittest failure

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To do not compile it and import std.format when toString is unused.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. In that case, we need to trace down the problem of why std.format doesn't recognize toString when it's inside an explicit eponymous template (rather than an implicit one that we usually use).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ping @klickverbot @quickfur
Reduced example:

module test2155a;
struct B {}
template A()
{
    import test2155a : B;
    void A(B b)
    {
        import std.format;
    }
}   

void foo() {
    import test2155a : B;
    B b;
    A(b);
}
test2155.d(4): Error: undefined identifier B

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds like a compiler bug / limitation.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@9il
Copy link
Member Author

9il commented Nov 15, 2014

@quickfur can we merge with comment?:

    template toString()
    {
        import std.format : FormatSpec, formatValue;
        // FIXME "()", Issue 13737
        void toString()(scope void delegate(const(char)[]) sink, FormatSpec!char fmt)
        {
            sink.formatValue(get!real, fmt);
        }
    }

@dnadlinger
Copy link
Member

Thanks! This makes me much more confident that there isn't something else we might have missed that is fundamentally broken.

In Phobos, @@BUG@@ is generally used to tag references to compiler bugs. Something like // Needs to be a template because of DMD @@BUG@@ 13737. would be more usual.

update toString  template

Issue 13737

new comment

add brace
@dnadlinger
Copy link
Member

Auto-merge toggled on

@9il
Copy link
Member Author

9il commented Nov 15, 2014

Thanks!

dnadlinger added a commit that referenced this pull request Nov 15, 2014
std.numeric: clean import & CustomFloat printing fix
@dnadlinger dnadlinger merged commit da397d7 into dlang:master Nov 15, 2014
@9il 9il deleted the numeric branch November 15, 2014 21:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants