www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Two questions

reply IM <3di gm.com> writes:
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
next sibling parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
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
parent IM <3di gm.com> writes:
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:
 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
Thank you so much. Will give this a try.
Jan 02
prev sibling parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
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
parent IM <3di gm.com> writes:
On Wednesday, 2 January 2019 at 21:56:03 UTC, Steven 
Schveighoffer wrote:
 On 1/2/19 12:38 PM, IM wrote:

 [...]
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.
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.
 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