Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
Both tuples and list are accepting the different type values as attached in snippet, then what is the main difference?
Scala is a typed language. You want your program to know as much as possible about the shape of the data it is working with.
List[Any] is not a very useful type. It does not tell you that there are three elements in there, and what their respective types are.
(Int, Double, String) tells you much more.
Lists are for collections of elements of all the same type (but unknown number).
Tuples are for the combination of a fixed number of elements (that can each have their own type).
Tuples can also be seen as ad-hoc versions of case classes (which you have to define first, but then give you named fields, and methods and all that):
val myData = FooDataRecord(id = 1, amount = 1.1, name = "a")
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 last year.
Improve this question
Is a trait a good way to put together all the constants that will be used by certain classes?
Or is there maybe a better way to handle constants, like a package object?
Singleton objects can contain fields, like any other object. If a constant exists on its own, it makes sense to put it in a singleton. Don't make a singleton just for all constants, but if they're logically grouped together, then you could pretty easily make an object with a good name.
object Calendar {
val DaysInYear = 365
val DaysInWeek = 7
val EpochYear = 1970
// ... etc ...
}
Package objects are deprecated in Scala 3 and set to be removed in the future. If you're already using Scala 3, then you can just put individual constants at the top-level of a file. Otherwise, throwing them in a singleton is the most future-proof way to code right now.
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 2 years ago.
Improve this question
I have a list of Swift Data Types, and I want to know if I missed any.
Here is my list:
Int
Double
Bool
String
Character
Optional
Any help appreciated.
This is absolutely, completely wrong. The five categories of types are:
Class
Struct
Enum
Tuple
Closure
Int, Double, Bool, String and Character are structs, and Optional is an Enum. But if these are the ones you count as type, then each of the five type categories can contain an infinite number of types.
You got a link to a tutorial site that supposedly listed all built-in types. None of these types are built into the language. Many are built into the Swift library. There is syntactic sugar for "Optional" built into the language, but in reality the various "Optional" types are just Enums with two cases "none" and "some".
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I'm trying to make a chatbox in scala. When a user will type anything, I want to stack those in a list.
But when I tried to do list += message
It's saying list is a list of string , and messgae is a string. So, it's not adding the string in the list actually.
Is there anyway to push the string in the list? Like in python u can do append??
You have two choices.
A mutable ListBuffer[String] ...
val ml = collection.mutable.ListBuffer("one","six")
ml += "ten" //res0: ml.type = ListBuffer(one, six, ten)
... or an immutable List[String] referenced via a mutable variable.
var il = List("two","four")
il = il :+ "five" //il: List[String] = List(two, four, five)
(You could combine them, a mutable variable holding a mutable collection, but that's a bad idea X 2.)
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Newbie to Scala.
I encountered this error while compile the code.
Error:(84, 130) type mismatch;
found : String
required: Array[String]
val mappingStr = "Mapping Strings: \n" + stringIndexers.map(r=>Array(r.getInputCol, r.labels.mkString(", "))).reduce(_+"\n"+_.mkString(": \n")) + "\n"
^
the hat char points at "Array" of my code.
I didn`t see any issue, can anyone help to explain why?
You map a list of some items into a list of Array[String], because it's what Array() apparently returns for each element of stringIndexers.
Then you try to reduce this List[Array[String]] by +-ing Strings. This expects _ in reduce to be a String, but it is not, it's an Array[String].
You should provide a way to convert your arrays into strings, or maybe flatten your list of arrays first, it's hard for me to tell which your intention is.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
What is the difference between asInstanceOf[] and isInstanceOf[]?
Generally speaking, a.asInstanceOf[B] performs the actual cast: it takes an object of type A and returns (if possible) object of type B, whereas a.isInstanceOf[B] returns boolean indicating whether a has type B or not.
Strictly speaking isInstanceOf[B] looks not only if a is of type B, but if a has type B in the upper side inheritance tree (so if B superclass of a, isInstanceOf yield true) and important notice is that isInstanceOf works on the actual object type hierarchy rather on the reference type.
I'd just like to add that the common pattern
if (x.isInstanceOf[B]) {
val b = x.asInstanceOf[B];
...
} else ...
can be written nicely as
x match {
case (b: B) => ...
...
}
This is especially useful if there are multiple tests of this kind for a single x.