Thread overview | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
March 13, 2009 # operator under C implementation in D1 | ||||
---|---|---|---|---|
| ||||
I know a little that in C there is a # operator which under a macro can return any type's value in character format.Just wanna know whether there is an equivelent implementation in D1.Say,in C using a/m macro can easily output enum's character value other than integer.It would be grateful if anybody would like to paste a few lines of code to demostrate such implementation. Regards, Sam |
March 13, 2009 Re: # operator under C implementation in D1 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sam Hu |
Sam Hu wrote:
> I know a little that in C there is a # operator which under a macro can return any type's value in character format.Just wanna know whether there is an equivelent implementation in D1.Say,in C using a/m macro can easily output enum's character value other than integer.It would be grateful if anybody would like to paste a few lines of code to demostrate such implementation.
>
> Regards,
> Sam
D doesn't have text substitution macros, so it doesn't have a # operator.
The closest you can get is .stringof, which has strange and mysterious ways. For example, int.stringof, (1+2).stringof, etc.
That, or just quote out the thing you want to turn into a string.
-- Daniel
|
March 13, 2009 Re: # operator under C implementation in D1 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sam Hu | On Fri, 13 Mar 2009 04:19:11 +0300, Sam Hu <samhu.samhu@gmail.com> wrote: > I know a little that in C there is a # operator which under a macro can return any type's value in character format.Just wanna know whether there is an equivelent implementation in D1.Say,in C using a/m macro can easily output enum's character value other than integer.It would be grateful if anybody would like to paste a few lines of code to demostrate such implementation. > > Regards, > Sam // C version #include <stdio.h> #define Stringize(x) #x int main() { int foo = 42; const char* s = Stringize(foo); printf("%s", s); return 0; } // D version import std.stdio; template Stringize(alias s) { auto Stringize = s.stringof; } int main() { auto foo = 42; string s = Stringize!(foo); writefln("%s", s); return 0; } |
March 13, 2009 Re: # operator under C implementation in D1 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Keep | Thank you very much both!!!I really learnt a lot.And write an excise based on your a/m. //**************** module stringDisp; import samsTools.PromptMessage; import tango.io.Stdout; enum Color {Red,Green,Blue,Yellow,Pink,Black} template Stringize(alias s) { auto Stringize=s.stringof; } void testEnum() { Color color=Color.Red; char[] colorName=Stringize!(color); Stdout.formatln("{}",colorName); } int main(char[][] args) { auto foo=42; char[] s=Stringize!(foo); Stdout.formatln("{}",s); testEnum; pause; return 0; } //*************** I noticed that this will print the variable's type name in string format,say,Enum Color{} Color color=Color.Red... The Stringize!(color) will print INT.What if I want it print "Red" or "Color.Red"? Thanks and best regards, Sam |
March 13, 2009 Re: # operator under C implementation in D1 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sam Hu | Sorry,and another question: What does ' alias s' mean in the template parm list? What's more,sometimes I noticed people write code like this: SomeType something...; alias SomeType MyType;//I understood alias systemProvidedType SomeType;//eh? say enum Color{} alias Color ColorType;//understood; alias int Color;//What's going on here? I was confused everytime when I met such code. Salute! Sam |
March 13, 2009 Re: # operator under C implementation in D1 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sam Hu | Sorry again.What I mean here in above sample alias int Color ; My confusion is that how can we rename int to an alreay exist user defined type,say Color?What's the purpose? Maybe my example is not so accurate to explain my confustion.Will paste a piece of code from tango lib when I get later. |
March 13, 2009 Re: # operator under C implementation in D1 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sam Hu | Here I found an example form tango.io.Console class Output: class Output { private Buffer buffer; private bool redirect; public alias append opCall; public alias flush opCall; final Output append (char[] x) { buffer.append (x.ptr, x.length); return this; } ... } On above code, public alias append opCall; public alias flush opCall; How can we do things like this since the opCall is the system defined? Thanks. |
March 14, 2009 Re: # operator under C implementation in D1 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sam Hu | Thu, 12 Mar 2009 23:44:40 -0400, Sam Hu wrote:
> Here I found an example form tango.io.Console class Output:
> class Output
> {
> private Buffer buffer;
> private bool redirect;
>
> public alias append opCall;
> public alias flush opCall;
>
> final Output append (char[] x)
> {
> buffer.append (x.ptr, x.length);
> return this;
> }
> ...
> }
> On above code,
> public alias append opCall;
> public alias flush opCall;
>
> How can we do things like this since the opCall is the system defined? Thanks.
You are correct in that it's impossible to re-alias an existing type. Your previous example with Color won't compile. Where you are wrong is in assumption that opCall is "system defined." It is not, actually. Compiler just searches for a function with this name. It uses one if found, or performs a default action otherwise. There is *no* compiler-generated opCall.
In this example, opCall is made a synonym for append and for flush:
Output o = ...;
o(); // equivalent to o.flush()
o("string"); // equivalent to o.append("string")
|
March 14, 2009 Re: # operator under C implementation in D1 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sam Hu | Thu, 12 Mar 2009 21:19:11 -0400, Sam Hu wrote: > I know a little that in C there is a # operator which under a macro can return any type's value in character format.Just wanna know whether there is an equivelent implementation in D1.Say,in C using a/m macro can easily output enum's character value other than integer.It would be grateful if anybody would like to paste a few lines of code to demostrate such implementation. I know this doesn't help, but the C preprocessor's # operator does not "return ... value in character format". All it does it converts a macro argument into a string literal: #define STRINGIZE(x) #x int foo = 43; const char *s = STRINGIZE(foo); // s now is "foo", not "43" const char *s1 = STRINGIZE(foo * 15 + 1); // s1 is "foo * 15 + 1" This is exactly what D's .stringof does. |
March 16, 2009 Re: # operator under C implementation in D1 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sergey Gromov | Sergey Gromov Wrote:
. Where you are wrong is
> in assumption that opCall is "system defined." It is not, actually. Compiler just searches for a function with this name. It uses one if found, or performs a default action otherwise. There is *no* compiler-generated opCall.
Got it!Now I understood.
Thank you very much!!!
Regards,
Sam
|
Copyright © 1999-2021 by the D Language Foundation