为什么这种结构类型绑定不能按预期工作?

发布于 2024-10-14 01:13:55 字数 672 浏览 1 评论 0原文

我正在尝试编写一个简单的辅助方法,该方法接收可以关闭的内容和一些接收前者并确保“可关闭”在执行该函数后关闭的函数。

例如,我想像这样使用它:

  closing(new FileOutputStream("/asda"))(_.write("asas"))

我的 impl 是

object Helpers {

  def closing[T <: { def close }](closeable: T)(action: T => Unit): Unit =
    try action apply closeable finally closeable close

}

但是当尝试编译这个简单的测试时:

object Test {

  import Helpers._

  closing(new FileOutputStream("/asda"))(_.write("asas"))

}

编译器抱怨:

推断类型参数 [java.io.FileOutputStream] 不 符合方法关闭的类型 参数范围 [T <: AnyRef{def 关闭:单位}]

有什么想法吗?

I'm trying to write a simple helper method that receives something that can be closed and some function which receives the former and ensures the "closeable" is closed after executing the function.

For example, I want to use it like this:

  closing(new FileOutputStream("/asda"))(_.write("asas"))

My impl is

object Helpers {

  def closing[T <: { def close }](closeable: T)(action: T => Unit): Unit =
    try action apply closeable finally closeable close

}

But when trying to compile this simple test:

object Test {

  import Helpers._

  closing(new FileOutputStream("/asda"))(_.write("asas"))

}

The compiler complains with:

inferred type arguments
[java.io.FileOutputStream] do not
conform to method closing's type
parameter bounds [T <: AnyRef{def
close: Unit}]

Any ideas why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

久隐师 2024-10-21 01:13:55

你需要写出

def closing[T <: { def close() }]

Scala 中带空括号的方法和根本不带括号的方法之间的区别。

You need to write

def closing[T <: { def close() }]

there is a difference in Scala between methods with empty parentheses and methods without parentheses at all.

神妖 2024-10-21 01:13:55

类型界限很棘手。特别是,除了参数本身之外,Scala 还跟踪参数列表的数量。试试这些吧!

class A { def f = 5 }
class B { def f() = 5 }
class C { def f()() = 5 }
def useA[X <: { def f: Int }](x: X) = x.f
def useB[X <: { def f(): Int }](x: X) = x.f
def useC[X <: { def f()(): Int}](x: X) = x.f

useA(new A)  // This works, but what other combinations do?

就你而言,你想要

def closing[T <: { def close() }] ...

P.S.如果您确实打算经常使用它,您可能还应该尝试一下

class D extends B { override def f = 6 }
class E extends A { override def f() = 6 }

,看看在每种情况下您需要使用哪个use

Type bounds are tricky. In particular, Scala keeps track of the number of parameter lists in addition to the parameters themselves. Try these out!

class A { def f = 5 }
class B { def f() = 5 }
class C { def f()() = 5 }
def useA[X <: { def f: Int }](x: X) = x.f
def useB[X <: { def f(): Int }](x: X) = x.f
def useC[X <: { def f()(): Int}](x: X) = x.f

useA(new A)  // This works, but what other combinations do?

In your case, you want

def closing[T <: { def close() }] ...

P.S. If you really plan on using this a lot, you probably ought also play with

class D extends B { override def f = 6 }
class E extends A { override def f() = 6 }

and see which use you need to use in each case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文