假设我们有三个对象:
MainObj {
someProp: false
toggleSomeProp: function () {
if (this.someProp)
this.someProp = false
else
this.someProp = true
}
...
}
FirstObj {
someOtherProp: ...
doSomethingWithOtherProp: function () {...}
...
}
SecondObj {
state: null
setState: function (s) {
this.state = s
}
getState: function() {
return this.state
}
...
}FirstObj和SecondObj从MainObj继承someProp和toggleSomeProp,并使用自己的属性和方法对其进行扩展。SecondObj用状态属性(和get/set方法)扩展MainObj,它可以是任何东西。
假设我们有两个对象FirstObjSrc和SecondObjSrc,它们都有getObj方法。第一个返回FirstObj,第二个返回SecondObj。
这就是我在Purescript中看到的执行情况:
foreign import data ObjEff :: * -> !
foreign import data Obj :: *
foreign import data FirstObjSrc :: *
foreign import data SecondObjSrc :: *
foreign import somePropImpl :: forall a s e. a -> Eff (oe :: ObjEff s | e) Boolean
foreign import toggleSomePropImpl :: forall a s e. a -> Eff (oe :: ObjEff s | e) Unit
foreign import someOtherPropImpl :: ...
foreign import doSomethingWithOtherPropImpl :: ...
foreign import getStateImpl :: forall a b s e. (a -> Maybe a) -> Maybe a -> b -> Eff (oe :: ObjEff s | e) (Maybe s)
foreign import setStateImpl :: forall a s e. a -> s -> Eff (oe :: ObjEff s | e) Unit
foreign import getFirstObjImpl :: forall a s e. FirstObjSrc -> Eff (oe :: ObjEff s | e) a
foreign import getSecondObjImpl :: forall a s e. SecondObjSrc -> Eff (oe :: ObjEff s | e) a
class MainObj a where
someProp :: forall s e. a -> Eff (oe :: ObjEff s | e) Boolean
toggleSomeProp :: forall s e. a -> Eff (oe :: ObjEff s | e) Unit
class FirstObj a where
someOtherProp :: ...
doSomethingWithOtherProp :: ...
class (MainObj a) <= SecondObj a where
getState :: forall s e. a -> Eff (oe :: ObjEff s | e) (Maybe s)
setState :: forall s e. a -> s -> Eff (oe :: ObjEff s | e) Unit
class ObjSrc a where
getObj :: forall b s e. a -> Eff (oe :: ObjEff s | e) b
instance objIsMainObj :: MainObj Obj where
someProp = somePropImpl
toggleSomeProp = toggleSomePropImpl
instance objIsFirstObj :: FirstObj Obj where
someOtherProp = someOtherPropImpl
doSomethingWithOtherProp = doSomethingWithOtherPropImpl
instance objIsSecondObj :: SecondObj Obj where
getState = getStateImpl Just Nothing
setState = setStateImpl
instance firstObjSrcIsObjSrc :: ObjSrc FirstObjSrc where
getObj = getFirstObjImpl
instance secondObjSrcIsObjSrc :: ObjSrc SecondObjSrc where
getObj = getSecondObjImpl
foreign import getFirstObjSrc :: forall s e. Eff (oe :: ObjEff s | e) FirstObjSrc
foreign import getSecondObjSrc :: forall s e. Eff (oe :: ObjEff s | e) SecondObjSrc所以,我有一些关于这个代码的问题:
ObjEff效应是否需要幻影型s?更新
假设上面的代码是某种虚构的浏览器(或NodeJS) API,因此无法以某种方式更改它。
发布于 2019-04-05 00:57:55
这个问题是基于一种旧的、不兼容的语言版本,而不是现在的版本。Eff和effect行已经被删除,取而代之的是Effect (实质上是Eff sans效果行)。我猜想*已经被Type取代了,!也被删除了(当我开始使用PureScript时,这些符号并不存在)。
用于对象的表示法有点混乱,因为它既不是JavaScript,也不是我熟悉的任何其他标准表示法。我的解释是,例如,
MainObj {
someProp: false
toggleSomeProp: function () {
if (this.someProp)
this.someProp = false
else
this.someProp = true
}
...
}意味着这(在JavaScript中)
function MainObj() {
}
MainObj.prototype.someProp = false;
MainObj.prototype.toggleSomeProp = function () {
if (this.someProp)
this.someProp = false
else
this.someProp = true
}
// ...在这种情况下,PureScript中对此的可能定义是:
foreign import data MainObj ∷ Type
foreign import someProp ∷ MainObj → Effect Boolean
foreign import toggleSomeProp ∷ MainObj → Effect Unit实施文件将是:
exports.someProp = function (mainObj) {
return function () {
return mainObj.someProp;
};
};
exports.toggleSomeProp = function (mainObj) {
return function () {
return mainObj.toggleSomeProp();
};
}或者为了更好的内联和更容易的实现,如下所示:
foreign import data MainObj ∷ Type
foreign import someProp ∷ EffectFn1 MainObj Boolean
foreign import toggleSomeProp ∷ EffectFn1 MainObj Unit执行工作将是:
exports.someProp = function (mainObj) {
return mainObj.someProp;
};
exports.toggleSomeProp = function (mainObj) {
return mainObj.toggleSomeProp();
}这有很多种方法可以完成(也可以完成),但是我几乎完全使用这个方法,因为它是直截了当的,显而易见的,而且适应性很强。
someProp必须作为有效的导入,因为给定相同的对象,它可以返回不同的结果。toggleSomeProp必须作为有效的导入,因为它以可观察的方式(通过someProp)变异状态。
https://stackoverflow.com/questions/31359338
复制相似问题