D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 6754 - extern() in a function signature
Summary: extern() in a function signature
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:
: 10837 (view as issue list)
Depends on:
Blocks:
 
Reported: 2011-10-01 17:22 UTC by bearophile_hugs
Modified: 2024-12-13 17:56 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2011-10-01 17:22:06 UTC
This comes from code shown in D.learn:
http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.learn&article_id=29886

You can't use extern(C) in a function signature:


void foo(extern(C) void function() f) {}
void main() {}


DMD 2.056head gives:

test.d(1): basic type expected, not extern
test.d(1): found 'extern' when expecting ')'
test.d(1): semicolon expected following function declaration
test.d(1): Declaration expected, not '('


Workaround: before the function definition you have to define the argument type with an alias:


alias extern(C) void function() CF;
void foo(CF f) {}
void main() {}
Comment 1 Andrej Mitrovic 2012-10-16 11:20:40 UTC
Since @safe is already allowed I don't see why extern() shouldn't be allowed:

void test(void function() @safe) { }  // compiles

There are workarounds e.g. using uniquely-named aliases or module-scope extern(C): declaration, the latter is a bit odd since the attribute ends up affecting parameters and not just declarations in module scope.

Walter can we get an OK to implement this enhancement? Allowing the enhancement would be beneficial for binding with C:
extern(C) void test(extern(C) void function() callback);
Comment 2 Andrej Mitrovic 2012-12-27 16:15:04 UTC
Someone mentioned in IRC they've already implemented this. To the person that did: please provide a patch (or a pull) so we don't duplicate our efforts, thanks.
Comment 3 Andrej Mitrovic 2013-08-17 07:54:52 UTC
*** Issue 10837 has been marked as a duplicate of this issue. ***
Comment 4 bearophile_hugs 2013-10-10 13:26:36 UTC
Another example:


alias extern(C) void function() TF1; // OK
alias TF2 = extern(C) void function(); // Error
void main() {}



test.d(2): Error: basic type expected, not extern
test.d(2): Error: semicolon expected to close alias declaration
test.d(2): Error: no identifier for declarator extern (C) void function()
Comment 5 dlangBugzillaToGithub 2024-12-13 17:56:36 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

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

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