[Git][ghc/ghc][master] Clarification for newtype constructors when using `coerce`

Marge Bot (@marge-bot) gitlab at gitlab.haskell.org
Thu Jan 25 05:28:49 UTC 2024



Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC


Commits:
699da01b by Hécate Moonlight at 2024-01-25T00:28:27-05:00
Clarification for newtype constructors when using `coerce`

- - - - -


1 changed file:

- compiler/GHC/Builtin/primops.txt.pp


Changes:

=====================================
compiler/GHC/Builtin/primops.txt.pp
=====================================
@@ -3955,10 +3955,25 @@ pseudoop   "coerce"
      more complicated settings, e.g. converting a list of newtypes to a list of
      concrete types.
 
+     When used in conversions involving a newtype wrapper,
+     make sure the newtype constructor is in scope.
+
      This function is representation-polymorphic, but the
      'RuntimeRep' type argument is marked as 'Inferred', meaning
      that it is not available for visible type application. This means
      the typechecker will accept @'coerce' \@'Int' \@Age 42 at .
+
+     === __Examples__
+
+     >>> newtype TTL = TTL Int deriving (Eq, Ord, Show)
+     >>> newtype Age = Age Int deriving (Eq, Ord, Show)
+     >>> coerce (Age 42) :: TTL
+     TTL 42
+     >>> coerce (+ (1 :: Int)) (Age 42) :: TTL
+     TTL 43
+     >>> coerce (map (+ (1 :: Int))) [Age 42, Age 24] :: [TTL]
+     [TTL 43,TTL 25]
+
    }
 
 ------------------------------------------------------------------------



View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/699da01bbbf3e42c7d38b9cfe443dd0e8e256342

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/699da01bbbf3e42c7d38b9cfe443dd0e8e256342
You're receiving this email because of your account on gitlab.haskell.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-commits/attachments/20240125/2e29236b/attachment-0001.html>


More information about the ghc-commits mailing list