为什么Optional不扩展Supplier

发布于 2024-12-11 13:53:04 字数 1109 浏览 0 评论 0原文

我使用了 供应商经常,我正在寻找新的 Guava 10 可选 现在。

与供应商相反,Optional 保证永远不会返回 null,但会抛出 IllegalStateException。此外,它是不可变的,因此一旦创建它就具有固定的已知值。与此相反,Supplier 可以用于创建通过调用 get() 触发的不同或惰性值(但不强制这样做)。

我关注了关于为什么可选不应该扩展供应商的讨论,我发现:

...这不会是一个行为良好的供应商

但我不明白为什么,因为供应商明确状态: 无保证此接口隐含了

对我来说这很合适,但我似乎以前以与最初预期不同的方式雇用供应商。有人可以向我解释为什么选项不应该用作供应商吗?

是的:将Optional转换为Supplier非常容易(此外,您可以选择改编后的Supplier.get()是否返回Optional.get()或Optional.orNull()) 但您需要一些额外的转换,并且必须为每个对象创建新对象:-(

供应商的预期用途和我对其文档的理解之间似乎存在一些不匹配。Dieter

I used Supplier quite often and I'm looking at new Guava 10 Optional now.

In contrast to a Supplier an Optional guarantees never to return null but will throw an IllegalStateException instead. In addition it is immutable and thus it has a fixed known value once it is created. In contrast to that a Supplier may be used to create different or lazy values triggered by calling get() (but it is not imposed to do so).

I followed the discussion about why an Optional should not extend a Supplier and I found:

...it would not be a well-behaved Supplier

But I can't see why, as Supplier explicitly states:
No guarantees are implied by this interface.

For me it would fit, but it seems I used to employ Suppliers in a different way as it was originally intended. Can someone please explain to me why an Optional should NOT be used as a Supplier?

Yes: it is quite easy to convert an Optional into a Supplier (and in addition you may choose if the adapted Supplier.get() will return Optional.get() or Optional.orNull())
but you need some additional transformation and have to create new objects for each :-(

Seems there is some mismatch between the intended use of a Supplier and my understanding of its documentation.

Dieter.

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

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

发布评论

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

评论(2

羁拥 2024-12-18 13:53:04

考虑一下这个例子

Supplier<String> s = Optional.absent();

。您有一个包含一个方法的类型,该方法不带任何参数,但调用该方法会导致程序员错误!这真的有意义吗?

您只需要“当前”选项的供应商身份,然后,只需使用 Suppliers.ofInstance

Consider the case of

Supplier<String> s = Optional.absent();

Think about this. You have a type containing one method, that takes no arguments, but for which it's a programmer error to ever invoke that method! Does that really make sense?

You'd only want Supplierness for "present" optionals, but then, just use Suppliers.ofInstance.

围归者 2024-12-18 13:53:04

通常期望 Supplier 能够返回对象(假设没有发生意外错误)。 Optional 是明确可能无法返回对象的东西。

我认为“此接口不暗示任何保证”通常意味着不保证它如何检索对象,而不是该接口根本不需要暗示检索对象的能力。即使您认为 Supplier 实例在每次调用 get() 时抛出异常是可以的,Guava 作者并不这么认为,而是选择只提供通常表现良好的供应商。

A Supplier is generally expected to be capable of returning objects (assuming no unexpected errors occur). An Optional is something that explicitly may not be capable of returning objects.

I think "no guarantees are implied by this interface" generally means that there are no guarantees about how it retrieves an object, not that the interface need not imply the ability to retrieve an object at all. Even if you feel it is OK for a Supplier instance to throw an exception every time you call get() on it, the Guava authors do not feel that way and choose to only provide suppliers that can be expected to be generally well-behaved.

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