D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 12074 - Add BlockStatement to TemplateSingleArgument
Summary: Add BlockStatement to TemplateSingleArgument
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P4 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-02-04 03:03 UTC by Vladimir Panteleev
Modified: 2024-12-13 18:16 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Vladimir Panteleev 2014-02-04 03:03:40 UTC
Currently, this is forbidden, both by the specification and implementation:

int call(alias fun)()
{
    return fun();
}

int main()
{
    return call!{ return 5; }();
}

It seems unambiguous, so I see no reason why it should not be allowed.

Perhaps the same idea applies to array/AA literals.
Comment 1 Vladimir Panteleev 2014-02-04 03:09:00 UTC
I see Kenji posted a patch in 2010 here:

http://d.hatena.ne.jp/khara_9rnsr/20100927/1285572362
Comment 2 Vladimir Panteleev 2014-02-04 03:40:23 UTC
Kenji's patch is now outdated (not surprising). However, this very simple patch seems to work just fine:

diff --git a/src/parse.c b/src/parse.c
index 78a2356..088326d 100644
--- a/src/parse.c
+++ b/src/parse.c
@@ -2360,6 +2360,8 @@ Objects *Parser::parseTemplateArgument()
         case TOKfuncstring:
         case TOKprettyfunc:
         case TOKthis:
+        case TOKlcurly:
+        case TOKlbracket:
         {   // Template argument is an expression
             Expression *ea = parsePrimaryExp();
             tiargs->push(ea);

The original patch explicitly set the expression type to that of a function. This doesn't seem to be necessary, as the compiler now infers whether it is a function or delegate literal depending on whether statements refer to the literal's context.
Comment 3 basile-z 2019-02-13 02:46:20 UTC
I like the idea but i see an ambiguity:

---
int call(alias fun)()
{
    return fun();
}

int call(int value)()
{
    return value+1;
}

void main()
{
    call!{ return 0; }();
}
---

which overload is called ? The one that will doe CTFE and then call!0 or the other ?
Comment 4 Vladimir Panteleev 2019-02-13 06:21:17 UTC
That is a semantic issue and completely orthogonal to this syntactic one. `call!({ return 0; })();` has the same issue as your example.
Comment 5 dlangBugzillaToGithub 2024-12-13 18:16:35 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/dmd/issues/18769

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB