Re: override toString() for a tuple?

2014-06-04 Thread Jonathan M Davis via Digitalmars-d-learn
On Wed, 04 Jun 2014 05:35:18 +
Steve D via Digitalmars-d-learn digitalmars-d-learn@puremagic.com
wrote:

 Is it possible to override std tuple's toString format?

 so that
  auto a = tuple(hello,1,2,3);
  writeln(a);

 prints
  (hello, 1, 2, 3)
 and not
  Tuple!(string, int, int, int)(hello, 1, 2, 3)


 I'm aware I could write a custom formatter function, but it would
 be nice not to
 have to use such a function for every tuple printed by the
 program.
 Overriding toString() one time in program (if possible) would
 give the ideal default behaviour. (I would duplicate the current
 typecons.d toString() and strip off the prefix)

 thanks for any help

toString is a member of Tuple, and there's no way to override that externally.
You could create a wrapper struct for a Tuple whose toString method did what
you want, and you could just create a function which generated the string that
you wanted that you used whenever printing out a Tuple, but there is no way to
globally override Tuple's toString.

The closest that you could do to overriding Tuple's toString in one place
would be to write your own wrappers for whatever printing functions you want
to use, have them detect when they're given a Tuple, and then print them the
way that you want and pass everything else directly on to writeln or whatever
it is you're wrapping. Then, the print functions would take care of it for
you, but writing such a function wouldn't exactly be fun.

If you're really determined to print tuples differently, you _could_ simply
copy std.typecons.Tuple to your own code and alter it to do what you want.

- Jonathan M Davis


Re: override toString() for a tuple?

2014-06-04 Thread Steve D via Digitalmars-d-learn
On Wednesday, 4 June 2014 at 06:04:22 UTC, Jonathan M Davis via 
Digitalmars-d-learn wrote:


toString is a member of Tuple, and there's no way to override 
that externally.

...

Hi Jonathan,

Yeah, I'll probably just keep my locally cobbled version of 
typecons.d in my path.
The other options would be hard going as I've got tuples printed 
from arrays and variant arrays etc as well as individually.
It's just easier to hack the default library code, although not 
so elegant. You would think the promise of OO and Inheritance 
would make it easy and free us from hacks like this ;)  That 
said, it's only a personal project so as long as it works, who 
cares?


Many Thanks for your reply
Steve D



Re: override toString() for a tuple?

2014-06-04 Thread Jonathan M Davis via Digitalmars-d-learn
On Wed, 04 Jun 2014 06:25:53 +
Steve D via Digitalmars-d-learn digitalmars-d-learn@puremagic.com
wrote:

 You would think the promise of OO and Inheritance
 would make it easy and free us from hacks like this ;)

That would require using OO and inheritance, which has nothing to do with
Tuple. ;)

And actually, I find that I very rarely need inheritance. It's definitely the
right solution for some problems, but in the vast majority of cases, I find
that structs are a better solution - especially because they're far more
composable. OO is actually very bad for code reuse, because it's not
particularly composable at all.

- Jonathan M Davis


override toString() for a tuple?

2014-06-03 Thread Steve D via Digitalmars-d-learn

Is it possible to override std tuple's toString format?

so that
auto a = tuple(hello,1,2,3);
writeln(a);

prints
(hello, 1, 2, 3)
and not
Tuple!(string, int, int, int)(hello, 1, 2, 3)


I'm aware I could write a custom formatter function, but it would 
be nice not to
have to use such a function for every tuple printed by the 
program.
Overriding toString() one time in program (if possible) would 
give the ideal default behaviour. (I would duplicate the current 
typecons.d toString() and strip off the prefix)


thanks for any help