Why is the order of declaration important?


#1

So I got this confusing error:

object StringUtil { 
	def joiner(strings: List[String], seperator: String): String = strings.mkString
	def joiner(strings: List[String]): String = joiner(strings, " ") 
}

import StringUtil._ 

println(List(joiner("super", "cali", "fragelistic")))  

At first glance it seems ok, BUT:
overloaded method value joiner with alternatives:
(strings: List[String])String
(strings: List[String],seperator: String)String
cannot be applied to (String, String, String)

the problem lies in the last line:
println(joiner(List(" … ")

I cannot quite fathom, nor explain how the order is important, or how scala handles these types.
Perhaps the reasone lies under the functional paradigma (considering(we have(all those(parantheses))) :slight_smile:

I get the same error message, as if I would forget a " : ". :confused:


#2

Your last line is not a declaration, but only function calls, that also means you don’t have types there. Let’s take apart that last line:

println( //print everything inside these parentheses
  List( //this is the list constructor, i.e. make a list with everything inside the parentheses as elements 
    joiner("super", "cali", "fragelistic")  // call the `joiner` function with three strings.
  )
)  

So you don’t pass a list to your function, but create a List from its result.


#3

Such a line doesn’t exist in your example.


#4

water is wet


#5

Well, there is the first part of your solution: You didn’t write what you think you wrote. If that’s so obvious to you to become flippant about it, maybe it’s a good idea not to do that.


#6

I honestly don’t understand what is supposed to be confusing here. You don’t understand how joiner(List(a,b,c)) is different than List(joiner(a,b,c))?


#7

This seems to work:
object TestString extends App {
val strs = List(“super” , “cali”,“fragelistic”)
println(List(joiner(strs)))
}