Variable list with two variables

I need something like this:

class Node (left : Node*, right : Node*) 

I understand the ambiguity of this signature.

Is there a better way around this than the following?

 class Node (left : Array[Node, right : Array[Node]) val n = new Node (Array(n1, n2), Array(n3)) 

Maybe some kind of delimiter like this?

 val n = new Node (n1, n2, Sep, n3) 
+4
source share
3 answers

You can have multiple argument lists, each of which can have (or just have) one repeat-args parameter:

 scala> def m1(ints: Int*)(strs: String*): Int = ints.length + strs.length dm1: (ints: Int*)(strs: String*)Int scala> m1(1, 2, 3)("one", "two", "three") res0: Int = 6 

I ran this in Scala 2.8 REPL. I do not know why it does not work in 2.7, carelessly.

+8
source

It works:

 class Node (left : Node*) (right : Node*) 

Scala is great!

+6
source

I do not believe that you can have multiple varargs. Perhaps you could do something like

 class Node(left: Node*) { def apply(right: Node*) = ... 

and then you can create new Node(n1,n2)(n3)

+1
source

Source: https://habr.com/ru/post/1309220/


All Articles