Note that there are some explanatory texts on larger screens.

plurals
  1. POHaskell Refactoring Advice
    primarykey
    data
    text
    <p>I am looking for some refactoring / Best practice advice on the following code. I would like to try to avoid the extensions while maintaining separate modules for different "versions" that are mutually exclusive. My current solution is to use a class and use existential quantification to create a common type for each of the <code>CountrySets</code>.</p> <p>This is an easy thing for me to accomplish if I was using OOP, but I can't seam to think "functional" yet.</p> <p>Thanks for your time.</p> <p><strong>Province.hs</strong></p> <pre><code>{-# LANGUAGE ExistentialQuantification, RankNTypes #-} module Province where class Country a where --some stuff that is not important data Power = forall a. (Show a, Eq a, Country a) =&gt; Power a | Netural | Water data Unit = Unit { power :: forall a. (Show a, Eq a, Country a) =&gt; a, piece :: Piece data Piece = Tank | Plane data Province = Province { power :: Power, provName :: String } deriving (Eq) instance Show Power where show (Power b) = "Power " ++ show b show (Netural) = "Netural" show (Water) = "Water" instance Eq Power where (==) a b = Prelude.show a == Prelude.show b </code></pre> <p><strong>Version1.hs</strong></p> <pre><code>import Province data CountrySet1 = Country11 | Country12 deriving (Eq, Show) instance Country CountrySet1 where provs = one1:one2:[] one1 = Province (Power Country11) "Place11" one2 = Province (Power Country12) "Place12" </code></pre> <p><strong>Version2.hs</strong></p> <pre><code>import Province data CountrySet2 = Country21 | Country22 deriving (Eq, Show) instance Country CountrySet2 where provs = two1:two2:[] two1 = Province (Power Country11) "Place21" two2 = Province (Power Country12) "Place22" </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload