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
最满意答案
这看起来是一个好的开始。 你应该可以使用
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 astmp = 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.
相关问答
更多-
首先,将页面重命名为.aspx扩展名,并在文件后添加代码。 其次,将此方法添加到后面的代码中: private void WriteLog() { string currentFileName = Path.GetFileNameWithoutExtension(Request.FilePath); string logFileName = string.Format("{0}_{1}.log.txt", currentFileName, DateTime.Now.ToString("ddM ...
-
将其中一个文件的全部内容存储到字符串中,而不是逐行比较。 String strLine1, strLine2; StringBuffer strFile2 = new StringBuffer(); //Store the contents of File2 in strFile2 while((strLine2 = br2.readLine()) != null) { strFile2.append(strLine2); } //Check whether each line of File1 is in ...
-
占位符在文本文档中(Placeholder within a text document)[2022-12-17]
这将匹配组^\$P\{(.*)\}$ 关于如何按点分组,请在此处查看此答案: 正则表达式如何捕获无限组? this will match inside the group ^\$P\{(.*)\}$ for how to group by dots check out this answer here: How do the regular expression capture infinite groups? -
目前,当你找到的东西时,你打电话 System.out.println(line.contains(userSearch)); 据我所知,它会打印“true”或“false”,因为String.contains(String)应该返回一个布尔值。 要打印整行,只需使用System.out.println(line); 或者你想看到什么? 我不知道你的val变量是什么,但如果它只是说“找到结果”或“未找到”你可以使用布尔值。 如果要计算结果,请编写var++; 当它找到某些东西时,如果该行不包含结果则没有。 ...
-
文本文档到屏幕(Text document to screen)[2023-05-07]
正如我评论的那样,你应该使用TextBox 。 您应该将属性Multiline设置为true,将ScrollBars为ScrollBars.Vertical 。 要读取文本文件,请使用以下行: System.IO.File.ReadAllText(textFileLocation) 这将返回带有文件内容的String。 As I commented, you should use a TextBox. You should set the property Multiline to true, and S ... -
两个想法: IR技术在通用域QA中非常好,特别是如果您的文档很大。 我使用elasticsearch索引文档并对索引发出查询。 如果您可以为每个查询保留内存索引和计算相似度值(假设您的文档不大),我会尝试这些短语和句子级别的相似性: https : //github.com/CogComp / cogcomp-NLP /树/主/相似性 Two ideas: IR techniques are pretty good in general-domain QA, especially if your docum ...
-
这看起来是一个好的开始。 你应该可以使用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 lo ...
-
如何从文本文档创建HashMap并显示特定的键和值(How to create a HashMap from a text document and show specific keys and value)[2023-08-20]
看起来没问题,你只需要在调用时改变参数的顺序 ourMap.put(key, value); 尝试 while ((line = in.readLine()) != null) { String parts[] = line.split(",", 2); ourMap.put(parts[0], parts[1]); // change here System.out.println(ourMap.toString()); } it's seems ok, you need o ... -
请尝试fllowing代码: MediaContent mc = new MediaContent(); mc.setUri(UrlForDownload); MediaSource ms = docsService.getMedia(mc); InputStream in = ms.getInputStream(); Please try fllowing code: MediaContent mc = new MediaContent(); mc.setUri(UrlForDownload); Med ...
-
import re userName = raw_input("USER: ") with open("test.txt", "r") as txtFile: for line in txtFile: if re.match(userName + ':', line): print line break # if you sure there is only one user, add break, make you code fas ...