Skip Navigation
Defeating Return Type Polymorphism
  • Well, I guess that I can see the value.. Leaving copy-pasting problem aside, you might, for instance, want to have a type for a message with moderately complex envelope and a wide variety of possible payload types. It would be useful to have functions that act on the envelope, and treat payload as something opaque.

    Thanks for the conversation!

  • Defeating Return Type Polymorphism
  • But how? Parsing function can return any of the types, we don't know what was in the bytestring. So we'd need to deal with all variations in any case, no?

    Is the difference in that it becomes possible to pattern-match on a type of an element inside the structure, rather than on the structure as a whole? So as long as you don't need that element, you can access elements that are common without pattern-matching? I guess it's a marginal benefit...

    Or do I still misunderstand?

  • Defeating Return Type Polymorphism
  • This is probably a stupid question, but what does this trickery with GADTs and Existential Types give compared to the "normal" way?

    import Data.Int (Int8, Int16)
    import Data.ByteString (ByteString)
    
    data ParsedImage
      = ParsedAs8 (Int, Int, [Int8])
      | ParsedAs16 (Int, Int, [Int16])
      deriving Show
    
    parseImage :: ByteString -> ParsedImage
    parseImage = undefined
    
    imageDimensions :: ByteString -> (Int, Int)
    imageDimensions str =
      case parseImage str of
        ParsedAs8 (w, h, _) -> (w, h)
        ParsedAs16 (w, h, _) -> (w, h)
    
    

    Basically, the same way as a function can return an Either and everyone is happy to pattern-match on the result.

  • InitialsDiceBearhttps://github.com/dicebear/dicebearhttps://creativecommons.org/publicdomain/zero/1.0/„Initials” (https://github.com/dicebear/dicebear) by „DiceBear”, licensed under „CC0 1.0” (https://creativecommons.org/publicdomain/zero/1.0/)LU
    lukewarm @kbin.social
    Posts 0
    Comments 4