èªååãã®ã¾ã¨ãã§ãã
Intersection(&)
RBSã«ã¯Intersectionåãããã¾ããObject & _Each[Integer]
ã®ããã«&
ã§ç¹ãã¦ããåã®ãã¨ã§ãã
ãã®åã«Union(|)ã¨ã¯
String | Symbol
ã®ããã«ã|
ã§ç¹ãã åãUnionåã¨è¨ãã¾ãããã¡ãã¯ç解ã¯ç°¡åã§ãã
ãString ããã㯠Symbol ã®ã©ã¡ããä¸æ¹ãã¨ãã解éã«ãªãã¾ãã使ç¨é »åº¦ã¯å¤ãã§ãã
Intersectionã®ä½¿ãæ¹
ããããµã¾ãã¦Object & _Each[Integer]
ã¯ä½ãæå³ãã¦ããã®ã§ããããï¼
ãObject
ãã¤_Each
ã¤ã³ã¿ã¼ãã§ã¼ã¹ãæã¤ã£ã¦ã©ããããã¨ï¼Object
ã«each
ã¡ã½ããã¯ãªããï¼ï¼ï¼ã
ã¨ããã®ããç§ãæåã«Intersectionåã«æ±ããææ³ã§ããã
åã«ã¤ãã¦å¦ãã§ãããã¡ã«ãä½ã¨ãªãè¨èªåã§ãããã¿ã¼ã³ããã£ãã®ã§æ¸ãæ®ãã¦ããã¾ãã
æä½éclassãä¿è¨¼ããã
ä¸çªå¤ãå©ç¨ã±ã¼ã¹ã¯ãBasicObjectã®æé¤ã ã¨æãã¾ãã
ä¾ãã°each
ã¡ã½ããã使ããã_Each
ãæå®ããããã ãã©ãnil?
ãis_a?
çããã¤ã¯ä½¿ã£ã¦ãå ´åã
ãã®å ´åã_Each_Nil_Isa
ãªãã¦ããå¥ã®Interfaceãä½ã£ã¦ãããã®ã§ããããã®nil?
ãis_a?
ã使ç¨ãã¦ããã®ã¯ãã¤ã³ã¿ã¼ãã§ã¼ã¹ã¨ããããã¯åã«Object class以ä¸ã®ãªãã¸ã§ã¯ã(ããããã)ãæ³å®ãã¦ããã ãã§ããå ´åãå¤ãã§ãããããªå ´åã«ä½¿ããã®ãObject &
ã¨ããæå®æ¹æ³ã§ããããã§
ãæä½éObject
classã®ã¤ã³ã¹ã¿ã³ã¹ã§ãããã¨ãä¿è¨¼ãã¤ã¤ãeach
ã¡ã½ããã使ãããã¨ã
ã¨ããåã表ç¾ã§ãã¾ãããæä½éObject
classã®ã¤ã³ã¹ã¿ã³ã¹ã§ãããã¨ãããã¨ã¯ãè¨ãæããã°ãBasicObjectã¯é¤ããã¨ãèãããã¾ãã
åå士ã足ãç®ããã
ã§ã¯æ¬å½ã«ã¤ã³ã¿ã¼ãã§ã¼ã¹ã¨ãã¦è¤æ°ã®ã¡ã½ããã使ãããã¨ã表ãããå ´åã¯ã©ã®ãããªæ¹æ³ãããã§ããããï¼
1) æ°ããinterfaceãä½ã
æ°ãã以ä¸ã®ãããªinterfaceãä½ããã¿ã¼ã³ã§ãã
interface _Each_Nil_Isa
def each: ç¥
def nil?: ç¥
def is_a?: ç¥
end
ããã§ãããã®ã§ããããã_Each
ãä¿®æ£ããããããã«è¿½å¾ãããå ´åãããã¾ãããã
2) æ°ããinterfaceãä½ãã¤ã¤ãå
ãããããã®ã¯include
ãã
ãããªãã°
interface _Each_Nil_Isa
include _Each
def nil?: ...
def is_a?: ...
end
ã®ããã«å
ããããã¤ã³ã¿ã¼ãã§ã¼ã¹ã¯include
ã§åæãã¦ä½¿ç¨ããã®ãã¢ãªã ã¨æãã¾ãã
3) Intersectionã使ã
ããã«å¥ã®æ¹æ³ãIntersectionã使ç¨ããæ¹æ³ã§ãã
2)ã®æ¹æ³ã¯å®ã¯ééã£ã¦ãã¦ãå®éã¯åå¤æ°ã使ç¨ãã¾ãã
interface _Each_Nil_Isa[T]
include _Each[T]
def nil?: ...
def is_a?: ...
end
ããããªããnil?
ãis_a?
ã¯åå¤æ°ã使ç¨ãã¦ãã¾ããã_Each
ã«ããåå¤æ°ã¯å¿
è¦ç¡ãç¹ãã¢ã¤ãã¾ãããã
ããã§ãæ°ããinterfaceãä½ããã©_Each
ã¯é¤å¤ãã¦ã¿ã¾ãã
interface _Nil_Isa
def nil?: ...
def is_a?: ...
end
ããã«ããã_Each
ã¨ãã£ã¤ããã°ããããã§ãã
_Each[Integer] & _Nil_Isa
ãããªæãã§ãåå士ã足ãç®ãããã¨ãã«&
ã使ããã¨è¦ãã¦ããã¨ããããã§ãã
è¦ã¯ãã£ã«ã¿ã¼
2ã¤ã®ææ³ã¯ã©ã¡ããifæã®&&
ã®ããã«ãåã«æ¡ä»¶ã追å ãã¦ãã£ã«ã¿ã¼ãã¦ããã¨ãèãããã¾ãã
Foo & _ToF
ã¨ããåã ã£ãããFooåã§ãã£ã«ã¿ã¼ãã¤ã¤ããã¤_ToF
ã§ããã£ã«ã¿ã¼ãã¦ãã¾ããããèããã¨ãIntersectionåã¨ãåéã«ãªããããããªãã§ããããã
Intersectionåã®å¶ç´
ãªãã§ãããã§ãIntersectionã§ç¹ãããã§ããããï¼
ä¾ãã°String & Integer
ã¨ããã¨ããStringã®ã¤ã³ã¹ã¿ã³ã¹ã§ããããã¤Integerã®ã¤ã³ã¹ã¿ã³ã¹ã§ãããã¨ããæå³ã«ãªãã¾ãããããããã®ã¤ã³ã¹ã¿ã³ã¹ã¯åå¨ãå¾ã¾ãããRubyã¯1ã¤ã®classã®ã¿ç¶æ¿ã§ããã®ã§ãIntersectionã«æ¸ããclassã¯1ã¤ã¾ã§ã«å¿
ç¶çã«ãªã£ã¦ãã¾ãã¾ãã
moduleã¯ãããã§ãIntersectionã§ç¹ãããã¾ããmoduleã¯ãããã§ãincludeã§ããã®ã§ããã¹ã¦ã®moduleãincludeããã¦ããã°ããããã§ãã
interfaceããããã§ãIntersectionã§ç¹ãããã¾ããclass/moduleã¯is_a?
ã§èª¿ã¹ã¦trueãè¿ã£ã¦ããã¤ã³ã¹ã¿ã³ã¹ã§ããã°ããã§ãããinterfaceã¯æå®ã®ã¡ã½ãããæã£ã¦ããã°ããã ãã§ãã_ToF & _ToI & _ToS
ã¨ã¤ãªãã¦ãå
¨ãä¸èªç¶ã§ã¯ããã¾ããã
ã¤ã¾ã
ãIntersectionåã¯ãç¹ãããã®ã¯classã¯1ã¾ã§ããã¨ã¯ãããã§ãOKã
ã¨ããã«ã¼ã«ãè¦ãã¦ãã¾ãã
moduleã®self-typeãIntersection
moduleã®å®ç¾©ã«moduleã®selfãä½ããè¨å®ã§ãã¾ããä¾ãã°Enumerable
module㯠_Each
ãself-typeã«ãã¦ãããeach
ã¡ã½ããã使ãããã¨ãä¿è¨¼ãã¦ãã¾ãã
self-typeã®èª¬æã¯æå°éã«ãã¦ããã®self-typeãå®ã¯ä½åã§ãæ¸ãã¾ãã
module Foo : _ToI, _ToF, _ToA
end
ãã®è¤æ°ã®self-typeã¯å
¨ã¦ã®æ¡ä»¶ãæºããã¦ãããã¨ã表ãã¦ããã®ã§ãIntersectionã®é¢ä¿ã«ããã¾ãã
å®éã«steepã§åå¨ããªãã¡ã½ãããæ¸ãã¦ã¿ãã¨self-typeãã®ãããã®ã§è¦ã¦ã¿ã¦ãã ããã
ã¾ã¨ã
Intersectionã¯ã¨ãã ã¡