Skip to content

Latest commit

 

History

History
135 lines (103 loc) · 4.25 KB

DIP1032.md

File metadata and controls

135 lines (103 loc) · 4.25 KB

Function pointers and Delegate Parameters Inherit Attributes from Function

Field Value
DIP: 1032
Review Count: 0
Author: Walter Bright walter@digitalmars.com
Implementation:
Status: Community Review Round 1

Abstract

When function parameters are declared as delegates or function pointers, any @safe, pure, nothrow, and @nogc attributes that are attached to the function type are ignored. This DIP proposes that any of these attributes attached to a function become the defaults for delegate and function pointer types declared in the function's parameter list.

Contents

Rationale

Consider the code:

@safe pure nothrow @nogc
int foo(int delegate() dg)
{
    return dg(); // Error
}

This fails because the delegate dg has none of foo's attributes. The user will need to write:

@safe pure nothrow @nogc
int foo(int delegate() @safe pure nothrow @nogc dg)
{
    return dg(); // No error
}

The following also compiles without error:

@safe pure nothrow @nogc
{
    alias dg_t = int delegate();

    int foo(dg_t dg)
    {
        return dg(); // No error
    }
}

demonstrating that the dg_t declaration is picking up attributes that are in scope.

Having the user repeat these attributes in the parameter declaration is both surprising and burdensome. It is even more inconvenient because when a delegate is passed to a function, most of the time it is so the function can call it, but the function cannot call it if it does not have at least the set of @safe, pure, nothrow, and @nogc attributes that the function has.

Passing delegates to functions is a key feature of D and underpins a number of coding patterns. Making them easier to use by eliminating the need for a clutter of attribute annotations will be welcome and will lower the barrier to adding annotations to functions in the first place.

A further reason is that the lazy function parameter attribute is underspecified and is a constant complication in the language specification. With this DIP, lazy can move towards being defined in terms of an equivalent delegate parameter, thereby simplifying the language by improving consistency.

This DIP proposes that delegate and function pointer parameters default to having the same combination of the four aforementioned attributes as the function which defines the parameter list.

Prior Work

None known.

Description

Function pointer and delegate types defined in the function parameter list default to the attributes of the function type that defines that parameter list. With this change, the following will compile without error:

@safe pure nothrow @nogc
int foo(int delegate() dg, int function() fp)
{
    return dg() + fp();
}

Breaking Changes and Deprecations

It is possible that a parameter declaration may require that a delegate or function pointer parameter have fewer attributes than the function itself. This would only be possible if the delegate or function pointer was never called, but was ignored or simply stored elsewhere.

Such can be accomplished by declaring the parameter type elsewhere:

alias dg_t = void delegate() dg_t;
alias fp_t = void function() fp_t;

dg_t global_dg;
fp_t global_fp;

@safe pure nothrow @nogc
void foo(dg_t dg, fp_t fp)
{
    global_dg = dg;
    global_fp = fp;
}

Because of possible code breakage, this feature will initially be available only via the -preview=attrdelegate compiler switch.

Copyright & License

Copyright (c) 2020 by the D Language Foundation

Licensed under Creative Commons Zero 1.0

Reviews