Babas
Babas

Reputation: 395

How to use a specific constructor from a datatype while there are multiple possible type constructor

I have the following data types and I would like to create an instance of Sport but I don't know how to call the type Football from the data type Sport.

`data Football a  = Football
{ players       :: Players a    
, Stadium       :: Stadium  a
}     

data Tennis a  = Tennis
{ players1      :: Players a    
, Stadium1      :: Stadium  a
}
data Sport a = SF  Football a  | ST Tennis a

When I do:

Instance Show a => Show (Sport a) where
show Football{..} = "<== Football ==>"
show Tennis{..} = "<== Tennis ==>"

I get a error message: Couldn't match expected type ‘Sport a’ with actual type ‘Football t6’ I tryed some other ways and check some other example but I could figure out how to do it... does some one have an idea?

Thanks in advance :)

Upvotes: 1

Views: 54

Answers (1)

chi
chi

Reputation: 116174

You need parentheses here

data Sport a = SF (Football a) | ST (Tennis a)

Then you need to pattern match against sports, not other types:

instance Show a => Show (Sport a) where
  show (SF Football{..}) = "<== Football ==>"
  show (ST Tennis{..}) = "<== Tennis ==>"

Upvotes: 4

Related Questions