www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Advocate OutBuffer.write return reference to itself rather than void

reply "Lynn Allan" <l_d_allan adelphia.net> writes:
<alert comment="newbie">

If appropriate, I would like to advocate that myOutBuffer.write(arg) return
a reference to itself rather than the current practice of not returning
anything (i.e. void). I believe this would lead to improved syntax and
possibly encourage wider usage of OutBuffer.

My impression is that it is more efficient to use a series of
myOutBuffer.write(arg) calls rather than appending with "~". However, the
resulting code seems more "tedious" and verbose than it has to be because
myOutBuffer.write(arg) doesn't return anything.

Inefficient but concise:
writefln(lastName ~ ", " ~ firstName ~ " " ~ middleInitial ~ ".");

More efficient but tediously verbose:
myOutBuffer.reserve(100);
myOutBuffer.write(lastName);
myOutBuffer.write(", ");
myOutBuffer.write(firstName);
myOutBuffer.write(" ");
myOutBuffer.write(middleInitial);
myOutBuffer.write(".");
writefln(myOutBuffer.toString());

As I recall, C++ and/or Java would allow "cascading / stringing together" of
the above series of write calls because it returned a pointer/reference to
itself rather than myOutBuffer.write(arg) returning void. This would allow
the following syntax which, to me, is preferred:

myOutBuffer.write(lastName).write(", ").write(firstName).write("
").write(middleInitial).write(".");

Am I missing or overlooking something? No doubt easier said than done, but
it seems like this change might be straightforward to implement. After all,
my impression is that the "~" operator is returning a reference to its
object to allow the concatenation to happen.

</alert>
Sep 14 2004
parent reply "Vathix" <vathixSpamFix dprogramming.com> writes:
"Lynn Allan" <l_d_allan adelphia.net> wrote in message
news:ci8f8q$l5n$1 digitaldaemon.com...
 <alert comment="newbie">

 If appropriate, I would like to advocate that myOutBuffer.write(arg)

 a reference to itself rather than the current practice of not returning
 anything (i.e. void). I believe this would lead to improved syntax and
 possibly encourage wider usage of OutBuffer.

 My impression is that it is more efficient to use a series of
 myOutBuffer.write(arg) calls rather than appending with "~". However, the
 resulting code seems more "tedious" and verbose than it has to be because
 myOutBuffer.write(arg) doesn't return anything.

 Inefficient but concise:
 writefln(lastName ~ ", " ~ firstName ~ " " ~ middleInitial ~ ".");

 More efficient but tediously verbose:
 myOutBuffer.reserve(100);
 myOutBuffer.write(lastName);
 myOutBuffer.write(", ");
 myOutBuffer.write(firstName);
 myOutBuffer.write(" ");
 myOutBuffer.write(middleInitial);
 myOutBuffer.write(".");
 writefln(myOutBuffer.toString());

 As I recall, C++ and/or Java would allow "cascading / stringing together"

 the above series of write calls because it returned a pointer/reference to
 itself rather than myOutBuffer.write(arg) returning void. This would allow
 the following syntax which, to me, is preferred:

 myOutBuffer.write(lastName).write(", ").write(firstName).write("
 ").write(middleInitial).write(".");

 Am I missing or overlooking something? No doubt easier said than done, but
 it seems like this change might be straightforward to implement. After

 my impression is that the "~" operator is returning a reference to its
 object to allow the concatenation to happen.

 </alert>

with(myOutBuffer) { reserve(100); write("fe"); write("fi"); write("fo"); write("fum"); } Even better?
Sep 14 2004
parent reply "Lynn Allan" <l_d_allan adelphia.net> writes:
But using the "With" statement, you get conflicts with imports and you end
up with code like:

#import std.outbuffer;
#import std.string;
#import std.stdio;
#
#void main()
#{
#  int areaCode = 800, lata = 555, lastPart = 1212;
#  std.outbuffer.OutBuffer myOutBufferFirst = new OutBuffer;
#  with(myOutBufferFirst) {
#  with(std.string) {                                      // <-- Ouch
#    write("Test of building phone number: ");
#    write('(');
#    write(toString(areaCode));
#    write(") ");
#    write(toString(lata));
#    write("-");
#    write(toString(lastPart));
#  }
#  }
#  writefln("myOutBufferFirst: ", myOutBufferFirst.toString());
// or
#  std.outbuffer.OutBuffer myOutBufferSecond = new OutBuffer;
#  with(myOutBufferSecond) {
#    write("Test of building phone number: ");
#    write('(');
#    write(std.string.toString(areaCode));  // <-- Ouch
#    write(") ");
#    write(std.string.toString(lata));
#    write("-");
#    write(std.string.toString(lastPart));
#  }
#  writefln("myOutBufferSecond: ", myOutBufferSecond.toString());
#}

"Vathix" <vathixSpamFix dprogramming.com> wrote in message
news:ci8hkf$n2o$1 digitaldaemon.com...
 "Lynn Allan" <l_d_allan adelphia.net> wrote in message
 news:ci8f8q$l5n$1 digitaldaemon.com...
 <alert comment="newbie">

 If appropriate, I would like to advocate that myOutBuffer.write(arg)

 a reference to itself rather than the current practice of not returning
 anything (i.e. void). I believe this would lead to improved syntax and
 possibly encourage wider usage of OutBuffer.

 My impression is that it is more efficient to use a series of
 myOutBuffer.write(arg) calls rather than appending with "~". However,


 resulting code seems more "tedious" and verbose than it has to be


 myOutBuffer.write(arg) doesn't return anything.

 Inefficient but concise:
 writefln(lastName ~ ", " ~ firstName ~ " " ~ middleInitial ~ ".");

 More efficient but tediously verbose:
 myOutBuffer.reserve(100);
 myOutBuffer.write(lastName);
 myOutBuffer.write(", ");
 myOutBuffer.write(firstName);
 myOutBuffer.write(" ");
 myOutBuffer.write(middleInitial);
 myOutBuffer.write(".");
 writefln(myOutBuffer.toString());

 As I recall, C++ and/or Java would allow "cascading / stringing


 of
 the above series of write calls because it returned a pointer/reference


 itself rather than myOutBuffer.write(arg) returning void. This would


 the following syntax which, to me, is preferred:

 myOutBuffer.write(lastName).write(", ").write(firstName).write("
 ").write(middleInitial).write(".");

 Am I missing or overlooking something? No doubt easier said than done,


 it seems like this change might be straightforward to implement. After

 my impression is that the "~" operator is returning a reference to its
 object to allow the concatenation to happen.

 </alert>

with(myOutBuffer) { reserve(100); write("fe"); write("fi"); write("fo"); write("fum"); } Even better?

Sep 24 2004
parent reply Regan Heath <regan netwin.co.nz> writes:
I tend to agree with you, in fact as the method returns absolutely nothing 
at the moment I cannot see what disadvantage there is to what you propose. 
What else could it return that would be of use? maybe the # of bytes 
written?

On Fri, 24 Sep 2004 08:09:07 -0600, Lynn Allan <l_d_allan adelphia.net> 
wrote:

 But using the "With" statement, you get conflicts with imports and you 
 end
 up with code like:

 #import std.outbuffer;
 #import std.string;
 #import std.stdio;
 #
 #void main()
 #{
 #  int areaCode = 800, lata = 555, lastPart = 1212;
 #  std.outbuffer.OutBuffer myOutBufferFirst = new OutBuffer;
 #  with(myOutBufferFirst) {
 #  with(std.string) {                                      // <-- Ouch
 #    write("Test of building phone number: ");
 #    write('(');
 #    write(toString(areaCode));
 #    write(") ");
 #    write(toString(lata));
 #    write("-");
 #    write(toString(lastPart));
 #  }
 #  }
 #  writefln("myOutBufferFirst: ", myOutBufferFirst.toString());
 // or
 #  std.outbuffer.OutBuffer myOutBufferSecond = new OutBuffer;
 #  with(myOutBufferSecond) {
 #    write("Test of building phone number: ");
 #    write('(');
 #    write(std.string.toString(areaCode));  // <-- Ouch
 #    write(") ");
 #    write(std.string.toString(lata));
 #    write("-");
 #    write(std.string.toString(lastPart));
 #  }
 #  writefln("myOutBufferSecond: ", myOutBufferSecond.toString());
 #}

 "Vathix" <vathixSpamFix dprogramming.com> wrote in message
 news:ci8hkf$n2o$1 digitaldaemon.com...
 "Lynn Allan" <l_d_allan adelphia.net> wrote in message
 news:ci8f8q$l5n$1 digitaldaemon.com...
 <alert comment="newbie">

 If appropriate, I would like to advocate that myOutBuffer.write(arg)

 a reference to itself rather than the current practice of not 

 anything (i.e. void). I believe this would lead to improved syntax and
 possibly encourage wider usage of OutBuffer.

 My impression is that it is more efficient to use a series of
 myOutBuffer.write(arg) calls rather than appending with "~". However,


 resulting code seems more "tedious" and verbose than it has to be


 myOutBuffer.write(arg) doesn't return anything.

 Inefficient but concise:
 writefln(lastName ~ ", " ~ firstName ~ " " ~ middleInitial ~ ".");

 More efficient but tediously verbose:
 myOutBuffer.reserve(100);
 myOutBuffer.write(lastName);
 myOutBuffer.write(", ");
 myOutBuffer.write(firstName);
 myOutBuffer.write(" ");
 myOutBuffer.write(middleInitial);
 myOutBuffer.write(".");
 writefln(myOutBuffer.toString());

 As I recall, C++ and/or Java would allow "cascading / stringing


 of
 the above series of write calls because it returned a 


 itself rather than myOutBuffer.write(arg) returning void. This would


 the following syntax which, to me, is preferred:

 myOutBuffer.write(lastName).write(", ").write(firstName).write("
 ").write(middleInitial).write(".");

 Am I missing or overlooking something? No doubt easier said than done,


 it seems like this change might be straightforward to implement. After

 my impression is that the "~" operator is returning a reference to its
 object to allow the concatenation to happen.

 </alert>

with(myOutBuffer) { reserve(100); write("fe"); write("fi"); write("fo"); write("fum"); } Even better?


-- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Sep 26 2004
parent reply Sean Kelly <sean f4.ca> writes:
In article <opseyewchn5a2sq9 digitalmars.com>, Regan Heath says...
I tend to agree with you, in fact as the method returns absolutely nothing 
at the moment I cannot see what disadvantage there is to what you propose. 
What else could it return that would be of use? maybe the # of bytes 
written?

Yup. That's already the way things work in my stream prototype: http://home.f4.ca/sean/d/stream.d Sean
Sep 26 2004
parent Regan Heath <regan netwin.co.nz> writes:
On Sun, 26 Sep 2004 22:06:22 +0000 (UTC), Sean Kelly <sean f4.ca> wrote:
 In article <opseyewchn5a2sq9 digitalmars.com>, Regan Heath says...
 I tend to agree with you, in fact as the method returns absolutely 
 nothing
 at the moment I cannot see what disadvantage there is to what you 
 propose.
 What else could it return that would be of use? maybe the # of bytes
 written?

Yup. That's already the way things work in my stream prototype: http://home.f4.ca/sean/d/stream.d

Cool. I see you're setting a 'BADBIT' when it fails to write/read the specified length of data, and have a throwOn method for setting what bits cause exceptions. Great idea! It neatly solves the 'when does it make sense to throw an exception' problem by giving the programmer the choice. Regan -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Sep 26 2004