On Friday, 1 April 2016 at 22:54:53 UTC, Simen Kjaeraas wrote:
On Friday, 1 April 2016 at 21:46:35 UTC, ag0aep6g wrote:
On 01.04.2016 22:59, Simen Kjaeraas wrote:
The usual way to fix it would be to include __FILE__ and __LINE__ in the
template arguments:

Right, no mixin this way. I wouldn't call this "truly nice", though.

It depends on code formatting to work. Put everything on one line and it breaks. Significant whitespace is a pain when generating code. Though this is not nearly as bad as significant indentation, of course.

__FILE__ also kind of breaks separate compilation. All object files have to be compiled from the same directory. Otherwise __FILE__ will be different.

__LINE__ has a similar (maybe even more obscure) issue. Add or remove a newline before compiling dependent modules and things break. Usually, one recompiles all dependents when a dependency changes, but a significant newline, really?

I kinda agree. And looking at https://dlang.org/spec/traits.html, I see there's __MODULE__, which would probably be a better choice than __FILE__.

As for __LINE__, what we'd want is basically something like __CONTEXT__, which doesn't exist, but might be the .mangleof of the surrounding scope:

struct S(string ctx = __CONTEXT__) {
    pragma(msg, ctx);
}

S!() a; // "3foo"

void bar() {
    S!() b; // "_D3foo3barFZv"
}

struct S2 {
    S!() c; // "S3foo2S2"
    void baz() {
        S!() d; // "_D3foo2S23bazMFZv"
    }
}

That'd remove the problem of significant whitespace. In fact, it'd also eliminate the need for __MODULE__ in this case.

Still though, that's not enough if we want this to work:

void foo() {
    alias a = Foo!(); alias b = Foo!();
    assert(!isSame!(a, b));
}

We could also add __COLUMN__, which would be the horizontal index of the instantiation's beginning:

    foo(3, Bar!3.baz);
//         ^Here. Position 11.

Next problem:

void main() {
    pragma(msg, __LINE__);
    mixin("pragma(msg, __LINE__);\npragma(msg, __LINE__);");
    pragma(msg, __LINE__);
}

That prints '4' twice - once for the actual line 4, the other for the second line of the mixin. However, __FILE__ is different, so I guess __CONTEXT__ could also be.

--
  Simen

What is needed is Lisp's gensym construct.

Reply via email to