kazu-yamamoto / iproute

IP Routing Table in Haskell
http://www.mew.org/~kazu/proj/iproute/
BSD 3-Clause "New" or "Revised" License
47 stars 28 forks source link

Type-safe from/to helper functions #31

Open ppetr opened 8 years ago

ppetr commented 8 years ago

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.

kazu-yamamoto commented 8 years ago

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

ppetr commented 8 years ago

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.