Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {-# LANGUAGE DataKinds #-}
- {-# LANGUAGE KindSignatures #-}
- {-# LANGUAGE GADTs #-}
- module Main where
- data UserType
- = VirtualUser
- | RealUser
- data User (userType :: UserType) where
- MkVirtualUser :: String -> User 'VirtualUser
- MkRealUser :: String -> User 'RealUser
- deliverPackage :: User 'RealUser -> String -> IO ()
- deliverPackage (MkRealUser name) package =
- putStrLn $ "Delivering package " ++ package ++ " for user " ++ name
- sendEmail :: User 'VirtualUser -> String -> IO ()
- sendEmail (MkVirtualUser email) body =
- putStrLn $ "Sending email to " ++ email ++ " with body: \n" ++ body
- userData :: User userType -> IO ()
- userData (MkRealUser name) = putStrLn $ "User with name: " ++ name
- userData (MkVirtualUser email) = putStrLn $ "User with email: " ++ email
- main :: IO ()
- main = do
- let vuser = MkVirtualUser "romanandreg@gmail.com"
- ruser = MkRealUser "Roman Gonzalez"
- -- Compiles with all types of users
- userData vuser
- userData ruser
- --------------------------------------------------------------------------------
- -- Compiles only with types of user @'RealUser@
- deliverPackage ruser "foobar"
- {-
- deliverPackage vuser "barfoo" -- <- Compile Error
- -- Couldn't match type ‘'VirtualUser’ with ‘'RealUser’
- -- Expected type: User 'RealUser
- -- Actual type: User 'VirtualUser
- -- In the first argument of ‘deliverPackage’, namely ‘vuser’
- -- In a stmt of a 'do' block: deliverPackage vuser "barfoo"
- -}
- --------------------------------------------------------------------------------
- -- Compiles only with types of user @'VirtualUser@
- sendEmail vuser "romanandreg@gmail.com"
- {-
- sendEmail ruser "other@mail.com" -- <- Compile Error
- -- Couldn't match type ‘'RealUser’ with ‘'VirtualUser’
- -- Expected type: User 'VirtualUser
- -- Actual type: User 'RealUser
- -- In the first argument of ‘sendEmail’, namely ‘ruser’
- -- In a stmt of a 'do' block: sendEmail ruser "other@mail.com"
- -}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement