Bug 10875 – Introduce functionLinkageType to mirror functionLinkage with an enum

Status
RESOLVED
Resolution
WONTFIX
Severity
enhancement
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-08-23T04:51:00Z
Last change time
2013-11-22T01:07:18Z
Keywords
pull
Assigned to
andrej.mitrovich
Creator
andrej.mitrovich

Comments

Comment #0 by andrej.mitrovich — 2013-08-23T04:51:27Z
Currently std.traits.functionLinkage returns the linkage type, but it returns it as a string. So if you have generic code, you might end up writing code like so: ----- import std.traits; extern(C) void func() { } void main() { enum linkage = functionLinkage!func; static if (linkage == "c") { } else static if (linkage == "D") { } } ----- Unfortunately there's a bug here, there is no lowercase "c" linkage type, only "C". It would be safer if functionLinkage returned an enum. But since it's too late to change the return type, I propose we introduce an enum version: ----- import std.traits; extern(C) void func() { } void main() { // new trait which returns a LinkageType enum instance enum linkage = functionLinkageType!func; static if (linkage == LinkageType.c) { } else static if (linkage == LinkageType.d) { } } ----- This will also allow a user to generate code by using EnumMembers on the LinkageType enum.
Comment #1 by andrej.mitrovich — 2013-08-23T04:54:39Z
(In reply to comment #0) > But since it's too late to change the return type, I propose we introduce an > enum version: Actually a reasonable alternative is to simply introduce the LinkageType enum which will have a string as its base type, so it can be used with the functionLinkage function: enum LinkageType : string { D = "D", C = "C", Windows = "Windows", Pascal = "Pascal", Cpp = "C++" } ----- import std.traits; extern(C) void func() { } void main() { enum linkage = functionLinkage!func; static if (linkage == LinkageType.C) { } else static if (linkage == LinkageType.D) { } } -----
Comment #2 by bearophile_hugs — 2013-08-23T06:25:57Z
(In reply to comment #1) > Actually a reasonable alternative is to simply introduce the LinkageType enum > which will have a string as its base type, This is a nice idea to fix the original design mistake of using strings. Do you know of other functions/templates in Phobos that could enjoy this the same improvement?
Comment #3 by andrej.mitrovich — 2013-08-23T09:22:04Z
(In reply to comment #2) > (In reply to comment #1) > > > Actually a reasonable alternative is to simply introduce the LinkageType enum > > which will have a string as its base type, > > This is a nice idea to fix the original design mistake of using strings. Do you > know of other functions/templates in Phobos that could enjoy this the same > improvement? Nothing of the top of my head, but I think there are a few more.
Comment #4 by andrej.mitrovich — 2013-09-18T18:27:04Z
Comment #5 by andrej.mitrovich — 2013-11-22T01:07:18Z
Unfortunately implementing this might very likely break existing code due to how enums are formatted. See this comment: https://github.com/D-Programming-Language/phobos/pull/1587#discussion_r7834400