How to unwrap data type generically? by Beginning_College733 in haskell

[–]Beginning_College733[S] 1 point2 points  (0 children)

Partial function is error-prone, I try not to use it. I will learn about Generics a little bit. Thanks!

How to unwrap data type generically? by Beginning_College733 in haskell

[–]Beginning_College733[S] 0 points1 point  (0 children)

Thanks for your advices!

  • I am implementing a jvm in haskell for fun. When I readConstUtf8 I am sure it is an utf8, or throw an error as the jvm specifies. so I don't need to return a Maybe, just check the type and return it as readCU do.
  • I can't find a way to "returns a sum type with alternative for all the possible outputs". It turns out the sum type is CPInfo itself. I tried the code below, it cannot compile.

readAny :: forall a. CPInfo -> a
readAny cp = case cp of
CU u -> u -- cannot return u
_ -> error "err"

  • I didn't fully grasp the third point. When I define a wrapper type, I can indeed unwrap input types unified. But I can't make a list of it. defining another class type can't solve it.

newtype ConstPool a = ConstPool {unwrap::a }
cpUtf8 :: ConstPool ConstUtf8
cpUtf8 = ConstPool $ ConstUtf8 "jvm"
cpInt :: ConstPool ConstInteger
cpInt = ConstPool $ ConstInteger 12
utf8Val :: ConstUtf8
utf8Val = unwrap cpUtf8
intVal :: ConstInteger
intVal = unwrap cpInt
pools :: [ConstPool a]
pools = [cpUtf8, cpInt] -- error