score:2
there's a difference between mutable data structures and mutable references - see this answer for details.
in this particular case, you're using mutable reference to immutable data structure, which means you can only replace it with completely different one. this would work:
var pair = (99, "luftballons")
println(pair._1) // ok
pair = (100, "luftballons") // ok
as others already pointed out there's a convenience method copy
defined for tuple
, which allows creating a copy of an object (potentially replacing some fields).
pair = pair.copy(5, "kittens") // ok
score:1
you have to update your pair like this:
pair = (89, pair._2)
pair: (int, string) = (89,luftballons)
by a new assignment to the pair, not to the underlying tuple. or you use pair.copy, like suggested by chengpohi.
scala> pair = pair.copy(_1=101)
pair: (int, string) = (101,luftballons)
Source: stackoverflow.com
Related Query
- Use of def, val, and var in scala
- Are there any methods included in Scala to convert tuples to lists?
- Why are the indexes of Scala tuples 1-based?
- Scala transform list of tuples to a tuple of lists
- Writing functions of tuples conveniently in Scala
- Why does the Scala library only defines tuples up to Tuple22?
- Scala overriding a non-abstract def with a var
- initialise a var in scala
- Why can't I assign to var in Scala subclass?
- Add two tuples containing simple elements in Scala
- make a lazy var in scala
- Scala List of tuples to flat list
- Scala collections: why do we need a case statement to extract values tuples in higher order functions?
- Looping through a list of tuples in Scala
- fold list of tuples in scala with destructuring
- Parse error of nested tuples in scala
- How to merge tuples by same elements in Scala
- Higher order operations with flattened tuples in scala
- Why does this Scala for expression using tuples fail to compile?
- Scala closures compared to Java innerclasses -> final VS var
- map error when applying on list of tuples in scala
- Is there a scala list operation that makes tuples from lists?
- Working with tuples in Scala
- Scala - How to convert from List of tuples of type (A,B) to type (B,A) using map
- Scala Immutable Set is Mutable when declaring as a var
- Scala immutable Map vs List of tuples
- Generic getter method for tuples in Scala which preserves dynamic type?
- Passing a list of tuples as a parameter to a spark udf in scala
- How do I convert the following var Scala code into vals?
- Folding a list of tuples using Scala
More Query from same tag
- Scala Play 2.5 How to make Java's Context/Session/Request accessible to a view?
- type parameterization of Scala
- Spark - Reading many small parquet files gets status of each file before hand
- Can anyone suggest a Java or Scala DOS/terminal-based UI framework?
- JMH Micro Benchmark Annotations with Scala
- setting parameters from working curl to gatling parameteres
- Error creating repository bean with Spring
- CSV Feeders for gatling 3
- Scala Strange Error with Partial Functions
- scala: how to explicitly ask for an implicit conversion?
- Batch operations with akka persistance, what options are there?
- Decode Base64 within Spark Dataframe using Scala
- Difference between Apache spark mllib.linalg vectors and spark.util vectors for machine learning
- Converting blocking code to using scala futures
- JSON format for Single property case classes
- How to convert a shell script (bash) to Scala code?
- How to convert Array[String] into spark Dataframe to save CSV file format?
- Subproject dependencies in SBT
- How can I flatten this Future[T] structure?
- How to chain operations in idiomatic scala
- Scala doesn't cast Element inside loop to yield value
- Apache Spark: Building file/string in reduce function
- Scala: More Efficient Way to Filter a List and Create a Sequence of Futures
- Is possible for a worker to be a coordinator and send to other workers in Spark?
- There is "Run Configuration" but no "Run As Scala Application" in Eclipse
- Call scala method from object dynamically
- Issue with headers on scala from a csv file
- Scala - Abstract types and Implicit Parameter Resolution
- Relation of free monad and AST
- Spark "ERROR EndpointWriter: AssociationError" when driver's port and address are set manually