August 19, 2004 Re: What are 'op' codes? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stephen Waits | 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. |
August 19, 2004 Re: What are 'op' codes? | ||||
---|---|---|---|---|
| ||||
Posted in reply to kinghajj | 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
|
August 19, 2004 Re: What are 'op' codes? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andy Friesen | 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 :) |
August 19, 2004 Re: What are 'op' codes? | ||||
---|---|---|---|---|
| ||||
Posted in reply to kinghajj | 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
|
August 19, 2004 Re: What are 'op' codes? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andy Friesen | "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 |
August 19, 2004 Re: What are 'op' codes? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ivan Senji | 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/ |
August 19, 2004 Re: What are 'op' codes? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stephen Waits | "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>) |
August 19, 2004 Re: What are 'op' codes? | ||||
---|---|---|---|---|
| ||||
Posted in reply to kinghajj | "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. |
Copyright © 1999-2021 by the D Language Foundation