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 Answers1

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 _*.

Community
  • 1
  • 1
Peter Neyens
  • 9,770
  • 27
  • 33
  • 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 –  Jul 14 '15 at 16:42