Skip to content

Commit 7e44db1

Browse files
committed
Fix issue #4191: The spec should warn against using mixin(__FUNCTION__)
The text of the warning pretty much says it all. mixin(__FUNCTION__) is an unreliable way to get a function's symbol and should not be used - but it seems to be a common solution to the problem given that we don't currently have a way to get at the symbol directly. So, this adds a warning against using mixin(__FUNCTION__) and explains how to do it in a reliable way instead.
1 parent d9ca981 commit 7e44db1

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

spec/expression.dd

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3676,6 +3676,27 @@ function: 'test.main', pretty function: 'int test.main(string[] args)',
36763676
file full path: '/example/test.d'
36773677
)
36783678

3679+
$(PANEL $(P $(RED Warning): Do not use $(D mixin(__FUNCTION__)) to get the
3680+
symbol for the current function. This seems to be a common
3681+
thing for programmers to attempt when trying to get the symbol
3682+
for the current function in order to do introspection on it,
3683+
since D does not currently have a direct way to get that
3684+
symbol. However, using $(D mixin(__FUNCTION__)) means that the
3685+
symbol for the function will be looked up by name, which means
3686+
that it's subject to the various rules that go with symbol
3687+
lookup, which can cause various problems. One such problem
3688+
would that if a function is overloaded, the result will be the
3689+
first overload whether that's the current function or not.)
3690+
3691+
$(P Given that D doesn't currently have a way to directly get the
3692+
symbol for the current function, the best way to do it is to
3693+
get the parent symbol of a symbol within the function, since
3694+
that avoids any issues surrounding symbol lookup rules. An
3695+
example of that which doesn't rely on any other symbols is
3696+
$(D __traits(parent {})). It declares an anonymous, nested
3697+
function, whose parent is then the current function. So,
3698+
getting its parent gets the symbol for the current function.))
3699+
36793700
$(H2 $(LNAME2 associativity, Associativity and Commutativity))
36803701

36813702
$(P An implementation may rearrange the evaluation of expressions

0 commit comments

Comments
 (0)