www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - What are 'op' codes?

reply kinghajj <kinghajj_member pathlink.com> writes:
My guess is that they handle what the class does when used in an operater (like
'==', '+', etc.). Is that so, and how do you use them?
Aug 18 2004
parent reply J C Calvarese <jcc7 cox.net> writes:
kinghajj wrote:
 My guess is that they handle what the class does when used in an operater (like
 '==', '+', etc.). Is that so, and how do you use them?
Thanks to the power of Google, I'll submit my guess. http://www.wlug.org.nz/OpCodes Op Codes The names given to individual instructions in AssemblyLanguage. Different CPU families have different op codes, and assembler has mnemonic names for the different instructions (which to the CPU are just 1s and 0s). For Intel x86 machines, common codes include JMP, AND, XOR, INT, MOVL, PUSH, POP, CALL, and so on. -- Justin (a/k/a jcc7) http://jcc_7.tripod.com/d/
Aug 18 2004
parent reply John Reimer <brk_6502 NOSP_AM.yahoo.com> writes:
J C Calvarese wrote:

 kinghajj wrote:
 
 My guess is that they handle what the class does when used in an 
 operater (like
 '==', '+', etc.). Is that so, and how do you use them?
Thanks to the power of Google, I'll submit my guess. http://www.wlug.org.nz/OpCodes Op Codes The names given to individual instructions in AssemblyLanguage. Different CPU families have different op codes, and assembler has mnemonic names for the different instructions (which to the CPU are just 1s and 0s). For Intel x86 machines, common codes include JMP, AND, XOR, INT, MOVL, PUSH, POP, CALL, and so on.
Wasn't he just asking about operator overloading? You know... opCmp opEquals, opAdd, and such...
Aug 19 2004
next sibling parent reply kinghajj <kinghajj_member pathlink.com> writes:
In article <cg1mdh$12mg$1 digitaldaemon.com>, John Reimer says...
J C Calvarese wrote:

 kinghajj wrote:
 
 My guess is that they handle what the class does when used in an 
 operater (like
 '==', '+', etc.). Is that so, and how do you use them?
Thanks to the power of Google, I'll submit my guess. http://www.wlug.org.nz/OpCodes Op Codes The names given to individual instructions in AssemblyLanguage. Different CPU families have different op codes, and assembler has mnemonic names for the different instructions (which to the CPU are just 1s and 0s). For Intel x86 machines, common codes include JMP, AND, XOR, INT, MOVL, PUSH, POP, CALL, and so on.
Wasn't he just asking about operator overloading? You know... opCmp opEquals, opAdd, and such...
Yes, I was, and I've already answered my own question (by reading the documentation "Operator Overloading"). It's a nice feature : I can see how it's so usefull!
Aug 19 2004
parent reply kinghajj <kinghajj_member pathlink.com> writes:
In article <cg1oah$18br$1 digitaldaemon.com>, kinghajj says...
In article <cg1mdh$12mg$1 digitaldaemon.com>, John Reimer says...
J C Calvarese wrote:

 kinghajj wrote:
 
 My guess is that they handle what the class does when used in an 
 operater (like
 '==', '+', etc.). Is that so, and how do you use them?
Thanks to the power of Google, I'll submit my guess. http://www.wlug.org.nz/OpCodes Op Codes The names given to individual instructions in AssemblyLanguage. Different CPU families have different op codes, and assembler has mnemonic names for the different instructions (which to the CPU are just 1s and 0s). For Intel x86 machines, common codes include JMP, AND, XOR, INT, MOVL, PUSH, POP, CALL, and so on.
Wasn't he just asking about operator overloading? You know... opCmp opEquals, opAdd, and such...
Yes, I was, and I've already answered my own question (by reading the documentation "Operator Overloading"). It's a nice feature : I can see how it's so usefull!
OK, now another question about the 'op' codes. You know how C++ has the cout stream, and it workd like this: cout << "Whatever, " << variable << " again"; How could you do that in D?
Aug 19 2004
next sibling parent John Reimer <brk_6502 NOSP_AM.yahoo.com> writes:
 OK, now another question about the 'op' codes.
 
 You know how C++ has the cout stream, and it workd like this:
 cout << "Whatever, " << variable << " again";
 
 How could you do that in D?
 
 
Operator overloading... You can do the exact same thing in D if the library you are using has overloaded those operators in such a fashion. Although overloading shift operators in such a way is not popular to everyone in D (I don't like it). The only library that currently lets you do it that way, that I know of, is mango on its io streams. But mango also gives an alternative way to do the same thing, thankfully. Later, John
Aug 19 2004
prev sibling next sibling parent reply Stephen Waits <steve waits.net> writes:
kinghajj wrote:
 You know how C++ has the cout stream, and it workd like this:
 cout << "Whatever, " << variable << " again";
 
 How could you do that in D?
Hopefully you can't. The thing is, you aren't going to find many, or any, in this group of people that believe that overloading operator << to deal with stream output is a good idea. Personally, I suggest you forget you ever saw that in C++. --Steve
Aug 19 2004
next sibling parent kinghajj <kinghajj_member pathlink.com> writes:
In article <cg2q79$284j$1 digitaldaemon.com>, Stephen Waits says...
kinghajj wrote:
 You know how C++ has the cout stream, and it workd like this:
 cout << "Whatever, " << variable << " again";
 
 How could you do that in D?
Hopefully you can't. The thing is, you aren't going to find many, or any, in this group of people that believe that overloading operator << to deal with stream output is a good idea. Personally, I suggest you forget you ever saw that in C++. --Steve
I never said it was a good idea, I was just wondering if it could be done.
Aug 19 2004
prev sibling parent "Matthew" <admin.hat stlsoft.dot.org> writes:
"Stephen Waits" <steve waits.net> wrote in message
news:cg2q79$284j$1 digitaldaemon.com...
 kinghajj wrote:
 You know how C++ has the cout stream, and it workd like this:
 cout << "Whatever, " << variable << " again";

 How could you do that in D?
Hopefully you can't. The thing is, you aren't going to find many, or any, in this group of people that believe that overloading operator << to deal with stream output is a good idea. Personally, I suggest you forget you ever saw that in C++.
Hear, hear! (And I'm supposed to be a C++ honcho! <g>)
Aug 19 2004
prev sibling parent reply Andy Friesen <andy ikagames.com> writes:
kinghajj wrote:

 OK, now another question about the 'op' codes.
 
 You know how C++ has the cout stream, and it workd like this:
 cout << "Whatever, " << variable << " again";
 
 How could you do that in D?
It's not the "D way", but it's still pretty simple: class CppStream { CppStream opShl(int x) { write(x); return this; } CppStream opShl(float f) { write(f); return this; } } Other classes can define << behaviour between themselves and streams by implementing an opShl_r() method: class Thing { CppStream opShl_r(CppStream lhs) { // In this case, 'this' is the left-hand-side argument // not the right hand side. lhs.write(this); return lhs; } } Like I said, though, it's supposedly not the "D way", so you will be set on fire and chased out of town or something (in that exact order) if you do it. -- andy
Aug 19 2004
parent reply kinghajj <kinghajj_member pathlink.com> writes:
In article <cg2see$29mf$1 digitaldaemon.com>, Andy Friesen says...
kinghajj wrote:

 OK, now another question about the 'op' codes.
 
 You know how C++ has the cout stream, and it workd like this:
 cout << "Whatever, " << variable << " again";
 
 How could you do that in D?
It's not the "D way", but it's still pretty simple: class CppStream { CppStream opShl(int x) { write(x); return this; } CppStream opShl(float f) { write(f); return this; } } Other classes can define << behaviour between themselves and streams by implementing an opShl_r() method: class Thing { CppStream opShl_r(CppStream lhs) { // In this case, 'this' is the left-hand-side argument // not the right hand side. lhs.write(this); return lhs; } } Like I said, though, it's supposedly not the "D way", so you will be set on fire and chased out of town or something (in that exact order) if you do it. -- andy
It's best to provide multiple options, to have more than one way, no? If someone could implement the C++ iostreams, maybe more C++ programmers would look into it. But, of course, still have writef() for us :)
Aug 19 2004
next sibling parent reply Andy Friesen <andy ikagames.com> writes:
kinghajj wrote:

 In article <cg2see$29mf$1 digitaldaemon.com>, Andy Friesen says...
 
Like I said, though, it's supposedly not the "D way", so you will be set 
on fire and chased out of town or something (in that exact order) if you 
do it.

 -- andy
It's best to provide multiple options, to have more than one way, no? If someone could implement the C++ iostreams, maybe more C++ programmers would look into it. But, of course, still have writef() for us :)
I think I prefer the C++ way because it offers compile time typesafety, as opposed to runtime. (it's also hypothetically a hair faster because of this) That being said, it's best not to think in C++ when writing D. -- andy
Aug 19 2004
parent reply "Ivan Senji" <ivan.senji public.srce.hr> writes:
"Andy Friesen" <andy ikagames.com> wrote in message
news:cg2tlb$2an8$1 digitaldaemon.com...
 kinghajj wrote:

 In article <cg2see$29mf$1 digitaldaemon.com>, Andy Friesen says...

Like I said, though, it's supposedly not the "D way", so you will be set
on fire and chased out of town or something (in that exact order) if you
do it.

 -- andy
It's best to provide multiple options, to have more than one way, no? If
someone
 could implement the C++ iostreams, maybe more C++ programmers would look
into
 it. But, of course, still have writef() for us :)
I think I prefer the C++ way because it offers compile time typesafety, as opposed to runtime. (it's also hypothetically a hair faster because of this)
I prefer the C++ way too! Plus: writef is a really nice function but it caused problems to me i wouldn't get if i used something like iostream << I was printing strings: with this code: char[] str;//some string writef(str); It all looks very simple and nice but what if the string i want to print is "%"? You get a nice little exception: "Error: std.format invalid specifier" So basically i can't use writef to print strings for wich i don't know what they contain? The best way IMO would be to have something like iostreams combined with formating when needed. like this: int x; char[] str = "%"; cout << str << format("%5d",x); This would print "str" correctly.
 That being said, it's best not to think in C++ when writing D.
<< and >> as stream operators appeared in C++ but they shouldn't automatically be non-D way of thinking. As you said: they are a nice typesafe compiletime way of doing streams. What is there not to like about that? And: It is not operator abuse if you use an operator in a way that everyone can understand and use. example: opShl defined on stream: Who would ever think that "stream << 5 << "Hello""; means that you are shifting a stream? :)
   -- andy
Aug 19 2004
parent Regan Heath <regan netwin.co.nz> writes:
On Thu, 19 Aug 2004 23:39:56 +0200, Ivan Senji <ivan.senji public.srce.hr> 
wrote:

<snip>

 I prefer the C++ way too! Plus: writef is a really nice function but it
 caused
 problems to me i wouldn't get if i used something like iostream <<
 I was printing strings: with this code:

 char[] str;//some string
 writef(str);

 It all looks very simple and nice but what if the string i want
 to print is "%"? You get a nice little exception:
 "Error: std.format invalid specifier"

 So basically i can't use writef to print strings for wich i don't know
 what they contain?
Can't you do the same as I do with printf in C? i.e. The solution is: printf("%s",str); //or is it %S? in other words, always specify a format string, that way the string you're printing does not get interpreted as a format string. However... Looking at the function signature in the phobos docs writef is defined as: void writef(...); Combined with the description "Arguments are formatted per the format strings and written to stdout" suggests to me that all arguments are treated as format strings, meaning you cannot do what I have shown above with writef. This seems wrong to me. In your case you simply want a 'write' function that does not expect a format string.
 The best way IMO would be to have something like iostreams
 combined with formating when needed.
 like this:

 int x;
 char[] str = "%";
 cout << str << format("%5d",x);

 This would print "str" correctly.


 That being said, it's best not to think in C++ when writing D.
<< and >> as stream operators appeared in C++ but they shouldn't automatically be non-D way of thinking. As you said: they are a nice typesafe compiletime way of doing streams. What is there not to like about that?
Whats the big advantage of 'compiletime' type safety over 'runtime'? What bug does << prevent that you get with writef (which is type safe)?
 And: It is not operator abuse if you use an operator in a way that
 everyone can understand and use.
The word 'everyone' is incorrect. A lot of people might know what it means, but only if they come from a c++ background. Personally I came from C, and had no idea what it did the first time I saw it, I assumed it was shifting something but could not decide what.
 example: opShl defined on stream: Who would ever think
 that "stream << 5 << "Hello""; means that you are shifting a stream?
Yes, that is exactly what I thought (when I first saw it), and it made no sense whatsoever. Regan -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Aug 19 2004
prev sibling parent "Matthew" <admin.hat stlsoft.dot.org> writes:
"kinghajj" <kinghajj_member pathlink.com> wrote in message
news:cg2t3j$2a13$1 digitaldaemon.com...
 In article <cg2see$29mf$1 digitaldaemon.com>, Andy Friesen says...
kinghajj wrote:

 OK, now another question about the 'op' codes.

 You know how C++ has the cout stream, and it workd like this:
 cout << "Whatever, " << variable << " again";

 How could you do that in D?
It's not the "D way", but it's still pretty simple: class CppStream { CppStream opShl(int x) { write(x); return this; } CppStream opShl(float f) { write(f); return this; } } Other classes can define << behaviour between themselves and streams by implementing an opShl_r() method: class Thing { CppStream opShl_r(CppStream lhs) { // In this case, 'this' is the left-hand-side argument // not the right hand side. lhs.write(this); return lhs; } } Like I said, though, it's supposedly not the "D way", so you will be set on fire and chased out of town or something (in that exact order) if you do it. -- andy
It's best to provide multiple options, to have more than one way, no? If someone could implement the C++ iostreams, maybe more C++ programmers would look into it. But, of course, still have writef() for us :)
I'm skeptical that you'll get many true C++ aficionados who also grok other languages that would advocate it's being included a priori in D.
Aug 19 2004
prev sibling parent reply J C Calvarese <jcc7 cox.net> writes:
In article <cg1mdh$12mg$1 digitaldaemon.com>, John Reimer says...
J C Calvarese wrote:

 kinghajj wrote:
 
 My guess is that they handle what the class does when used in an 
 operater (like
 '==', '+', etc.). Is that so, and how do you use them?
Thanks to the power of Google, I'll submit my guess. http://www.wlug.org.nz/OpCodes Op Codes The names given to individual instructions in AssemblyLanguage. Different CPU families have different op codes, and assembler has mnemonic names for the different instructions (which to the CPU are just 1s and 0s). For Intel x86 machines, common codes include JMP, AND, XOR, INT, MOVL, PUSH, POP, CALL, and so on.
Wasn't he just asking about operator overloading? You know... opCmp opEquals, opAdd, and such...
Apparently so. If he would've asked what op functions like opCmp were I would've answered a whole other question. In the future, I guess I'll leave the psychic hotline questions to Miss Cleo.... jcc7
Aug 19 2004
parent reply John Reimer <brk_6502 NOSP_AM.yahoo.com> writes:
J C Calvarese wrote:

 
 Apparently so. If he would've asked what op functions like opCmp were I
would've
 answered a whole other question.
 
 In the future, I guess I'll leave the psychic hotline questions to Miss
Cleo....
 
 jcc7
Hey, what are you saying, Justin? You looking for a fight with me and my psychic abilities? j/k :-)
Aug 19 2004
parent J C Calvarese <jcc7 cox.net> writes:
In article <cg294n$1v55$2 digitaldaemon.com>, John Reimer says...
J C Calvarese wrote:

 
 Apparently so. If he would've asked what op functions like opCmp were I
would've
 answered a whole other question.
 
 In the future, I guess I'll leave the psychic hotline questions to Miss
Cleo....
 
 jcc7
Hey, what are you saying, Justin? You looking for a fight with me and my psychic abilities?
LOL, no. I'm just tired of when people ask a vague question, and I get criticized for answering the wrong question. Just too thin skinned, I suppose. I wasn't mad at you.
j/k  :-)
jcc7
Aug 19 2004