Algebra data type extension

Note: if this question is somehow strange, it is because I have only recently been exposed by Haskell, and I am still adapting to functional thinking.

Given a data type, for example Maybe:

data MyOwnMaybe a = MyOwnNothing | MyOwnJust a

everyone using my data type will write functions like

maybeToList :: MyOwnMaybe a -> [a]
maybeToList MyOwnNothing  = []
maybeToList (MyOwnJust x) = [x]

Now suppose that at a later time I want to extend this data type

data MyOwnMaybe a = MyOwnNothing | MyOwnJust a | SuperpositionOfNothingAndJust a

How can I make sure that all functions will be broken at compile time?

Of course, there is a chance that somehow I do not "get" algebraic data types, and perhaps I should not do this at all, but given the data type Action

data Action = Reset | Send | Remove

, Action Add ( , Action)

+4
3

, : . .

-; . .

, : , .

, . , , .

, . , ; ; , .

, , ,

myOwnNothing :: MyOwnMaybe a
myOwnJust :: a -> MyOwnMaybe a

fromMyOwnMaybe :: MyOwnMaybe a -> b -> (a -> b) -> b
fromMyOwnMaybe MyOwnNothing b _ = b
fromMyOwnMaybe (MyOwnJust a) _ f = f a

, MyOwnMaybe; , .

, , - , . , Bool ( ) : True False, FileNotFound - ( ). Maybe [].

: , .

+3

, , , GHC -W -fwarn-incomplete-patterns.

, SO-:

Haskell, ?

, ADT :

data Alphabet = A | B | C | ... | X | Y | Z

isVowel :: Alphabet -> Bool
isVowel A = True
isVowel E = True
isVowel I = True
isVowel O = True
isVowel U = True
isVowel _ = False

, 21 .

, Alphabet, isVowel ""?

+3

, , , . , ( " " ). ADT , , .

+1
source

All Articles