Showing a value, when there are many approaches?

Hi, I'm writing a module that contains a data type for a MacAddress. Given that, I can do things like validate and format. But there are several ways you can display a MacAddress: 'aabbccddeeff' or '00:11:22:33:AA:BB' or 'aa:bb:cc:00:11:22'. Different external systems want the mac addresses in different formats, and I'd like to centralize that logic. ** So the design question: I can't just use a `show` function directly to do this, since there are options I need to pass. I could use separate functions that return a string. I could use newtype to wrap each display style of mac address in a type that has a specific `show` instance that spits out the right format. So a `Pretty` and `Lower` and similar types. --- Is there a best-practice here? Am I totally missing a better way to model this idea?

Show is not meant to necessarily be human readable. It is meant to be the exact opposite of Read. You show it, then you read it back in and it is unambiguous. Having different ways to show it kind of defeats the purpose of show. Most people just make a custom function that takes a macaddress and some other parameter to determine which format you want it to be output as and then just use that. On Wed, Aug 7, 2013 at 1:04 PM, Chris Schneider < chris@christopher-schneider.com> wrote:
Hi, I'm writing a module that contains a data type for a MacAddress.
Given that, I can do things like validate and format.
But there are several ways you can display a MacAddress:
'aabbccddeeff' or '00:11:22:33:AA:BB' or 'aa:bb:cc:00:11:22'.
Different external systems want the mac addresses in different formats, and I'd like to centralize that logic.
** So the design question:
I can't just use a `show` function directly to do this, since there are options I need to pass.
I could use separate functions that return a string.
I could use newtype to wrap each display style of mac address in a type that has a specific `show` instance that spits out the right format. So a `Pretty` and `Lower` and similar types.
---
Is there a best-practice here? Am I totally missing a better way to model this idea?
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On Wed, Aug 7, 2013 at 1:04 PM, Chris Schneider < chris@christopher-schneider.com> wrote:
I can't just use a `show` function directly to do this, since there are options I need to pass.
And you shouldn't use `show` for this, because if it prints your MacAddress as anything other than an unambiguous representation of the Haskell MacAddress type then it has lost information useful for debugging. More to the point: if I cannot see the difference between your MacAddress and a String, then it's an unacceptable `show`. `show` should reflect the Haskell data structure so I can verify that my program is doing the right thing; if it shows what looks like a String, but I'm expecting to see a MacAddress, then I will suspect a type error somewhere. `show` is like Perl's Data::Dumper or Python's repr. I don't want pretty, I want accuracy. -- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net

Ok, that makes sense, I misinterpreted `show` since for integers & friends
it is just the same as a `toString` in other languages.
Is there a standard name for "render a human readable form of this data"?
toString() in java, to_s in ruby, and similar?
On Wed, Aug 7, 2013 at 12:51 PM, Brandon Allbery
On Wed, Aug 7, 2013 at 1:04 PM, Chris Schneider < chris@christopher-schneider.com> wrote:
I can't just use a `show` function directly to do this, since there are options I need to pass.
And you shouldn't use `show` for this, because if it prints your MacAddress as anything other than an unambiguous representation of the Haskell MacAddress type then it has lost information useful for debugging. More to the point: if I cannot see the difference between your MacAddress and a String, then it's an unacceptable `show`. `show` should reflect the Haskell data structure so I can verify that my program is doing the right thing; if it shows what looks like a String, but I'm expecting to see a MacAddress, then I will suspect a type error somewhere.
`show` is like Perl's Data::Dumper or Python's repr. I don't want pretty, I want accuracy.
-- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
participants (3)
-
Brandon Allbery
-
Chris Schneider
-
David McBride