2016-10-22 58 views
1

下面是我的方法簽名和定義階什麼是錯的方法定義

def accumulate[T[_]: Traversable, O: Monoid, A]: (A => O) => T[A] => O = 
    fao => ta => 
     (implicitly[Traversable[T]].traverse[({type f[X] = Acc[O, X]})#f, A, O](ta)(a => Acc(fao(a)))).value 

    def reduce[T[_]: Traversable, O: Monoid]: T[O] => O = to => accumulate[T, O, O](a => a)(to) 

但是我得到了我的定義,下面的錯誤減少

Error:(160, 82) not enough arguments for method accumulate: (implicit evidence$7: Traversable[T], implicit evidence$8: Monoid[O])(O => O) => (T[O] => O). 
Unspecified value parameter evidence$8. 
    def reduce[T[_]: Traversable, O: Monoid]: T[O] => O = to => accumulate[T, O, O](a => a)(to) 
                       ^

不知道我要去的地方錯誤。任何幫助,將不勝感激。

謝謝!

+1

它會更容易,如果你提供一個參考Traversable的/含半幺羣/累加器的實現,據我所知Scala的內置儘管Scalaz和Cat中的遍歷類型類可以遍歷,但沒有遍歷方法。 –

+0

@AngeloGenovese假設只有一個(合法的)「Traversable」或「Monoid」的實現是可以安全的,他可以在這裏指出這是通常可以在'scalaz'或'cats'中找到的實現。 – Yawar

+0

@Yawar公平的,但這也意味着爲了試圖重現他的問題,我花時間弄清楚了什麼樣的Acc,並且記住Scala集合Traversable與Traverse類型不一樣。奇怪的是,使用cat和一個虛擬imp的Acc我沒有在scala 2.10上得到相同的編譯錯誤。 (從頭開始,只是我的IDE隱藏了我的東西) –

回答

2

您正在被隱藏(隱含)參數絆倒到accumulate方法。上下文界定你放在它意味着真的有以下類型簽名的方法:

def accumulate[T[_], O, A](
    implicit traversable: Traversable[T], 
    monoid: Monoid[O]): (A => O) => T[A] => O 

其實我建議,如果你實際上需要使用你使用情境界定它們在你的方法中的相應含義(而不是將它們隱式地傳遞給另一種方法)。明確地(有諷刺意味地)輸入含義更清楚。

那麼,正在發生的事情reduce是,你試圖在功能a => a通過在編譯器期待的Traversable[T]Monoid[O]兩個隱含參數的位置。該解決方案是在implicits傳遞明確,或monomorphiseaccumulate調用前:

def reduce[T[_]: Traversable, O: Monoid]: T[O] => O = { to => 
    // This forces the compiler to pass in the correct implicits 
    val accumulate_ = accumulate[T, O, O] 
    accumulate_(a => a)(to) 
} 
+0

謝謝!這樣可行。但爲什麼這不起作用 積累(Traversable [T],Monoid [O])(a => a)(to)以及您寫的內容。這給我一個編譯錯誤。 –

+0

@AbdulRahman嗯,也許'Traversable.apply'和'Monoid.apply'沒有被定義爲返回正確的含義?你能發佈編譯錯誤消息嗎? – Yawar

+0

錯誤:(162,27)對象Traversable不帶類型參數。 累積(Traversable [T],Monoid [O])(a => a)(to) 錯誤:(162,38)對象Monoid不帶類型參數。 (a => a)(to) ^ –

相關問題