module Webserver.Control.Nil where ---------------------------------------------------------------------------------------------------- import qualified Data.Aeson as Aeson import qualified Data.ByteString as Strict.ByteString import qualified Data.ByteString.Lazy as Lazy.ByteString import qualified Data.HashMap.Lazy as HashMap import qualified Data.HashSet as HashSet import Data.Int (Int16, Int32, Int64) import qualified Data.Map.Strict as Strict.Map import Data.Monoid (mempty) import qualified Data.Text as Strict.Text import qualified Data.Text.Lazy as Lazy.Text import Data.Time.Calendar (Day) import qualified Data.Time.Calendar.OrdinalDate as Ordinal import Data.Time.Clock (DiffTime, NominalDiffTime, UTCTime (..)) import Data.Time.LocalTime (LocalTime (..), TimeOfDay, ZonedTime (..), midnight, utc) import qualified Data.UUID as UUID import Data.Word (Word, Word16, Word32, Word64, Word8) import GHC.Integer (Integer) ---------------------------------------------------------------------------------------------------- {- OOP uses the concept of "falsey" object. Empty strings, Empty lists, 0 numerics, etc. Complex types/objects kind-of also can have this representation, but they're more based on the falsey-ness of its parts. Consider this record: ```haskell data SomeRecord = SomeRecord { aString :: String , anInteger :: Integer , aListOfUnits :: List () , aMaybeThing :: Maybe Thing } ``` To exhibit the falsey behavior we mentioned, each field part needs to be falsey ```haskell someFalseyRecord :: SomeRecord someFalseyRecord = SomeRecord { aString = "" , anInteger = 0 , aListOfUnits = [] , aMaybeThing = Nothing } ``` So that's the idea behind this typeclass, but rather than use the "falsey" name we call it a Nil record/type. -} class Nil a where nil :: a default nil :: (Num a) => a nil = 0 instance Nil Char where nil = '\0' instance Nil Bool where nil = False instance Nil Int instance Nil Int16 instance Nil Int32 instance Nil Int64 instance Nil Word instance Nil Word8 instance Nil Word16 instance Nil Word32 instance Nil Word64 instance Nil Integer instance Nil Double instance Nil Float instance Nil (Maybe a) where nil = Nothing instance Nil (List a) where nil = [] instance Nil Strict.Text.Text where nil = Strict.Text.empty instance Nil Lazy.Text.Text where nil = Lazy.Text.empty instance Nil Strict.ByteString.ByteString where nil = Strict.ByteString.empty instance Nil Lazy.ByteString.ByteString where nil = Lazy.ByteString.empty instance Nil (Strict.Map.Map k v) where nil = Strict.Map.empty instance Nil (HashMap k v) where nil = HashMap.empty instance Nil (HashSet a) where nil = HashSet.empty instance Nil Aeson.Value where nil = Aeson.Object mempty instance Nil UUID where nil = UUID.nil instance Nil DiffTime instance Nil NominalDiffTime instance Nil UTCTime where nil = UTCTime nil nil instance Nil Day where nil = Ordinal.fromOrdinalDate 1970 1 instance Nil TimeOfDay where nil = midnight instance Nil LocalTime where nil = LocalTime nil nil instance Nil ZonedTime where nil = ZonedTime nil utc