digitalmars.D.learn - Rational of some DMD decisions
- Robin (20/20) Feb 08 2015 Hiho,
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (17/20) Feb 08 2015 Post-increment cannot be overloaded in D. It is always implemented in
- Robin (4/4) Feb 08 2015 Thank you for your reply. I didn't know that it isn't possible to
Hiho, as I am currently very insterested in compiler implementation I often look into the DMD github and look how things are done there. Often I find myself curious about things at first but find out the rational behind certain implementation decisions. One thing I can't figure out is the different approach with pre- and postfix expressions. While prefix expressions inherit from UnaExp (unary expression) which makes sense to me as it is an expression with only one parameter (in general) the postfix inc and decrement expressions are inherited from BinExpr (binary expression) which define their second expression to be an integer expression with a value of 1. So I am very curious about the rational behind this design decision. Why do Postfix expression not inherit from UnaExp, too? Am I missing something very important? Thanks in advance! =) I hope this is the right place to ask questions about the DMD compiler. Regards, Robin
Feb 08 2015
On 02/08/2015 01:05 PM, Robin wrote:the postfix inc and decrement expressions are inherited from BinExpr (binary expression) which define their second expression to be an integer expression with a value of 1.Post-increment cannot be overloaded in D. It is always implemented in terms of pre-increment (or +=): http://dlang.org/operatoroverloading.html Although I can't find it on the spec, I think it used to be spelled out that pre-increment (and post-increment) could be the equivalent of "+= 1". I am pretty sure about that because my notes say so! :p (because I based everything on what I knew to be correct at the time). http://ddili.org/ders/d.en/operator_overloading.html Quoting myself: "if an opBinary overload supports the duration += 1 usage, then opUnary need not be overloaded for ++duration and duration++. Instead, the compiler uses the duration += 1 expression behind the scenes. Similarly, the duration -= 1 overload covers the uses of --duration and duration-- as well." Perhaps that's why the compiler uses a binary operator for post-increment. Ali
Feb 08 2015
Thank you for your reply. I didn't know that it isn't possible to overload the post-inc and decrement operators in Dlang but I think I don't even miss this feature if pre-inc/dec is still available. =)
Feb 08 2015