关于 Scala 中视图的问题

发布于 2024-10-29 17:49:24 字数 300 浏览 1 评论 0原文

我看到了一些例子,其中转换函数 T =>; S 作为隐式参数传递。 Scala 调用这个函数view,甚至提供特殊的语法糖 - - 视图绑定 - 对于这种情况。

然而我们已经有了隐式转换!我可以用隐式转换替换这些视图(即作为隐式参数传递的转换函数)吗? ?我可以使用 views 做什么而不能使用隐式转换?

I saw examples, where a conversion function T => S is passed as an implicit parameter. Scala calls this function view and even provides special syntax sugar -- view bound -- for that case .

However we already have implicit conversions ! Can I replace these views (i.e. conversion functions passed as implicit parameters) with implicit conversions ? ? What I can do with views what I can't with implicit conversions ?

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

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

发布评论

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

评论(2

挽清梦 2024-11-05 17:49:24

“是”有什么优势

case class Num(i: Int)
implicit def intToNum(i: Int) = Num(i)

def test[A <% Num](a: A): Int = a.i
test(33)

我对你的问题的理解是,相对于

def test2(a: Num): Int = a.i
test2(33)

?视图的含义正是这样:类型 T 可以被视为另一种类型 S。您的方法或函数可能希望首先处理 T。一个例子是 Ordered:

def sort[A <% Ordered[A]](x: A, y: A): (A, A) = if (x < y) (x, y) else (y, x)

sort(1, 2)     // --> (1,2)
sort("B", "A") // --> (A,B)

视图边界的另外两个用例:

  • 您可能希望仅在某些情况下从 T 转换为 S,例如延迟
    (这在某种程度上与上面的情况相同:您基本上想要使用 T)
  • 您可能想要链接隐式转换。请参阅这篇文章:如何在 Scala 中链接隐式?

My understanding of your question is, what would be the advantage of

case class Num(i: Int)
implicit def intToNum(i: Int) = Num(i)

def test[A <% Num](a: A): Int = a.i
test(33)

over

def test2(a: Num): Int = a.i
test2(33)

Yes? Well the meaning of view is exactly that: the type T can be viewed as another type S. Your method or function might want to deal with T in the first place. An example is Ordered:

def sort[A <% Ordered[A]](x: A, y: A): (A, A) = if (x < y) (x, y) else (y, x)

sort(1, 2)     // --> (1,2)
sort("B", "A") // --> (A,B)

Two more use cases for view bounds:

  • you may want to convert from T to S only under certain circumstances, e.g. lazily
    (this is in a way the same situation as above: you basically want to work with T)
  • you may want to chain implicit conversions. See this post: How can I chain implicits in Scala?
不羁少年 2024-11-05 17:49:24

您所说的隐式转换只不过是全局范围内的视图。

使用类型参数时,视图边界是必要的,因为它是需要隐式转换的标志。例如:

def max[T](a: T, b: T): T = if (a < b) b else a

因为 T 上没有任何约束,所以编译器不知道 < 方法可用。让我们看看编译器是否允许您继续执行此操作,然后考虑这两个调用:

max(1, 2)
max(true, false)

签名 max[T](a: T, b: T): T 中没有任何内容告诉编译器应该这样做不允许第二次调用,但应该允许第一次。这就是视图边界的用武之地:

def max[T <% Ordered[T]](a: T, b: T): T = if (a < b) b else a

它不仅告诉编译器 < 方法来自哪里,还告诉编译器 max(true, false) 不是有效的。

What you call implicit conversions is nothing more than a view in global scope.

View bounds are necessary in when using type parameters, as it is a sign that an implicit conversion is necessary. For example:

def max[T](a: T, b: T): T = if (a < b) b else a

Because there's no constrains whatsoever on T, the compiler doesn't know that a < method will be available. Let's see the compiler let you go ahead with that, then consider these two calls:

max(1, 2)
max(true, false)

There's nothing in the signature max[T](a: T, b: T): T that tells the compiler it should not allow the second call, but should allow the first. This is where view bounds come in:

def max[T <% Ordered[T]](a: T, b: T): T = if (a < b) b else a

That not only tells the compiler where the < method comes from, but also tells the compiler that max(true, false) is not valid.

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