scala 有没有一种简单的方法可以将布尔值转换为整数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2633719/
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
Is There an Easy Way to Convert a Boolean to an Integer?
提问by Russ Bradberry
I am new to scala and I am finding the need to convert a boolean value to an integer. I know i can use something like if (x) 1 else 0but I would like to know if there is a preferred method, or something built into the framework (ie toInt())
我是 Scala 的新手,我发现需要将布尔值转换为整数。我知道我可以使用类似的东西,if (x) 1 else 0但我想知道是否有首选方法,或者框架中内置的东西(即toInt())
回答by Patrick
If you want to mix Booleanand Intoperation use an implicitas above but without creating a class:
如果你想混合Boolean和Int操作使用implicit上面的但不创建一个类:
implicit def bool2int(b:Boolean) = if (b) 1 else 0
scala> false:Int
res4: Int = 0
scala> true:Int
res5: Int = 1
scala> val b=true
b: Boolean = true
scala> 2*b+1
res2: Int = 3
回答by Hymanson Davis
You can do this easily with implicit conversions:
您可以使用隐式转换轻松完成此操作:
class asInt(b: Boolean) {
def toInt = if(b) 1 else 0
}
implicit def convertBooleanToInt(b: Boolean) = new asInt(b)
Then, you will get something like
然后,你会得到类似的东西
scala> false toInt
res1: Int = 0
回答by Paul Murray
While using an implicitis probably the best way to go, if you want a quick-and-dirty conversion from booleanto intyou can use boolean.compare(false)
虽然使用 animplicit可能是最好的方法,但如果您想要从boolean到的快速转换,int您可以使用boolean.compare(false)
回答by ArtemGr
Actually, I'd expect it to be if (x) 1 else 0, not if (x) 0 else 1.
实际上,我希望它是if (x) 1 else 0,而不是if (x) 0 else 1。
That's why you should write your own conversions. Integer isn't a boolean, and if you want for some reason to store booleans as integers, then you should hone your own standards of how the truth and not truth are represented.
这就是为什么您应该编写自己的转换。Integer 不是布尔值,如果您出于某种原因想要将布尔值存储为整数,那么您应该磨练自己的标准,即如何表示真相而不是真相。
Boolean "true" is not a number, it is an instance of the Boolean type. Like java.lang.Boolean.TRUE. It can be stored internally as an integer, but that is an implementation detail that shouldn't be leaked into the language.
Boolean "true" 不是数字,它是 Boolean 类型的一个实例。喜欢java.lang.Boolean.TRUE。它可以在内部存储为整数,但这是一个不应泄露到语言中的实现细节。
I'd say that if (x) 0 else 1is the preferred method of conversion. It is simple and fast.
我会说这if (x) 0 else 1是转换的首选方法。它简单快捷。
You can also write x match {case true => 0; case false => 1}if you want to use a more general pattern matching approach.
x match {case true => 0; case false => 1}如果您想使用更通用的模式匹配方法,也可以编写。
回答by Gaurav Kumar
If you don't want to go the implicit way, this may be useful:
如果您不想采用隐式方式,这可能很有用:
var myBool:Boolean = true
myBool: Boolean = true
myBool.compare(false)
res3: Int = 1
myBool = false
myBool: Boolean = false
myBool.compare(false)
res3: Int = 0
回答by Suma
Since Scala 2.10 the solution by Hymanson Davis is more often written using an implicitvalueclass:
从 Scala 2.10 开始,Hymanson Davis 的解决方案更常使用隐式值类编写:
implicit class BoolToInt(val b:Boolean) extends AnyVal {
def toInt = if (b) 1 else 0
def * (x:Int) = if (b) x else 0
}
For added comfort I have also added a multiplication operator, as this is the most common use of a Boolean to Int conversion for me. I prefer this over making the conversion itself implicit (solution provided by Patrick), as that loses more of the type control than I want.
为了增加舒适度,我还添加了一个乘法运算符,因为这是我最常用的 Boolean 到 Int 转换。我更喜欢这个而不是使转换本身隐式(由帕特里克提供的解决方案),因为这失去了比我想要的更多的类型控制。

