scala 如何创建一个空的 java.util.UUID 对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26637225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 06:39:45  来源:igfitidea点击:

How can I create an empty java.util.UUID object?

javascalauuid

提问by jeffrey

I don't know why I can't find the answer to this, but I need to pass a blank UUID object in one of my functions to represent a lack of UUID. What is the UUID analagous form of

我不知道为什么我找不到这个问题的答案,但是我需要在我的一个函数中传递一个空白的 UUID 对象来表示缺少 UUID。什么是 UUID 的类似形式

val x: "" 

, which would be an empty string. I'm essentially trying to get an empty UUID. I tried

, 这将是一个空字符串。我本质上是想获得一个空的 UUID。我试过

UUID.fromString("")

but received an error, as you need a valid UUID string.

但收到一个错误,因为您需要一个有效的 UUID 字符串。

EDIT: I am implementing this in Scala.

编辑:我正在 Scala 中实现这个。

回答by Michael Zajac

Let me preface this by saying it would be much better to use Option[UUID]instead, with Nonerepresenting an empty UUID.

让我先说一下,使用它会更好Option[UUID]None代表一个空的 UUID。

You can't use an empty String, as it does not conform to the UUID format, described here.

您不能使用空的String,因为它不符合此处描述的 UUID 格式。

You coulduse

可以

UUID.fromString("00000000-0000-0000-0000-000000000000")

Which would be the same as

这将与

new UUID(0L, 0L)

But the usage of that would be arbitrary, and it would be much better to signify the absence or lack of a UUIDwith Option.

但是它的用法是任意的,最好表示没有或缺少UUIDwith Option

回答by Denis Makarenko

Did you consider using Option[UUID] as a parameter type? In this case you can pass None to indicate lack of UUID. An empty string is not a valid guid that's why it is rejected by UUID.fromString

您是否考虑使用 Option[UUID] 作为参数类型?在这种情况下,您可以通过 None 来指示缺少 UUID。空字符串不是有效的 guid,这就是它被 UUID.fromString 拒绝的原因