License | MIT |
---|---|
Maintainer | mmzk1526@outlook.com |
Portability | GHC |
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
Similar to Data.TypeID, but the type is statically determined in the type level.
When using TypeID, if we want to check if the type matches, we usually need
to get the prefix of the TypeID and compare it with the desired prefix at
runtime. However, with Haskell's type system, we can do this at compile time
instead. We call this TypeID with compile-time prefix a KindID
.
Of course, that would require the desired prefix to be known at compile time. This is actually quite common, especially when we are using one prefix for one table in the database.
For example, suppose we have a function that takes a KindID
with the prefix
"user", it may have a signature like this:
f :: KindID "user" -> IO ()
Then if we try to pass in a KindID
with the prefix "post", the compiler
will complain, thus removing the runtime check and the associated overhead.
All the prefixes are type-checked at compile time, so if we try to pass in invalid prefixes, the compiler (again) will complain.
This module contains functions to generate and parse these type-level TypeIDs as well as conversion functions to and from the usual term-level TypeIDs. These functions are usually used with a type application, e.g.
do tid <- genKindID @"user" ...
Synopsis
- data KindID (prefix :: Symbol)
- getPrefix :: forall prefix. ValidPrefix prefix => KindID prefix -> Text
- getUUID :: forall prefix. ValidPrefix prefix => KindID prefix -> UUID
- getTime :: forall prefix. ValidPrefix prefix => KindID prefix -> Word64
- type ValidPrefix (prefix :: Symbol) = (KnownSymbol prefix, LengthSymbol prefix < 64, IsLowerSymbol prefix ~ 'True)
- genKindID :: forall prefix. ValidPrefix prefix => IO (KindID prefix)
- genKindIDs :: forall prefix. ValidPrefix prefix => Word16 -> IO [KindID prefix]
- nil :: KindID ""
- decorate :: forall prefix. ValidPrefix prefix => UUID -> KindID prefix
- toString :: forall prefix. ValidPrefix prefix => KindID prefix -> String
- toText :: forall prefix. ValidPrefix prefix => KindID prefix -> Text
- toByteString :: forall prefix. ValidPrefix prefix => KindID prefix -> ByteString
- parseString :: forall prefix. ValidPrefix prefix => String -> Either TypeIDError (KindID prefix)
- parseText :: forall prefix. ValidPrefix prefix => Text -> Either TypeIDError (KindID prefix)
- parseByteString :: forall prefix. ValidPrefix prefix => ByteString -> Either TypeIDError (KindID prefix)
- toTypeID :: forall prefix. ValidPrefix prefix => KindID prefix -> TypeID
- fromTypeID :: forall prefix. ValidPrefix prefix => TypeID -> Maybe (KindID prefix)
Data types
data KindID (prefix :: Symbol) Source #
A TypeID with the prefix encoded at type level.
It is dubbed KindID
because we the prefix here is a data kind rather than
a type.
Note that the Show
instance is for debugging purposes only. To pretty-print
a KindID
, use toString
, toText
or toByteString
.
Instances
Show (KindID prefix) Source # | |
Eq (KindID prefix) Source # | |
Ord (KindID prefix) Source # | |
Defined in Data.KindID.Internal compare :: KindID prefix -> KindID prefix -> Ordering # (<) :: KindID prefix -> KindID prefix -> Bool # (<=) :: KindID prefix -> KindID prefix -> Bool # (>) :: KindID prefix -> KindID prefix -> Bool # (>=) :: KindID prefix -> KindID prefix -> Bool # | |
ValidPrefix prefix => FromJSON (KindID prefix) Source # | |
ValidPrefix prefix => ToJSON (KindID prefix) Source # | |
Defined in Data.KindID |
getPrefix :: forall prefix. ValidPrefix prefix => KindID prefix -> Text Source #
Get the prefix of the KindID
.
getTime :: forall prefix. ValidPrefix prefix => KindID prefix -> Word64 Source #
Get the timestamp of the KindID
.
type ValidPrefix (prefix :: Symbol) = (KnownSymbol prefix, LengthSymbol prefix < 64, IsLowerSymbol prefix ~ 'True) Source #
A constraint for valid prefix Symbol
s.
KindID generation
genKindID :: forall prefix. ValidPrefix prefix => IO (KindID prefix) Source #
Generate a new KindID
from a prefix.
It throws a TypeIDError
if the prefix does not match the specification,
namely if it's longer than 63 characters or if it contains characters other
than lowercase latin letters.
genKindIDs :: forall prefix. ValidPrefix prefix => Word16 -> IO [KindID prefix] Source #
Encoding & decoding
toString :: forall prefix. ValidPrefix prefix => KindID prefix -> String Source #
Pretty-print a KindID
.
toByteString :: forall prefix. ValidPrefix prefix => KindID prefix -> ByteString Source #
Pretty-print a KindID
to lazy ByteString
.
parseString :: forall prefix. ValidPrefix prefix => String -> Either TypeIDError (KindID prefix) Source #
parseText :: forall prefix. ValidPrefix prefix => Text -> Either TypeIDError (KindID prefix) Source #
parseByteString :: forall prefix. ValidPrefix prefix => ByteString -> Either TypeIDError (KindID prefix) Source #
Parse a KindID
from its string representation as a lazy ByteString
.
Type-level & term-level conversion
fromTypeID :: forall prefix. ValidPrefix prefix => TypeID -> Maybe (KindID prefix) Source #
Orphan instances
ValidPrefix prefix => FromJSON (KindID prefix) Source # | |
ValidPrefix prefix => ToJSON (KindID prefix) Source # | |