如何使用scala类型别名(Int、String)

如何使用scala类型别名(Int、String),scala,types,type-alias,Scala,Types,Type Alias,在类似的情况下 type abc=(Int,String) val list=mutable.set[abc]() 我如何在列表中添加内容?类型为(Int,String)的东西看起来像什么? 我尝试过做类似于list+=(5,“hello”)的事情,但没有任何效果。不完全确定您到底在寻找什么,但下面是一些向列表中添加abc类型的示例,其中还包括REPL输出 type abc = (Int, String) defined type alias abc scala> val item :

在类似的情况下

type abc=(Int,String)
val list=mutable.set[abc]()
我如何在列表中添加内容?类型为(Int,String)的东西看起来像什么?
我尝试过做类似于
list+=(5,“hello”)
的事情,但没有任何效果。

不完全确定您到底在寻找什么,但下面是一些向列表中添加abc类型的示例,其中还包括REPL输出

type abc = (Int, String)
defined type alias abc

scala> val item : abc = (1, "s")
item: (Int, String) = (1,s)

// i.e. Creating a new abc
scala> val item2 = new abc(1, "s")
item2: (Int, String) = (1,s)

scala> val list = List(item, item2)
list: List[(Int, String)] = List((1,s), (1,s))

// Shows an explicit use of type alias in declaration
val list2 = List[abc](item, item2)
list2: List[(Int, String)] = List((1,s), (1,s))

// Adding to a mutable list although immutable would be a better approach
scala> var list3 = List[abc]()
list3: List[(Int, String)] = List()

scala> list3 = (5, "hello") :: list3
list3: List[(Int, String)] = List((5,hello))

// Appending abc (tuple) type to mutable list
scala> list3 = list3 :+ (5, "hello")
list3: List[(Int, String)] = List((5,hello), (5,hello))

不完全确定您到底在寻找什么,但这里有一些示例,可以向列表中添加一种类型的abc,其中还包括REPL输出

type abc = (Int, String)
defined type alias abc

scala> val item : abc = (1, "s")
item: (Int, String) = (1,s)

// i.e. Creating a new abc
scala> val item2 = new abc(1, "s")
item2: (Int, String) = (1,s)

scala> val list = List(item, item2)
list: List[(Int, String)] = List((1,s), (1,s))

// Shows an explicit use of type alias in declaration
val list2 = List[abc](item, item2)
list2: List[(Int, String)] = List((1,s), (1,s))

// Adding to a mutable list although immutable would be a better approach
scala> var list3 = List[abc]()
list3: List[(Int, String)] = List()

scala> list3 = (5, "hello") :: list3
list3: List[(Int, String)] = List((5,hello))

// Appending abc (tuple) type to mutable list
scala> list3 = list3 :+ (5, "hello")
list3: List[(Int, String)] = List((5,hello), (5,hello))

我发现现有的答案令人分心。它没有解释问题是什么,这只是因为括号被解释为参数括号,而不是元组括号。看这里:

scala> list+=(5,"hello")
<console>:10: error: type mismatch;
 found   : Int(5)
 required: abc
    (which expands to)  (Int, String)
              list+=(5,"hello")
                     ^
<console>:10: error: type mismatch;
 found   : String("hello")
 required: abc
    (which expands to)  (Int, String)
              list+=(5,"hello")
                       ^

scala> list+=(5 -> "hello")
res1: list.type = Set((5,hello))

scala> list+=((5,"hello"))
res2: list.type = Set((5,hello))
scala>list+=(5,“你好”)
:10:错误:类型不匹配;
发现:Int(5)
必填项:abc
(扩展为)(Int,String)
列表+=(5,“你好”)
^
:10:错误:类型不匹配;
找到:字符串(“你好”)
必填项:abc
(扩展为)(Int,String)
列表+=(5,“你好”)
^
scala>list+=(5->“你好”)
res1:list.type=Set((5,你好))
scala>list+=((5,“你好”))
res2:list.type=Set((5,你好))
第一次失败是因为您使用两个参数调用方法
+=
,而不是使用一个元组参数调用它

第二次是有效的,因为我使用了
->
来表示元组

第三次是有效的,因为我放了额外的元组括号来表示元组


也就是说,将
集合
称为
列表
是不好的,因为人们倾向于认为它是一个
列表

,我发现现有的答案让人分心。它没有解释问题是什么,这只是因为括号被解释为参数括号,而不是元组括号。看这里:

scala> list+=(5,"hello")
<console>:10: error: type mismatch;
 found   : Int(5)
 required: abc
    (which expands to)  (Int, String)
              list+=(5,"hello")
                     ^
<console>:10: error: type mismatch;
 found   : String("hello")
 required: abc
    (which expands to)  (Int, String)
              list+=(5,"hello")
                       ^

scala> list+=(5 -> "hello")
res1: list.type = Set((5,hello))

scala> list+=((5,"hello"))
res2: list.type = Set((5,hello))
scala>list+=(5,“你好”)
:10:错误:类型不匹配;
发现:Int(5)
必填项:abc
(扩展为)(Int,String)
列表+=(5,“你好”)
^
:10:错误:类型不匹配;
找到:字符串(“你好”)
必填项:abc
(扩展为)(Int,String)
列表+=(5,“你好”)
^
scala>list+=(5->“你好”)
res1:list.type=Set((5,你好))
scala>list+=((5,“你好”))
res2:list.type=Set((5,你好))
第一次失败是因为您使用两个参数调用方法
+=
,而不是使用一个元组参数调用它

第二次是有效的,因为我使用了
->
来表示元组

第三次是有效的,因为我放了额外的元组括号来表示元组


也就是说,调用
集合
一个
列表
是不好的,因为人们会认为它是一个
列表

谢谢这就是我一直在寻找的,我不断地遇到像你的例子中那样的错误,不确定如何消除它们。谢谢,这就是我一直在寻找的,我不断地犯错误,就像你的例子一样,不知道如何消除它们。