Is there an equivalent of Nil for Set in scala?
I tried using Nil as a value for Set, but I got an error (expected since the type of Nil is List)
Thanks
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Set.emptyis that set; although you can’t get at it directly, it turns out that it is just a private object in theSetcompanion object (called, obviously enough,EmptySet). All thatSet.emptydoes is return that set with a cast to the correct type.It is done this way, instead of with
Nil, because sets are invariant in their parameters.NilisList[Nothing](), but you couldn’t add anything to aSet[Nothing]().If you need to specify the type of your empty set, you can use e.g.
Set.empty[String].