10

新しいクラスを定義して演算子をオーバーロードするときに、登録されている古いスタイルの S3 クラスで演算子をオーバーロードしても期待どおりに機能しない理由を誰かが説明できますか?

次の例に示すように。

これは動作しません。

require(ff) 
setOldClass(Classes=c("ff_vector")) 
setMethod( 
  f="*", 
  signature = signature(e1 = c("ff_vector"), e2 = c("ff_vector")), 
  definition = function (e1, e2){ 
        print("S3 setOldClass")
        e1[] * e2[] 
    } 
) 
ff(1:10) * ff(1:10) 
Error in ff(1:10) * ff(1:10) : non-numeric argument to binary operator

しかし、これは機能します。

setClass("myff_vector", representation(x="ff_vector"))
setMethod( 
  f="*", 
  signature = signature(e1 = c("myff_vector"), e2 = c("myff_vector")), 
  definition = function (e1, e2){ 
        print("S4 setOldClass")
        e1@x[] * e2@x[] 
    } 
) 
new("myff_vector", x = ff(1:10)) * new("myff_vector", x = ff(1:10))
[1] "S4 setOldClass"
[1]   1   4   9  16  25  36  49  64  81 100
4

2 に答える 2