[Issue 13196] Infer struct/class template args from constructor args

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Thu Jul 24 22:44:04 PDT 2014


https://issues.dlang.org/show_bug.cgi?id=13196

--- Comment #6 from Jonathan M Davis <jmdavisProg at gmx.com> ---
(In reply to Manu from comment #5)
> (In reply to Jonathan M Davis from comment #4)
> > Regardless, we can potentially make it so that the compiler is able to infer
> > the constructor's template arguments for templated types in some basic
> > cases, but I think that it's clear that we can only do it for a certain
> > subset.
> 
> It's not clear at all. Present a case where it can't work? I haven't seen
> any yet.

An obvious case is something like

struct S(T)
{
    this(U)(U u)
        if(is(U : T))
    {...}

    T _t;
}

Also, the previous example of

struct S(T)
{
    static if(is(T == bool))
    {
        this(T[] t) {}
    }
    else
    {
        this(int i) {}
    }
}

wouldn't work as was previously discussed. The basic case of

struct S(T)
{
    this(T t) {}
}

should be possible, but I'd expect that much of anything fancier wouldn't.
Regardless, the fact that

struct S(T)
{
    this(T t) {}
}

is equivalent to

template S(T)
{
    struct S
    {
        this(T t) {}
    }
}

is a major hurdle from an implementation point of view, because it would mean
having to know what was going to be in the template before it's even
instantiated. Also, you could have something like

template S(T)
{
    struct S
    {
        this(int i) {}
    }
}

so the compiler can't assume that the type that's being passed to the
constructor has anything to do with the template argument.

When you do S!int(5), the compiler knows the template argument, can instantiate
the template, and _then_ look at whether trying to call the result of the
template instantiation with an argument of 5 makes any sense. What you're
asking for basically requires that the compiler figure out the guts of the
template before it's even instantiated. I expect that it's possible to do that,
but it would be a major shift in how templates are handled in the compiler, so
it wouldn't surprise me at all if it never happened, as nice as it would be.

--


More information about the Digitalmars-d-bugs mailing list