Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
According to this, Scala case classes automatically generates an equality method. However:
class SomeCaseClass(val string:String) {}
val a = "123"
assertTrue( a.equals( a ) ) // Passes
assertTrue( new SomeCaseClass(a).equals( new SomeCaseClass(a) ) ) // Fails, Scala 2.10
So, what does this automatically generated equals method do?
It's not a case class. That's the case class:
scala> case class SomeCaseClass(string: String)
defined class SomeCaseClass
scala> val a = "123"
a: String = 123
scala> SomeCaseClass(a) == SomeCaseClass(a)
res1: Boolean = true
== is just a syntax sugar for equals
In your example, you've just called equals (you'll get same result with ===) on regular object (this equals is not automatically generated), so it checked just referential equality.
P.S. You may notice that case class doesn't require new as it has automatically generated companion object. It doesn't require val in constructor as it assumes it by default. {} isn't mandatory for both regular and case classess.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 11 months ago.
Improve this question
I have a piece of code implemented in 2 ways:
val c = if(x.y.isDefined){
methodA(....)
} else {
method(....)
}
c
And this:
val c = x.y.
map(methodA(....))
.orElse(Some(mehodB(....))
.getOrElse("")
c
x.y is an Option and methodA, methodB return String values.
Which of these approaches is more preferred in Scala? Personally I find first one to be more easy to understand, but my more scala proficient colleague prefers second one.
use the if or something based on pattern match (by the way, no need for assigning to c if you return the expression)
x.y match {
case Some(_) => methodA()
case None => methodB()
}
x.fold(methodB) { _ => methodA }
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
How to convert iterator of a case class to another case class and convert it to list.
Simply converting iterator into list using .toList does not work in scala 2.12
Error : value toList is not a member of Object
case class student(id int, name string)
case class studentNew(studentId int, name string)
val i: Iterator[student] = inputRec
val studentList: List[studentNew] = inputRec.map (s => studentNew(s.id, s.name))
// To convert to case class of id/name to studentId/name and make a list
version scala:2.12
This is a working snippet for 2.12
case class student(id: Int, name: String)
case class studentNew(studentId: Int, name: String)
val inputRec: Iterator[student] = ???
val studentList: List[studentNew] = inputRec.map (s => studentNew(s.id, s.name)).toList
You cant assign an Iterator to a List.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
I have a function that looks like:
case class User(name: String)
val users = List(User("a"), User("b"), User("c"))
val bannedUsers = List(User("b"))
def processUsers(
users: Set[User],
bannedUsers: Set[User]
): Set[Either[(String, Int), Boolean]] = {
users.map { u =>
verifyUsers(users, bannedUsers).flatMap { verifiedUsers =>
verifiedUsers.name match {
case "a" => Right(true)
case "b" => Right(true)
case "c" =>
Left(("hello", 3))
}
}
}
}
def verifyUsers(
users: Set[User],
bannedUsers: Set[User]
): Either[String, User] = {
val n = util.Random.nextInt(users.size)
Right(users.iterator.drop(n).next)
}
println(processUsers(users.toSet, bannedUsers.toSet))
I am getting a compile time error saying:
type mismatch; [error] found : scala.util.Either[Product with
java.io.Serializable,Boolean]
The types seem to match in my view, what is wrong?
castie: https://scastie.scala-lang.org/z1bBfOMpRPyV88VekNdz6w
You should do Left(str -> user) or Left((str, user)), Scala deprecated auto-tupling arguments since it caused too many errors in practice so it might cause errors.
You probably messed up the return type of a previous pat of code e.g. have some Right(User(1)) earlier in flatMap before doing Left(...) in else. Case classes (and tuples) implement scala.Product and java.io.Serializable, so inferring this is a sign of using bad values.
flatMap in Either changes the type of Right so you most likely did something like Right(value: User).flatMap(... => Left((str, user))) forcing compiler to infer common type of e.g. User and (User, String).
Long story short adjust the left type of method2 before flatMap or use some leftmap.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
what is the type of this object?
class Zad1[A,B](val fst:A, val snd:B) {
override def toString: String = "(" + fst +","+snd+")"
}
object Zad1 {
def main(args: Array[String]): Unit = {
val v = new Zad1[Int, String](1, "2")
println(v)
}
}
I tried to print the class name with :
println(v.getClass) // would print: class $line8.$read$$iw$$iw$Zad1
The type of a singleton object is its singleton type, ergo, the type of Zad1 is Zad1.type.
This is related to how Scala REPL works.
Though you type just:
scala> class Zad1[A,B](val fst:A, val snd:B) {...}
REPL wraps it into a series of other objects($line8.$read.$iw.$iw), so getClass returns class $line8.$read$$iw$$iw$Zad1.
Read about it here:
Trying to understand how classes declared on the REPL are treated internally
If you run the same as a Scala program(not from REPL), getClass will return something much readable, e.g. class com.example.Zad1
This question already has answers here:
Scala foreach strange behaviour
(5 answers)
Closed 7 years ago.
Given these case classes:
case class FeatureDistance(id: Long, distance: Double)
case class SearchResult(score: Float, id: Long)
Why does this not compile?
val distances = List[FeatureDistance](FeatureDistance(1L, 10f))
val results = distances.map(SearchResult(0f, _.id))
But this does:
val results = distances.map(fd => SearchResult(0f, fd.id))
The compilation error says: missing parameter type for expanded function ((x$3) => x$3.id)
Is it because _ is only scoped to the map function so it's not visible in the SearchResult.apply call?
After doing a bit of research, I found a post on the old scala forums that contains this quote:
When you use "_" as a place holder for an anonymous parameter of a function, the scope of that function is the innermost parenthesis containing it.
So, it's just a question of scope. I suspect this has to do with problems that could otherwise result from having nested function calls that use more than one underscore. For instance:
//suppose we have some x:List[List[Int]]
x.map(_.map(_ + 1))