给定以下代码:
{-# LANGUAGE OverloadedStrings #-}
newtype Firstname = Firstname String deriving (Eq, Show)
instance IsString Firstname where fromString = Firstname
newtype Lastname = Lastname String deriving (Eq, Show)
instance IsString Lastname where fromString = Lastname
data Person = Person { firstname :: Firstname, lastname :: Lastname, age :: Int } deriving Show
我想删除围绕创建强类型字符串的样板。是否可以使用 Template Haskell(或其他方式)来实现这一点?
例如:
{-# LANGUAGE OverloadedStrings, TemplateHaskell #-}
$(strongString ''Firstname)
$(strongString ''Lastname)
data Person = Person { firstname :: Firstname, lastname :: Lastname, age :: Int } deriving Show