0

I'm pretty new to Scala and I've hit my first hurdle ...

java.nio.file.Paths has this method:

public static Path get(String first, String ... more)

This java code compiles (and runs):

String[] tokens = new String[] { "/home", "toby", "mydir" };
Path path = Paths.get(tokens[0], Arrays.copyOfRange(tokens, 1, tokens.length -1));

However this scala code does not compile:

import collection.JavaConversions._
...
val tokens = Array("/home", "toby", "mydir")
val path = Paths.get(tokens(0), tokens.tail)

The error I get is "type mismatch; found : Array[String] required: String"

What am I missing? Thanks

1 Answer 1

2

Paths.get does not want an array as the second parameter, String... more is the varargs notation.

Try:

val path = Paths.get(tokens.head, tokens.tail: _*)
// path: java.nio.file.Path = /home/toby/mydir

Look at this question for more explanation on _*.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I'll take a look. This is something specific to Scala right? I mean varargs are just syntatic sugar over arrays so in Java I can pass an array