我有一个班级A
,它还定义了对B
的隐式转换。
case class A(number:Int)
case class B(number:Int, tag:String)
implicit def AtoB(a:A) = B(a.number,"Hello World")
我还有一个检索A
的函数,并希望调用一个隐含B
参数的函数:
def hello()(implicit b:B)= {....}
def execute = {
implicit val a:A = ....
hello() //doesnt compile missing implicit parameter b
}
如何在不明确定义B
的情况下使此代码正常工作?即
val b:B = a
答案 0 :(得分:3)
定义一个这样的函数:
Sub FilnamnRitningar()
Dim varDirectory As Variant
Dim flag As Boolean
Dim i As Integer
Dim strDirectory As String
Dim ws As Worksheet
Dim rng As Range
Dim a As String
Dim o As String
Dim a1 As String
Dim a2 As String
a = Chr(228)
a1 = Chr(229)
o = Chr(246)
a2 = Chr(197)
Set ws = Sheets("Macron")
Set rng = ws.Range("A4:C100")
strDirectory = ("C:\Users\" & Application.VLookup("saljareKonto", rng, 3, False) & "\Dropbox (ATLE Consulting)\ATLE Consulting\Partner\" & Application.VLookup("partnerBolagsnamn", rng, 3, False) & "\Best" & a & "llare\" & Application.VLookup("kundFulltNamn", rng, 3, False) & "\Ritningar\")
i = 1
flag = True
varDirectory = Dir(strDirectory, vbDirectory)
Range("M153").Select
While flag = True
If varDirectory = "" Then
flag = False
Else
Cells(i + 1, 1) = varDirectory
'returns the next file or directory in the path
varDirectory = Dir
i = i + 1
End If
Wend
End Sub
当您拨打implicit def implicitBfromA(implicit a: A): B = a
或将其放入hello
的配套对象时,请将其放在范围内。
此功能不是隐式转换。它声明,如果范围中已存在类型为B
的隐式值,则存在类型B
的隐式值。
请注意,要使其正常工作,应该在文件中 A
之后定义,或者AtoB
应该有明确指定的返回类型:{{1或者你应该在其正文中明确地调用AtoB
:implicit def AtoB(a:A): B = B(a.number, "Hello World")
一个完整的工作示例:
AtoB