简体   繁体   中英

Scala type mismatch when method return type is added

I have a method that runs when there is no specified return type, but as soon as the return type is added, a mismatch error is given. The return type of the returned value is the same.

This works fine:

def get_csv_page(url: String){
    scala.io.Source.fromURL(url).getLines.drop(1).toList
 }

Returns:

res2: List[String]

But adding : List[String] causes mismatch:

def get_csv_page(url: String) = List[String]{
    scala.io.Source.fromURL(url).getLines.drop(1).toList
}

:12: error: type mismatch;
 found   : List[String]
 required: String
        scala.io.Source.fromURL(url).getLines.drop(1).toList
                                                             ^

This is a syntax error, it should be a : to specify the return type and then an = :

def get_csv_page(url: String): List[String] = {
    scala.io.Source.fromURL(url).getLines.drop(1).toList
}

What you have right now is trying to construct a List literal, which is why it is wanting you to provide a single String for an element of your list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM