Bug 23264 – Allow __traits(parameters) in the return type or contract of a function

Status
NEW
Severity
enhancement
Priority
P4
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2022-07-21T13:36:04Z
Last change time
2024-12-13T19:23:55Z
Assigned to
No Owner
Creator
Bolpat
Moved to GitHub: dmd#20132 →

Comments

Comment #0 by qs.il.paperinik — 2022-07-21T13:36:04Z
Allow __traits(parameters) to occur in any part of the function, not only its body and `in` and `out` contract. The following should be legal: typeof(__traits(parameters))[0] f(Ts...)(Ts args) { … } void f(Ts...)(Ts args) if (__traits(parameters).length > 1) { … } But they are rejected with Error: `__traits(parameters)` may only be used inside a function It is unmistakably clear what the intention is.
Comment #1 by wolframw — 2022-08-26T11:22:01Z
There would be an ambiguity for nested functions: void foo(T1...)(T1 args1) { typeof(__traits(parameters))[0] bar(T2...)(T2 args2) { // ... } } Would the return value of bar depend on foo's or bar's parameters? Currently, it would refer to foo's parameters.
Comment #2 by qs.il.paperinik — 2022-08-29T12:11:42Z
(In reply to wolframw from comment #1) > There would be an ambiguity for nested functions: > > void foo(T1...)(T1 args1) > { > typeof(__traits(parameters))[0] bar(T2...)(T2 args2) > { > // ... > } > } > > Would the return value of bar depend on foo's or bar's parameters? > Currently, it would refer to foo's parameters. Good catch! I don’t think it’s a big problem. Return types are ambiguous in the same fashion: void f(T)() { T g(T)() { return T.init; } } The `T` value that function `g` returns could – for the uninformed reader – mean f’s type parameter or g’s type parameter (of course, it’s g’s). I see no reason why __traits(parameters) would work any differently. Also, if you wanted the outer function’s parameters, one can alias them. void foo(T1...)(T1 args1) { alias fooParams = typeof(__traits(parameters)); fooParams[0] bar(T2...)(T2 args2) { // ... } }
Comment #3 by robert.schadek — 2024-12-13T19:23:55Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/20132 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB