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
Related
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 to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
Found this stepik task and have no idea what to do:
Write a function which gets a Map[Char, Int] as an input and makes a Set[LetterTree] (LetterTree is the case class below), in which from each (c,w) map entry, we make a LetterLevel(c,w), then we then put it in the said Set[LetterTree].
trait LetterTree {
def weight: Int
}
case class LetterLevel(ch: Char, weight: Int) extends LetterTree
object TreeBuild extends App {
def TreeSetBuild(freqs: Map[Char,Int]): Set[LetterTree] = ???```
}
You could use tupled to construct LetterLevel instances from the tuple elements of the Map:
object TreeBuild extends App {
def treeSetBuild(freqs: Map[Char,Int]): Set[LetterTree] =
freqs.map(LetterLevel.tupled).toSet
}
TreeBuild.treeSetBuild(Map('a'->1, 'b'->2, 'c'->3))
// res1: Set[LetterTree] = Set(LetterLevel(a,1), LetterLevel(b,2), LetterLevel(c,3))
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 4 years ago.
Improve this question
I want to put the print behind
like
"foo" match {
case str => println(str)
}
or
List("foo") foreach print
Are there other best one?
like in Haskell
return "foo" >>= print
so if you just want to have somewhat haskell like semantics you can try the cats or the scalaz libraries. both have IO monads (cats in cats-effects) that would allow you to use the print in a principled manner as in your last example. If you don't care about the purity aspect you could also achieve this with the Id Monad which also both libraries offer.
You can do this:
case class Print(v: String) {
def print = Predef.print(v)
}
object Print {
implicit def stringToString(s: String) = new Print(s)
}
import Print._
object App extends App {
"foo".print
"bar" print
}
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
I want to pass type-parameters into Scala Collections like Seq, Array, etc.
For example,
val a: Seq[Int]; // it's easy to specify a type parameter "Int"
val b: Seq[xxxxxxx]; // I want to receive a type of Seq from other like functions parameters or any other variables in runtime.
is it possible in Scala ?
Actually I want to do this...
def fun( arr1 : Any, arr2 : Any, arr3: Any ) {
val seq: Seq[???] = Seq(arr1, arr2, arr3);
myOwnLibrary(seq); // Seq[Any] is not available as a myOwnLibrary's parameter;
}
Not sure but possibly this is what are you trying to do:
scala> def mkArray[T : ClassTag](elems: T*) = Array[T](elems: _*)
mkArray: [T](elems: T*)(implicit evidence$1: scala.reflect.ClassTag[T])Array[T]
scala> mkArray(42, 13)
res0: Array[Int] = Array(42, 13)
scala> mkArray("Japan","Brazil","Germany")
res1: Array[String] = Array(Japan, Brazil, Germany)
http://www.scala-lang.org/api/current/scala/reflect/ClassTag.html
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.