为什么scala无法对f绑定多态进行类型推断?(Why scala fails type inference for f-bound polymorphism?)
举例说明问题:
trait WTF[W <: WTF[W]] { def get : Int } trait Zero extends WTF[Zero] { override def get : Int = 0 } case object Zero extends Zero final case class Box(inner : Int) extends WTF[Box] { override def get : Int = inner } def printWTF[W <: WTF[W]](w : W) = println(w.get) printWTF(Box(-1)) printWTF(Zero)
Box
,但Zero
产生错误:WTF.scala:22: error: inferred type arguments [Zero.type] do not conform to method printWTF's type parameter bounds [W <: WTF[W]] printWTF(Zero) ^ WTF.scala:22: error: type mismatch; found : Zero.type required: W printWTF(Zero) ^ two errors found
如果我手动注释类型,它编译:
printWTF[Zero](Zero) printWTF(Zero : Zero)
第一行按预期工作。 我经常遇到无法从参数推断出类型参数的情况。 例如
def test[A](x : Int) : Unit
。A
类型在参数签名中没有出现,因此您应该手动指定它。但后者对我来说非常模糊。 我刚刚添加了类型转换,它始终是真的,并且奇迹般地编译器学习如何推断方法类型参数。 但
Zero
始终是Zero
类型,为什么编译器无法在没有提示的情况下推断它呢?Simple example to illustrate the issue:
trait WTF[W <: WTF[W]] { def get : Int } trait Zero extends WTF[Zero] { override def get : Int = 0 } case object Zero extends Zero final case class Box(inner : Int) extends WTF[Box] { override def get : Int = inner } def printWTF[W <: WTF[W]](w : W) = println(w.get) printWTF(Box(-1)) printWTF(Zero)
Box
is ok, butZero
produces error:WTF.scala:22: error: inferred type arguments [Zero.type] do not conform to method printWTF's type parameter bounds [W <: WTF[W]] printWTF(Zero) ^ WTF.scala:22: error: type mismatch; found : Zero.type required: W printWTF(Zero) ^ two errors found
If I annotate type manually, it compiles:
printWTF[Zero](Zero) printWTF(Zero : Zero)
The first line works as expected. I frequently encounter cases where type parameters could not be inferred from arguments. e.g.
def test[A](x : Int) : Unit
. TheA
type appears nowhere in the argument signature, so you should specify it manually.But the latter is very obscured to me. I just added type cast that always is true, and miraculously the compiler learns how to infer method type parameters. But
Zero
is always ofZero
type, why the compiler could not infer it without hints from me?
原文:https://stackoverflow.com/questions/42181021
最满意答案
@Symeon - 必须在清单文件中提供Internet权限。
并使用HTML我会建议请使用Phonegap ..导入CoroDova.jar和phonegap所需的权限..当你通过Android上的Phonegap运行HTML时我将轻松运行,即使你将能够使用jquery mobile和javascripts和所有
@Symeon - Have to Provided the Internet Permission in Manifest File.
And to use HTML i will suggest Please use Phonegap.. import CoroDova.jar and required Permissions for phonegap.. than when u run the HTML through Phonegap on Android i will run with ease and Even u will be able to use jquery mobile and javascripts and all
相关问答
更多-
android 3d游戏开发 所需要的软件[2023-10-23]
eclipse -
安卓应用程序开发使用哪个开发工具最好?[2022-11-21]
eclipse -
安卓手机编程现在用什么软件?并求下载地址。[2022-05-06]
eclipse -
要将代码添加为库,首先需要将其导入Eclipse工作区(右键单击Package Explorer,选择'Import ...','Android','将现有Android代码导入工作区......'),然后通过在该项目的属性窗口中选择“is library”将其定义为“Library”。 To add code as a library, you first need to import it into your Eclipse workspace (right-click your Package Exp ...
-
没关系,我明白了。 显然,当您选择存储库时,您必须右键单击并单击刷新,它将显示文件夹列表(愚蠢的日食不会自动执行)。 然后选择项目并将其创建为新项目。 Voilla,你可以编译和运行它 Never mind, I figured it out. Apperently, when you choose the repository you have to right click and click refresh and it will show you a list of Folders (stupid e ...
-
您可能需要升级您的eclipse版本。 最新版本的Helios应该内置它。或者根据您的版本,您可能需要添加支持。 ( http://www.htmlgoodies.com/html5/tutorials/working-with-html5-in-eclipse-juno.html#fbid=URJEyY5-4N1 ) 安装新软件 - > Web,XML和Java EE开发 - >网页编辑器 看看这里添加支持http://lizusefulstuff.blogspot.kr/2012/07/how-to-a ...
-
使用Eclipse的Android项目 - HTML - WebSources(Android Project with Eclipse - HTML - WebSources)[2024-05-02]
@Symeon - 必须在清单文件中提供Internet权限。 并使用HTML我会建议请使用Phonegap ..导入CoroDova.jar和phonegap所需的权限..当你通过Android上的Phonegap运行HTML时我将轻松运行,即使你将能够使用jquery mobile和javascripts和所有 @Symeon - Have to Provided the Internet Permission in Manifest File. And to use HTML i will sugge ... -
单击新类时,将显示弹出窗口,您可以在其中设置新包名称。 (如果你不是在寻找这个,那么你应该尝试下面的新事物)。 您可以单击包(在创建新项目时创建),右键单击该包,然后转到新的类以正确设置。 我希望这回答了你的问题。 When you click on new class, you get the popup window, where you can set the new package name. (If you're not looking for this, then you should try ...
-
您需要将桌面主题更改为其他主题。 对于KDE用户转到: 配置桌面 => 应用程序外观 => GTK并将主题从oxygen-gtk更改为Adwaita 。 您的Eclipse将不再崩溃,桌面看起来会更加优质。 You need to change the Desktop Theme to a different theme. For KDE users goto: Configure Desktop => Application Appearance => GTK and change the theme f ...
-
我在互联网上发现的是你可以升级你的java。 所以这发生了。 Mac的这个问题的解决方案是编辑Eclipse.ini文件以使用java 1.6。下面的代码将起作用: -vm/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Commands/java What I found up on the internet was that you could have upgraded your java. So this occurred. T ...