Printing shortest decimal form of floating point number with Mir
ag0aep6g
anonymous at example.com
Mon Jan 4 17:48:50 UTC 2021
On Monday, 4 January 2021 at 17:24:53 UTC, jmh530 wrote:
> Your point is basically that a template alias only becomes an
> actual alias when it has been instantiated. You then note that
> the deduction process operates in terms of Bar (in that you
> have to find a T that fits Bar!T to get to an alias of Foo!T).
Right.
> I think part of what is confusing is that the temporary x in my
> example is a Foo!int and not a Bar!int, which is why the
> Foo!int can't be passed into f(T)(Bar!T).
Well, `x` is both a `Foo!int` and a `Bar!int`. They're the same
type. `f(Bar!int())` fails in the same way as `f(Foo!int())` does.
> I think part of the issue is that many people's mental model
> would be for f(T)(Bar!T) to get re-writtn as f(T)(Foo!T), which
> is related to Ola's point with respect to type unification.
I think you're hitting the nail on the head here regarding the
confusion. Such a rewrite makes intuitive sense, and it would be
nice, but it doesn't happen.
> But the compiler isn't really doing any re-writing, so much as
> it sees the Foo!int and does not have the information necessary
> to determine that a Foo!int should satisfy Bar!T (as you point
> out). It would need to extract int from Foo!int, then
> instantiate Bar!T to get Foo!int (which I believe is what the
> implementation was doing, or something similar).
The compiler can and does extract `Foo` and `int` from `Foo!int`.
Then it compares `Foo` to `Bar` and sees that they're not the
same template. Then it gives up. The compiler would need to
inspect the template `Bar`, see that it's an alias template that
expands to `Foo!T` and then continue with that. I.e., it would
need to do the rewrite as you say.
More information about the Digitalmars-d-announce
mailing list