문제

I'm having hard time trying to get nth element from the varargs sequnce in scala. Here's my code

def foo(args: String*) = args.toArray(1)

I receive error like:

error: type mismatch;
found   : Int(1)
required: scala.reflect.ClassTag[?]
     def foo(args: String*) = args.toArray(1)

What's interesting, code like this works great:

def foo(args: String*) = args.toArray.apply(1)

I'm pretty new to scala but I thought that it should be exactly the same. Is using apply right way to select nth element from vararg seq?

도움이 되었습니까?

해결책

You can call apply on the input argument directly:

def foo(args: String*) = args(1)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top