Thread overview | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
November 06, 2003 formatted output trial balloon | ||||
---|---|---|---|---|
| ||||
Since with () overloading class objects can now look like functions, I've been toying with the idea of using this for formatted I/O. Here's a trial mockup of what it might look like: ----------------------------------------------- import std.string; import std.c.stdio; import std.c.stdlib; class Stdout { Stdout opCall(int i) { printf("%d", i); return this; } Stdout opCall(char[] format, ...) { va_list ap; ap = cast(va_list)&format; ap += format.size; vprintf(format, ap); return this; } Stdout nl() { printf("\n"); return this; } } void main() { Stdout stdout = new Stdout(); stdout("hello")(" world")(47).nl()("bar")("%06i\n", 62); } ------------------------------------------- (Note that it retains the power of printf!) The issue here is how the syntax stdout("foo")("bar") looks. In C++ it would look like stdout<<"foo"<<"bar" which I don't find appealing. |
November 06, 2003 Re: formatted output trial balloon | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | Andy Friesen also had very close to this idea earlier. -Walter http://www.digitalmars.com/drn-bin/wwwnews?D/17402 |
November 06, 2003 Re: formatted output trial balloon | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | I like it! Im not a fan of the .nl() though it looks inconsistent, not sure how else to handle it though. C "Walter" <walter@digitalmars.com> wrote in message news:boc9me$1g2b$1@digitaldaemon.com... > Since with () overloading class objects can now look like functions, I've been toying with the idea of using this for formatted I/O. Here's a trial mockup of what it might look like: > > ----------------------------------------------- > import std.string; > import std.c.stdio; > import std.c.stdlib; > > class Stdout > { > Stdout opCall(int i) > { > printf("%d", i); > return this; > } > > Stdout opCall(char[] format, ...) > { > va_list ap; > ap = cast(va_list)&format; > ap += format.size; > vprintf(format, ap); > return this; > } > > Stdout nl() > { > printf("\n"); > return this; > } > } > > void main() > { Stdout stdout = new Stdout(); > stdout("hello")(" world")(47).nl()("bar")("%06i\n", 62); > } > ------------------------------------------- > > (Note that it retains the power of printf!) The issue here is how the syntax > stdout("foo")("bar") looks. In C++ it would look like stdout<<"foo"<<"bar" > which I don't find appealing. > > |
November 06, 2003 Re: formatted output trial balloon | ||||
---|---|---|---|---|
| ||||
Posted in reply to Charles Sanders | Note .nl() is the same as ("\n") if that's any better. -Ben "Charles Sanders" <sanders-consulting@comcast.net> wrote in message news:bocf1q$1o62$1@digitaldaemon.com... > I like it! Im not a fan of the .nl() though it looks inconsistent, not sure > how else to handle it though. > > C > > "Walter" <walter@digitalmars.com> wrote in message news:boc9me$1g2b$1@digitaldaemon.com... > > Since with () overloading class objects can now look like functions, I've > > been toying with the idea of using this for formatted I/O. Here's a trial > > mockup of what it might look like: > > > > ----------------------------------------------- > > import std.string; > > import std.c.stdio; > > import std.c.stdlib; > > > > class Stdout > > { > > Stdout opCall(int i) > > { > > printf("%d", i); > > return this; > > } > > > > Stdout opCall(char[] format, ...) > > { > > va_list ap; > > ap = cast(va_list)&format; > > ap += format.size; > > vprintf(format, ap); > > return this; > > } > > > > Stdout nl() > > { > > printf("\n"); > > return this; > > } > > } > > > > void main() > > { Stdout stdout = new Stdout(); > > stdout("hello")(" world")(47).nl()("bar")("%06i\n", 62); > > } > > ------------------------------------------- > > > > (Note that it retains the power of printf!) The issue here is how the > syntax > > stdout("foo")("bar") looks. In C++ it would look like stdout<<"foo"<<"bar" > > which I don't find appealing. > > > > > > |
November 06, 2003 Re: formatted output trial balloon | ||||
---|---|---|---|---|
| ||||
Posted in reply to Charles Sanders | Charles Sanders wrote: >I like it! Im not a fan of the .nl() though it looks inconsistent, not sure >how else to handle it though. > >C > > Instead of nl you could have a constant, ie endl stdout("hello")(" world")(47)(endl)("bar")("%06i\n", 62); |
November 06, 2003 Re: formatted output trial balloon | ||||
---|---|---|---|---|
| ||||
Posted in reply to J Anderson | "J Anderson" <REMOVEanderson@badmama.com.au> wrote in message news:bocj2a$1tns$2@digitaldaemon.com... > Charles Sanders wrote: > > >I like it! Im not a fan of the .nl() though it looks inconsistent, not sure > >how else to handle it though. > > Instead of nl you could have a constant, ie endl Yah. > stdout("hello")(" world")(47)(endl)("bar")("%06i\n", 62); Why on earth would you go to all the trouble to replace printf, and end up supporting the same bad technique anyway in all its untype-safe glory? That last bit should be some kind of integer formatting function or object. Sean |
November 06, 2003 Re: formatted output trial balloon | ||||
---|---|---|---|---|
| ||||
Posted in reply to Charles Sanders | In article <bocf1q$1o62$1@digitaldaemon.com>, Charles Sanders says... > >I like it! Im not a fan of the .nl() though it looks inconsistent, not sure how else to handle it though. > >C I agree. ("\n") seems a little cleaner to me. Also, would it make sense to declare Stdout static in stdio so you don't need to instantiate it each time? -Jon > >"Walter" <walter@digitalmars.com> wrote in message news:boc9me$1g2b$1@digitaldaemon.com... >> Since with () overloading class objects can now look like functions, I've been toying with the idea of using this for formatted I/O. Here's a trial mockup of what it might look like: >> >> ----------------------------------------------- >> import std.string; >> import std.c.stdio; >> import std.c.stdlib; >> >> class Stdout >> { >> Stdout opCall(int i) >> { >> printf("%d", i); >> return this; >> } >> >> Stdout opCall(char[] format, ...) >> { >> va_list ap; >> ap = cast(va_list)&format; >> ap += format.size; >> vprintf(format, ap); >> return this; >> } >> >> Stdout nl() >> { >> printf("\n"); >> return this; >> } >> } >> >> void main() >> { Stdout stdout = new Stdout(); >> stdout("hello")(" world")(47).nl()("bar")("%06i\n", 62); >> } >> ------------------------------------------- >> >> (Note that it retains the power of printf!) The issue here is how the >syntax >> stdout("foo")("bar") looks. In C++ it would look like stdout<<"foo"<<"bar" >> which I don't find appealing. >> >> > > |
November 06, 2003 Re: formatted output trial balloon | ||||
---|---|---|---|---|
| ||||
Posted in reply to J Anderson | But what would endl be defined as, an arbitrary number ? How would it know i didnt actually want to output that specific number ? C "J Anderson" <REMOVEanderson@badmama.com.au> wrote in message news:bocj2a$1tns$2@digitaldaemon.com... > Charles Sanders wrote: > > >I like it! Im not a fan of the .nl() though it looks inconsistent, not sure > >how else to handle it though. > > > >C > > > > > Instead of nl you could have a constant, ie endl > > stdout("hello")(" world")(47)(endl)("bar")("%06i\n", 62); > > |
November 06, 2003 Re: formatted output trial balloon | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | Walter wrote: > Since with () overloading class objects can now look like functions, I've > been toying with the idea of using this for formatted I/O. Here's a trial > mockup of what it might look like: > > [...] > (Note that it retains the power of printf!) The issue here is how the syntax > stdout("foo")("bar") looks. In C++ it would look like stdout<<"foo"<<"bar" > which I don't find appealing. I think () is a bit too busy. '~' may be better, since it's a lot less hoggish in terms of eyeball-space. Plus, D already uses it to indicate concatenation. stdout ~ "Hello " ~ "world" ~ 47 ~ endl ~ "bar" ~ format("%06i\n", 62); -- andy |
November 06, 2003 Re: formatted output trial balloon | ||||
---|---|---|---|---|
| ||||
Posted in reply to Charles Sanders | Charles Sanders wrote:
> But what would endl be defined as, an arbitrary number ? How would it know
> i didnt actually want to output that specific number ?
>
> C
A typedeffed value. Or some sort of unique mutator object.
-- andy
|
Copyright © 1999-2021 by the D Language Foundation