3 つのオブジェクトがあるとします。
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
およびsomeProp
拡張toggleSomeProp
します。状態プロパティ (および get/set メソッド) で拡張され、何でもかまいません。MainObj
SecondObj
MainObj
また、2 つのオブジェクトがFirstObjSrc
ありSecondObjSrc
、どちらにもgetObj
メソッドがあるとします。最初の 1 つは返さFirstObj
れ、2 番目の 1 つは返されます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
ですか?- そうであれば(またはそうでない場合)、その理由を理解したいと思います(https://wiki.haskell.org/Phantom_typeと他のいくつかの説明を読みましたが、基本を理解していると思います。しかし、効果は私を少し混乱させます)。
アップデート
上記のコードは架空のブラウザー (または NodeJS) API の一種であり、何らかの形で変更する方法はないとしましょう。