2012-08-01 167 views
6

我從包中導入的函數implicit有問題。scala:隱式轉換不起作用

我有一個類,使用正則表達式來查找文本中的東西。我想用它作爲:

val pattern = "some pattern here".r           
pattern findSomethingIn some_text            

要做到這一點,我定義implicit finction到pattern轉換爲包含包裝WrapperfindSomethingIn功能

package mypackage {               

    class Wrapper (val pattern: Regex) {          
    def findSomethingIn(text: String): Something = ...      
    }                   

    object Wrapper {               
    implicit def regex2Something(pat: Regex): Wrapper = new Wrapper(pat) 
    }                   

}                    

如果我用它作爲

import mypackage._               

Wrapper.regex2Something(pattern) findSomethingIn some_text     

它的工作原理。而如果我使用

pattern findSomethingIn some_text // implicit should work here??    

我得到

value findPriceIn is not a member of scala.util.amtching.Regex    

所以隱式轉換不會在這裏工作......這是什麼問題?

回答

9

您需要

import mypackage.Wrapper._ 

導入適當的方法。

有關更多信息,請參閱this blog entry,並特別注意Conversions對象的定義/導入。