Purescript 我如何处理某些新类型的分组
我正在努力让下面的内容发挥作用,但我似乎做不好。我希望这样的事情能够奏效:Purescript 我如何处理某些新类型的分组,purescript,Purescript,我正在努力让下面的内容发挥作用,但我似乎做不好。我希望这样的事情能够奏效: newtype Skill = Skill { name :: String, other :: Int } newtype Feature = Feature { name :: String, something :: Boolean } newtype Special = Special { name :: String, different :: String } type Namable = forall r
newtype Skill = Skill { name :: String, other :: Int }
newtype Feature = Feature { name :: String, something :: Boolean }
newtype Special = Special { name :: String, different :: String }
type Namable = forall r. { name :: String | r }
changeName :: forall m. String -> m Namable -> m Namable
changeName newName (ctor namable) =
ctor $ namable { name = newName }
该错误类似于:
Error 1 of 1
Unable to parse module:
unexpected "namable"
expecting @, ::, operator or )
基本上,我想向一组newtype
s添加功能。这并不总是整个集合,也不总是不止一个集合,但我希望编写实际的功能“忽略”我定义的类型。如果将来我决定添加一个newtype统计信息
,我希望它“正常工作”
为了改进这个问题,我想知道我要找的签名类型是否有名称
f :: forall m a. (a -> a) -> m a -> m a
似乎是一个不太通用的定义。似乎我需要的是
Newtype
typeclass:
newtype Skill = Skill { name :: String, other :: Int }
derive instance newtypeSkill :: Newtype Skill _
newtype Feature = Feature { name :: String, something :: Boolean }
derive instance newtypeFeature :: Newtype Feature _
newtype Special = Special { name :: String, different :: String }
derive instance newtypeSpecial :: Newtype Special _
type Namable = forall r. { name :: String | r }
changeModel ::forall p m. Newtype m p => (p -> p) -> m -> m
changeModel f m = over wrap f m
changeName :: forall m. Newtype m Namable => String -> m -> m
changeName newName m = changeModel (\x -> x { name = newName }) m
fishing :: Skill
fishing = Skill { name : "Fishing", other : 2 }
skating = changeName "Skating" fishing
changeModel
函数现在完全通用于Newtype
类型<代码>更改名称现在与函数应用程序一样简单
详情:
因为我在使用(也可以看一下),我免费得到了over
和wrap
之类的东西
该函数很难理解,因为示例是以无点风格编写的
函数返回类型构造函数
lambda函数是“您希望对新类型的数据执行的操作”
最后的m
是您传入的实际值。这是展开并传入lambda的值