You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently it is way too easy to mix up index with size, since they are of the same type. For example function isSafeIndex :: ix -> ix -> Bool does not give any type level guarantees that first argument is size while the second one is index, which can lead to nasty bugs.
This ticket proposes a newtype wrapper:
newtypeSzix=Szix
Which will not have any runtime overhead but will promote library safety.
This change will affect many functions in the library, for example core function makeArray will now have a type signature:
but the change, IMHO, totally worth it. Especially since transitioning current code is trivial.
Together with the wrapper should be added helper pattern synonyms:
pattern Sz1 :: Int -> Sz Ix1
pattern Sz1 i = Sz i
pattern Sz2 :: Int -> Int -> Sz Ix2
pattern Sz2 m n = Sz (m :. n)
...
-- so forth until Sz5
Bonus.
It would be possible to prevent negative size to be constructed altogether similar to how the Stride is currently implemented. Unlikely that it will have any overhead, but the drawback is that on ghc < 8.2 pattern matching will be yielding an non-exhaustive warning. That is still up for a debate though. Moreover it can be added in the future, since it would be backwards compatibe.
The text was updated successfully, but these errors were encountered:
Currently it is way too easy to mix up index with size, since they are of the same type. For example function
isSafeIndex :: ix -> ix -> Bool
does not give any type level guarantees that first argument is size while the second one is index, which can lead to nasty bugs.This ticket proposes a newtype wrapper:
Which will not have any runtime overhead but will promote library safety.
This change will affect many functions in the library, for example core function
makeArray
will now have a type signature:but the change, IMHO, totally worth it. Especially since transitioning current code is trivial.
Together with the wrapper should be added helper pattern synonyms:
Bonus.
It would be possible to prevent negative size to be constructed altogether similar to how the
Stride
is currently implemented. Unlikely that it will have any overhead, but the drawback is that on ghc < 8.2 pattern matching will be yielding annon-exhaustive
warning. That is still up for a debate though. Moreover it can be added in the future, since it would be backwards compatibe.The text was updated successfully, but these errors were encountered: