I need to implement a dynamic dispatch, I've used Existential types based on this page and produced the following code :
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE ExistentialQuantification #-}
module Cqrs.Command where
import Data.UUID
import Data.Time
import Data.Aeson
import Cqrs.Core
import Data.Text
import GHC.Generics
type CommandName = String
class (FromJSON command,ToJSON command , Show command) => Command_ command where
getCommandId :: command -> CommandId
getAggregateId :: command -> AggregateId
getCommandName :: command -> String
data Command = forall command . Command_ command => Command command
getMyCommandName :: Command -> String
getMyCommandName command = getCommandName command
I'm not able to use functions from the typeclass Command_ on a Command datatype, the compiler complains that way :
/Users/xxx/dev/gsdFlow/src/Cqrs/Command.hs:26:28: error:
• No instance for (Command_ Command)
arising from a use of ‘getCommandName’
• In the expression: getCommandName command
In an equation for ‘getMyCommandName’:
getMyCommandName command = getCommandName command
|
26 | getMyCommandName command = getCommandName command
| ^^^^^^^^^^^^^^^^^^^^^^