www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - # operator under C implementation in D1

reply Sam Hu <samhu.samhu gmail.com> writes:

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
Mar 12 2009
next sibling parent reply Daniel Keep <daniel.keep.lists gmail.com> writes:
Sam Hu wrote:

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
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
Mar 12 2009
parent reply Sam Hu <samhu.samhu gmail.com> writes:
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
Mar 12 2009
parent reply Sam Hu <samhu.samhu gmail.com> writes:
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
Mar 12 2009
parent reply Sam Hu <samhu.samhu gmail.com> writes:
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.
Mar 12 2009
parent reply Sam Hu <samhu.samhu gmail.com> writes:
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.
Mar 12 2009
parent reply Sergey Gromov <snake.scaly gmail.com> writes:
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")
Mar 14 2009
parent Sam Hu <samhu.samhu gmail.com> writes:
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
Mar 15 2009
prev sibling next sibling parent "Denis Koroskin" <2korden gmail.com> writes:
On Fri, 13 Mar 2009 04:19:11 +0300, Sam Hu <samhu.samhu gmail.com> wrote:


 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; }
Mar 12 2009
prev sibling parent Sergey Gromov <snake.scaly gmail.com> writes:
Thu, 12 Mar 2009 21:19:11 -0400, Sam Hu wrote:


 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.
"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.
Mar 14 2009