-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#45] Add newtype Id with phantom type parameter
Resolves #45
- Loading branch information
1 parent
3b481ba
commit 20f4cce
Showing
5 changed files
with
46 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,7 +70,9 @@ library | |
IW.Config | ||
|
||
-- Core Modules | ||
IW.Core.Id | ||
IW.Core.Issue | ||
IW.Core.Repo | ||
|
||
-- Database | ||
IW.Db | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{-# OPTIONS_GHC -fno-warn-redundant-constraints #-} | ||
|
||
{-# LANGUAGE AllowAmbiguousTypes #-} | ||
|
||
module IW.Core.Id | ||
( Id (..) | ||
, AnyId | ||
, castId | ||
) where | ||
|
||
import Data.Type.Equality (type (==)) | ||
|
||
|
||
-- | Wrapper for integer id. Contains phantom type parameter for increased | ||
-- type-safety. | ||
newtype Id a = Id { unId :: Int } | ||
deriving stock (Show, Generic) | ||
deriving newtype (Eq, Ord, FromField, ToField, FromJSON, ToJSON) | ||
|
||
-- | When we don't care about type of 'Id' but don't want to deal with type variables. | ||
type AnyId = Id () | ||
|
||
-- | Unsafe cast of 'Id'. Implementation uses smart trick to enforce usage | ||
-- always with @TypeApplications@. | ||
castId :: forall to from to' . ((to == to') ~ 'True) => Id from -> Id to' | ||
castId (Id a) = Id a |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{-# LANGUAGE DeriveAnyClass #-} | ||
|
||
module IW.Core.Repo | ||
( Repo (..) | ||
) where | ||
|
||
import IW.Core.Id (Id) | ||
|
||
|
||
-- | Data type representing a GitHub repository. | ||
data Repo = Repo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters