Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 如何将枚举类型传递给函数_Scala - Fatal编程技术网

Scala 如何将枚举类型传递给函数

Scala 如何将枚举类型传递给函数,scala,Scala,我有一个枚举 object UserTokenType extends Enumeration { type TokenType = Value val RegistrationConfirmation = Value("RegistrationConfirmation") val ResetPasswordConfirmation = Value("ResetPasswordConfirmation") } 我想把它传递给这样一个函数 de

我有一个枚举

object UserTokenType extends Enumeration {
  type TokenType = Value
  val RegistrationConfirmation = Value("RegistrationConfirmation")
  val ResetPasswordConfirmation = Value("ResetPasswordConfirmation")
}
我想把它传递给这样一个函数

def generateEmailTokenForUser(user:User,tokenType:UserTokenType) = {..}
但是上面的代码没有编译


我需要将其作为Int传递吗?我关心的是如何检查是否传递了有效的枚举值?

正如注释中所建议的,您可以使用
,它是在中定义的类(请参阅链接文档)。您不能在这里直接使用
UserToken
,因为它不是类或类型(因此这样称呼会产生误导)

工作代码示例:

objectusertoken扩展枚举{
val RegistrationConfirmation=值(“RegistrationConfirmation”)
val ResetPasswordConfirmation=值(“ResetPasswordConfirmation”)
}
val v:UserToken.Value=UserToken.ResetPasswordConfirmation
def generateemailtokeforuser(用户:用户,令牌:UserToken.Value)={
令牌匹配{
case UserToken.RegistrationConfirmation=>println(“案例1”)
case UserToken.ResetPasswordConfirmation=>println(“案例2”)
}
}

正如注释中所建议的,您可以使用
值,该值是在中定义的类(请参见链接文档)。您不能在这里直接使用
UserToken
,因为它不是类或类型(因此这样称呼会产生误导)

工作代码示例:

objectusertoken扩展枚举{
val RegistrationConfirmation=值(“RegistrationConfirmation”)
val ResetPasswordConfirmation=值(“ResetPasswordConfirmation”)
}
val v:UserToken.Value=UserToken.ResetPasswordConfirmation
def generateemailtokeforuser(用户:用户,令牌:UserToken.Value)={
令牌匹配{
case UserToken.RegistrationConfirmation=>println(“案例1”)
case UserToken.ResetPasswordConfirmation=>println(“案例2”)
}
}

如果要保留原始方法签名,请尝试以下操作

object UserTokenType extends Enumeration {
  type UserTokenType = Value
  val RegistrationConfirmation = Value("RegistrationConfirmation")
  val ResetPasswordConfirmation = Value("ResetPasswordConfirmation")
}
import UserTokenType._

def generateEmailTokenForUser(tokenType: UserTokenType) = ???
generateEmailTokenForUser(RegistrationConfirmation)
我们将类型别名更改为

type UserTokenType = Value
然后把它带到了范围内

import UserTokenType._

作为备选方案考虑


作为一个旁注,考虑单值对象类型和单值对象类型之间的差异,作为值

val x:         Int        =      42
val y: UserTokenType.type = UserTokenType
                |                 |
               type             value
所以当你写信的时候

def generateEmailTokenForUser(tokenType: UserTokenType)
这在原则上类似于

def f(x: 42)

如果要保留原始方法签名,请尝试以下操作

object UserTokenType extends Enumeration {
  type UserTokenType = Value
  val RegistrationConfirmation = Value("RegistrationConfirmation")
  val ResetPasswordConfirmation = Value("ResetPasswordConfirmation")
}
import UserTokenType._

def generateEmailTokenForUser(tokenType: UserTokenType) = ???
generateEmailTokenForUser(RegistrationConfirmation)
我们将类型别名更改为

type UserTokenType = Value
然后把它带到了范围内

import UserTokenType._

作为备选方案考虑


作为一个旁注,考虑单值对象类型和单值对象类型之间的差异,作为值

val x:         Int        =      42
val y: UserTokenType.type = UserTokenType
                |                 |
               type             value
所以当你写信的时候

def generateEmailTokenForUser(tokenType: UserTokenType)
这在原则上类似于

def f(x: 42)

您可以使用
UserTokenType.Value
UserTokenType.UserTokenType
还可以创建
sealed trait UserTokenType
case对象注册确认扩展UserTokenType
。此解决方案更适合于模式匹配,因为sbt可以在编译时警告您是否遗漏了某些案例。您可以使用
UserTokenType.Value
UserTokenType.UserTokenType
还可以创建
UserTokenType
case对象注册确认扩展UserTokenType
。这种解决方案更适合于模式匹配,因为如果您遗漏了某些情况,sbt可以在编译时向您发出警告。