iproute icon indicating copy to clipboard operation
iproute copied to clipboard

Type-safe from/to helper functions

Open ppetr opened this issue 8 years ago • 2 comments

Currently various from.../to... functions work with just [Int]. I'd suggest to:

  1. Return appropriate data types, that is Word8 for to/fromIPv4 and to/fromIPv6b, and Word16 for to/fromIPv6.
  2. Add variants for from/toIPv6 and from/toIPv4 that take/return 4-tuples and 8-tuples instead of lists. This is safer, and it's still possible to efficiently work with such tuples by wrapping them for example with NTup4 / NTup8 which provide Traversable, Applicative etc.

I'll be happy to contribute, if we agree on this.

ppetr avatar Jul 02 '16 10:07 ppetr

So, do you mean that these function should be polymorphic? Anyway, please show your code to me.

kazu-yamamoto avatar Jul 05 '16 02:07 kazu-yamamoto

My current code for the network package is here: https://github.com/haskell/network/pull/210 (sorry for the confusion before). At the end I thought that it'd be better suited for the network package directly, but perhaps both packages could benefit from such functions.

No, I had no polymorphism on my mind. IPv4 addresses are defined to be 4 8-byte unsigned numbers and IPv6 as 8 16-byte unsigned numbers, so my suggestion is to add functions that convert to precisely such a representation. And to use tuples instead of lists, as conversions from list are partial functions (are well defined only if the argument has the correct length), while conversions from tuples are total functions. Making them polymorphic might make their use easier, but might cause errors if they're used on an inappropriate integral instance, so I'd rather keep them with the exact correct type,.although I'm open to discussion.

ppetr avatar Jul 09 '16 16:07 ppetr