D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 6394 - template type constraints embedded in the function parameters.
Summary: template type constraints embedded in the function parameters.
Status: RESOLVED WORKSFORME
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: Other Windows
: P2 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-07-28 08:43 UTC by Gor Gyolchanyan
Modified: 2014-03-19 19:13 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Gor Gyolchanyan 2011-07-28 08:43:43 UTC

    
Comment 1 Gor Gyolchanyan 2011-07-28 08:49:18 UTC
Given the code:

---------------

template SomeStruct(Type)
{
    static if(is(Type == struct) || is(Type == class) || is(Type == interface))
        enum StaticStruct = true;
    else
        enum StaticStruct = false;
}

---------------

I suggest a syntax sugar for template functions:

---------------

void f(SomeStruct s)
{
}

---------------

be transformed into:

---------------

void f(Type_0_)(Type_0_ s)
    if(SomeStruct!Type_0_)
{
}

---------------

IF the given type isn't a valid type, it'll search for a template, that takes a single type and evaluates to a bool and transform the function if the type name is such a template.

this would greatly increase the readability of generic functions and allow to avoid creating enormous template constraints, as well as provide a better diagnostics, like "the type T is not a SomeStruct" or something like that.
Comment 2 Infiltrator 2014-03-19 19:13:36 UTC
The proposed code compiles as of v2.065.