首页 \ 问答 \ Scala中的隐式函数未按预期工作(Implicit function in Scala not working as expected)

Scala中的隐式函数未按预期工作(Implicit function in Scala not working as expected)

如果我定义了以下函数,它需要Long,并且我定义了下面的隐式函数,当我将Date传递给第一个函数并且一切按预期工作时,使用隐式函数:

def millisToDays(in: Long): Int = (in / (1000L * 3600L * 24L)).toInt
implicit def dateToLong(d: Date) = d.getTime
println(millisToDays(new Date))

但是对于下面的第二个例子,我在第三行得到编译器错误:“推断类型参数[Int]不符合方法mySum的类型参数bounds [t <:java.lang.Number]”

def mySum[T <: Number](as: T*): Double = as.foldLeft(0d)(_ + _.doubleValue)
implicit def intToInteger(n: Int): Integer = new Integer(n.toInt)
var r = mySum(2, 3)

我做错了什么? 为什么不使用intToInteger隐式函数?

我猜测问题是隐式函数不返回“T <:Number”,而是返回一个Integer,因此编译器无法猜测隐式函数实际上是否有用。

无论如何,我可以给编译器一个提示,它应该使用隐式函数?

谢谢! 蚂蚁


If I define the following function which expects a Long, and I define the following implicit function, the implicit function is used when I pass a Date to the first function and everything works as expected:

def millisToDays(in: Long): Int = (in / (1000L * 3600L * 24L)).toInt
implicit def dateToLong(d: Date) = d.getTime
println(millisToDays(new Date))

But for the following second example, I get a compiler error on the third line: "inferred type arguments [Int] do not conform to method mySum's type parameter bounds [t <: java.lang.Number]"

def mySum[T <: Number](as: T*): Double = as.foldLeft(0d)(_ + _.doubleValue)
implicit def intToInteger(n: Int): Integer = new Integer(n.toInt)
var r = mySum(2, 3)

What have I done wrong? Why isn't the intToInteger implicit function being used?

I am guessing that the problem is that the implicit function does not return a "T <: Number", but rather an Integer, so the compiler can't guess that the implicit function is actually useful.

Is there anyway which I can give the compiler a hint that it should use the implicit function?

Thanks! Ant


原文:https://stackoverflow.com/questions/8718530
更新时间:2023-04-10 20:04

最满意答案

这看起来是一个好的开始。 你应该可以使用msg - '0'来获得数字的值。 对于ascii代码,数字以正确的顺序(0,1,2,3,4,5,6,7,8,9)彼此相邻放置。 使用'0'减去时所执行的操作是使用ascii代码'0'减去msg。 你将会得到数字

tmp = msg - '0';
idx = find(tmp>=0 & tmp < 10); % Get the position in the row
val = tmp(idx); % Or tmp(tmp>=0 & tmp < 10) with logical indexing.

我同意fgetl可能是最适合没有特定结构的文本。 但是,如果你有一个特殊的文本结构,你可以使用它,从而能够使用更有效的算法。

如果您确实在找到文本中数字的绝对位置后,可以为每次迭代保存msgLength = msgLength + length(msg) ,并使用它来计算数字的绝对位置。


This looks like a good start. You should be able to use msg - '0' to get the value of the numbers. For ascii code the digits are placed next to each other in the right order (0,1,2,3,4,5,6,7,8,9). What you do when you subtract with '0' is that you subtract msg with the ascii code of '0'. You will then get the digits as

tmp = msg - '0';
idx = find(tmp>=0 & tmp < 10); % Get the position in the row
val = tmp(idx); % Or tmp(tmp>=0 & tmp < 10) with logical indexing.

I agree that fgetl is probably the best to use for text without specific structure. However, in case you have a special structure of the text you can use that and thus be able to use more effective algorithms.

In case you was actually after finding the absolute position of the digits in the text, you can save the msgLength = msgLength + length(msg) for every iteration and use that to calculate the absolute position of the digits.

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。