Search code examples
stringscalainitializationtuplesis-empty

Initialize an empty tuple scala


I want to create a mutable tuple in scala (String, Set[String]). And before I want to initialize it to an empty value first before adding to it

the set can not be initialized to empty set. The same for String. I'm getting an error saying class java.lang.String is not a value Is there a simple way to do that ? and What am I doing wrong


Solution

  • Empty Set can be defined like so

    Set.empty[String]   // because assert(Set.empty[String].isEmpty)
    

    Empty String can be defined like so

    ""                  // because assert("".isEmpty)
    

    Empty tuple is not something Scala models as TupleN, I think. Instead it uses Unit type for it. Perhaps you are after something like so

    val ta: (String, Set[String]) = ("", Set.empty)
    val tb: (Option[String], Option[Set[String]]) = (None, None)
    val tc: Option[(String, Set[String])] = None
    

    The error message

    class java.lang.String is not a value
    

    means you are using a type where value is expected, for example, consider the difference between

    Set[String]    // ok
    Set(String)    // error