Functions to Polymorphic data types

后端 未结 3 1856
青春惊慌失措
青春惊慌失措 2021-01-13 12:37

data Foo a is defined like:

data Foo a where
  Foo :: (Typeable a, Show a) => a -> Foo a
  -- perhaps more constructors

instance Show a =         


        
3条回答
  •  生来不讨喜
    2021-01-13 13:09

    getFoo :: (Show a, Typeable a) => String -> Foo a
    getFoo "five" = fiveFoo
    getFoo "false" = falseFoo
    

    If fiveFoo :: Foo Int and falseFoo :: Foo Bool, you're essentially asking for getFoo to return a different type depending on what value you feed it at run-time. You can't do that. In Haskell, all types must be known at compile-time.

    If all you want to be able to do is convert the thing to a string, why not just store it as a string in the first place? I'm guessing the answer is that this is actually a simplification of the real problem you're trying to solve...(?)

提交回复
热议问题