[Issue 9949] template initialization when alias cannot be read at compile time

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Apr 18 05:23:39 PDT 2013


http://d.puremagic.com/issues/show_bug.cgi?id=9949


Andrej Mitrovic <andrej.mitrovich at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrej.mitrovich at gmail.com


--- Comment #1 from Andrej Mitrovic <andrej.mitrovich at gmail.com> 2013-04-18 05:23:37 PDT ---
(In reply to comment #0)
> The following code compiles aldo s is not readable at compile time:
> 
> struct S (alias T) {
>   typeof(T) value;
> }
> 
> void main () {
>   auto s = "some";
>   s ~= "string";
>   S!s value;
> }

That code isn't reading 's', it is only using it to get its type. And all types
are known at compile-time. The code is ok.

> 
> 
> The side effect of this is the following error:
> 
> // Error: function literal __lambda3 (S!(s) a) is not
> // callable using argument types (S!(s))
> 
> module program;
> 
> import std.stdio;
> 
> struct S (alias T) {
>   typeof(T) value;
> }
> 
> void f (alias l = x => 1) (string s) {
>   l(S!(s).init);
> }
> 
> void main () {
>   auto s = "some";
>   s ~= "string";
>   f!((S!s a) { return 1; })(s);
> }

It doesn't compile because 's' within 'main' and 's' within 'f' are two
different variables. You'd have to pass 's' from within main as an alias
parameter to 'f'. E.g.:

----
import std.stdio;

struct S(alias T)
{
    typeof(T) value;
}

void f(alias lambda = x => 1, alias str)()
{
    lambda(S!str.init);
}

void main()
{
    string str = "some";
    f!((S!str a) { return 1; }, str)();
}
----

But I'd recommend changing the struct S definition to take a type and use
typeof() at the call site to avoid having to use aliases everywhere.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list