首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >效果和幻影类型

效果和幻影类型
EN

Stack Overflow用户
提问于 2015-07-11 16:33:08
回答 1查看 169关注 0票数 3

假设我们有三个对象:

代码语言:javascript
复制
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
  }
  ...
}

FirstObjSecondObjMainObj继承someProptoggleSomeProp,并使用自己的属性和方法对其进行扩展。SecondObj用状态属性(和get/set方法)扩展MainObj,它可以是任何东西。

假设我们有两个对象FirstObjSrcSecondObjSrc,它们都有getObj方法。第一个返回FirstObj,第二个返回SecondObj

这就是我在Purescript中看到的执行情况:

代码语言:javascript
复制
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

所以,我有一些关于这个代码的问题:

  1. 这个实现是正确的吗?
  2. ObjEff效应是否需要幻影型s
  3. 如果是这样(或者没有),那么我想了解为什么(我在type和其他一些网站上读过一篇解释,我认为我理解基本知识,但效果有点混乱)。

更新

假设上面的代码是某种虚构的浏览器(或NodeJS) API,因此无法以某种方式更改它。

EN

回答 1

Stack Overflow用户

发布于 2019-04-05 00:57:55

这个问题是基于一种旧的、不兼容的语言版本,而不是现在的版本。Eff和effect行已经被删除,取而代之的是Effect (实质上是Eff sans效果行)。我猜想*已经被Type取代了,!也被删除了(当我开始使用PureScript时,这些符号并不存在)。

用于对象的表示法有点混乱,因为它既不是JavaScript,也不是我熟悉的任何其他标准表示法。我的解释是,例如,

代码语言:javascript
复制
MainObj {
  someProp: false
  toggleSomeProp: function () {
    if (this.someProp)
      this.someProp = false
    else
      this.someProp = true
  }
  ...
}

意味着这(在JavaScript中)

代码语言:javascript
复制
function MainObj() {
}
MainObj.prototype.someProp = false;
MainObj.prototype.toggleSomeProp = function () {
  if (this.someProp)
    this.someProp = false
  else
    this.someProp = true
}
// ...

在这种情况下,PureScript中对此的可能定义是:

代码语言:javascript
复制
foreign import data MainObj ∷ Type

foreign import someProp ∷ MainObj → Effect Boolean

foreign import toggleSomeProp ∷ MainObj → Effect Unit

实施文件将是:

代码语言:javascript
复制
exports.someProp = function (mainObj) {
  return function () {
    return mainObj.someProp;
  };
};

exports.toggleSomeProp = function (mainObj) {
  return function () {
    return mainObj.toggleSomeProp();
  };
}

或者为了更好的内联和更容易的实现,如下所示:

代码语言:javascript
复制
foreign import data MainObj ∷ Type

foreign import someProp ∷ EffectFn1 MainObj Boolean

foreign import toggleSomeProp ∷ EffectFn1 MainObj Unit

执行工作将是:

代码语言:javascript
复制
exports.someProp = function (mainObj) {
  return mainObj.someProp;
};

exports.toggleSomeProp = function (mainObj) {
  return mainObj.toggleSomeProp();
}

这有很多种方法可以完成(也可以完成),但是我几乎完全使用这个方法,因为它是直截了当的,显而易见的,而且适应性很强。

someProp必须作为有效的导入,因为给定相同的对象,它可以返回不同的结果。toggleSomeProp必须作为有效的导入,因为它以可观察的方式(通过someProp)变异状态。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31359338

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档