Implicit parameters: Difference between revisions
m (Fix link to GHC doc) |
(fix link to ghc user guide) |
||
(4 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
{{GHCUsersGuide|exts/implicit_parameters||an Implicit Parameters section}} | |||
Working example: | Working example: | ||
Line 7: | Line 7: | ||
import Data.List (sortBy) | import Data.List (sortBy) | ||
sortBy' :: (?cmp :: a -> a -> Ordering) => [a] -> [a] | |||
sort | sortBy' = sortBy ?cmp | ||
sort :: Ord a => [a] -> [a] | |||
sort = let ?cmp = compare in sortBy' | |||
main = | main = putStrLn (show (sort [3,1,2])) | ||
[[Category:Language extensions]] | [[Category:Language extensions]] | ||
[[Category:GHC]] | [[Category:GHC]] | ||
[[Category:Stub articles]] | [[Category:Stub articles]] |
Latest revision as of 22:55, 12 June 2021
The GHC Users Guide has an Implicit Parameters section.
Working example:
{-# LANGUAGE ImplicitParams #-} import Data.List (sortBy) sortBy' :: (?cmp :: a -> a -> Ordering) => [a] -> [a] sortBy' = sortBy ?cmp
sort :: Ord a => [a] -> [a] sort = let ?cmp = compare in sortBy' main = putStrLn (show (sort [3,1,2]))