digitalmars.D.learn - Two questions
- IM (17/17) Jan 02 2019 1- How do I do in D the equivalent of the following C++ macro?
- H. S. Teoh (16/21) Jan 02 2019 [...]
- IM (2/23) Jan 02 2019 Thank you so much. Will give this a try.
- Steven Schveighoffer (12/29) Jan 02 2019 With those ... I have to guess.
- IM (5/18) Jan 02 2019 Perfect, this was it. Thank you so much. doSomeWork() was nested
1- How do I do in D the equivalent of the following C++ macro? #define OUT_VAL(val) (count << #val << " = " << val << endl) In particular the #val above to the actual macro argument as a string? 2- Yesterday I was experimenting with something and I wrote something like the following: struct MyType { ... } void doSomeWork(ref MyType o) { ... } auto t = MyType(...); t.doSomeWork(); // <-- failed to compile. Why did the above UFCS call fail to compile? I had to do doSomeWork(t) instead. Thank you so much!
Jan 02 2019
On Wed, Jan 02, 2019 at 05:38:41PM +0000, IM via Digitalmars-d-learn wrote:1- How do I do in D the equivalent of the following C++ macro? #define OUT_VAL(val) (count << #val << " = " << val << endl) In particular the #val above to the actual macro argument as a string?[...] Try something along these lines: import std.stdio; void OUT_VAL(alias val)() { writefln("%s = %s", __traits(identifier, val), val); } void main() { int i = 123; string s = "abc"; OUT_VAL!i; OUT_VAL!s; } T -- The easy way is the wrong way, and the hard way is the stupid way. Pick one.
Jan 02 2019
On Wednesday, 2 January 2019 at 17:49:52 UTC, H. S. Teoh wrote:On Wed, Jan 02, 2019 at 05:38:41PM +0000, IM via Digitalmars-d-learn wrote:Thank you so much. Will give this a try.1- How do I do in D the equivalent of the following C++ macro? #define OUT_VAL(val) (count << #val << " = " << val << endl) In particular the #val above to the actual macro argument as a string?[...] Try something along these lines: import std.stdio; void OUT_VAL(alias val)() { writefln("%s = %s", __traits(identifier, val), val); } void main() { int i = 123; string s = "abc"; OUT_VAL!i; OUT_VAL!s; } T
Jan 02 2019
On 1/2/19 12:38 PM, IM wrote:2- Yesterday I was experimenting with something and I wrote something like the following: struct MyType { ... } void doSomeWork(ref MyType o) { ... } auto t = MyType(...); t.doSomeWork(); // <-- failed to compile. Why did the above UFCS call fail to compile? I had to do doSomeWork(t) instead.With those ... I have to guess. There are 2 possibilities. Possibility 1: there is a method named 'doSomeWork' which takes at least one parameter. This overrides the UFCS function (member functions always win over UFCS). Possibility 2: All this is actually inside a function or unittest. Nested functions cannot participate in UFCS. Of course, these are guesses. But given the very scant code above, I'm not sure I could offer any other suggestions. If neither of those is the case, I'd need a working example. -Steve
Jan 02 2019
On Wednesday, 2 January 2019 at 21:56:03 UTC, Steven Schveighoffer wrote:On 1/2/19 12:38 PM, IM wrote:Perfect, this was it. Thank you so much. doSomeWork() was nested inside a unittest{} block. I didn't know UFCS won't work in this case.[...]With those ... I have to guess. There are 2 possibilities. Possibility 1: there is a method named 'doSomeWork' which takes at least one parameter. This overrides the UFCS function (member functions always win over UFCS). Possibility 2: All this is actually inside a function or unittest. Nested functions cannot participate in UFCS.Of course, these are guesses. But given the very scant code above, I'm not sure I could offer any other suggestions. If neither of those is the case, I'd need a working example. -Steve
Jan 02 2019