English 中文(简体)
“1”* BigInt(1)”的工作如何,我怎么做?
原标题:How does ‘1 * BigInt(1)’ work and how can I do the same?

我试图执行某些类型的行动,我打过这个问题。

mynum * 1

工作,但不是

1 * mynum

我试图界定这种默示转变。

case class Num(v: Int) {
  def * (o: Int) = new Num(v*o)
}

implicit def int2Num(v: Int) = Num(v)

但这似乎不可行,因为我总是有以下错误:

scala> 1 * new Num(2)
<console>:14: error: overloaded method value * with alternatives:
  (x: Double)Double <and>
  (x: Float)Float <and>
  (x: Long)Long <and>
  (x: Int)Int <and>
  (x: Char)Int <and>
  (x: Short)Int <and>
  (x: Byte)Int
 cannot be applied to (Num)
              1 * new Num(2)
                ^

另一方面

1 * BigInt(1)

因此,在研究守则时,必须找到解决办法。

有哪些机制发挥作用?

EDIT: I created a new question with the actual problem I hit, Why is the implicit conversion not considered in this case with generic parameters?.

最佳回答

我认为,你在接受恩姆语作为论据的Num族中缺乏一种方法。

问题回答

当申请<代码>a.meth(args)失败时,汇编者从<代码>a查询具有的物品。 符合<代码>的任何隐含价值或方法 A =>{ def meth(...)}将照此办理。 如发现,该守则改写为view(a).meth(args)

哪里? 首先,它审视了目前的范围,由当地界定的含蓄物和进口含蓄物组成。

(实际是搜索的第二阶段,其中考虑采用按姓名提出理由的转换方法,但对于这一回答并不重要)。)

如果这失败,它就看着隐含的范围。 这包括我们重新搜索的那部分的对应物体。 在这种情况下,我们在<代码>A =>{ def meth(......)}之后重新编号,因此,我们仅看<代码>A(及其超级类型)。

In Scala trunk, the implicit scope is extended a tiny bit, to include the companion objects of the types of the arguments. Not sure if that was already in 2.9.1, perhaps a friendly reader will figure that out for me and update this answer.

So 1 + BigInt(2) is expanded to BigInt.int2bigInt(1) + BigInt(2)





相关问题
Maths in LaTex table of contents

I am trying to add a table of contents for my LaTex document. The issue I am having is that this line: subsubsection{The expectation of (X^2)} Causes an error in the file that contains the ...

Math Overflow -- Handling Large Numbers

I am having a problem handling large numbers. I need to calculate the log of a very large number. The number is the product of a series of numbers. For example: log(2x3x66x435x444) though my actual ...

Radial plotting algorithm

I have to write an algorithm in AS3.0 that plots the location of points radially. I d like to input a radius and an angle at which the point should be placed. Obviously I remember from geometry ...

Subsequent weighted algorithm for comparison

I have two rows of numbers ... 1) 2 2 1 0 0 1 2) 1.5 1 0 .5 1 2 Each column is compared to each other. Lower values are better. For example Column 1, row 2 s value (1.5) is more ...

热门标签