Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala - mutable (var) method parameter reference

EDIT: I keep getting upvotes here. Just for the record, I no longer think this is important. I haven't needed it since I posted it.

I would like to do following in Scala ...

def save(srcPath: String, destPath: String) {     if (!destPath.endsWith('/'))         destPath += '/'     // do something } 

... but I can't beacuse destPath is a val. Is there any way to declare destPath as var?

Note: there are similar questions but in all of them OP just wanted to modify array.

Please do not advise following:

Mutating the input parameters is often seen as bad style and makes it harder to reason about code.

I think it's valid in imperative programming (Scala allows both, right?) and adding something like tmpDestPath would just add clutter.

EDIT: Don't misunderstand. I know that strings aren't mutable and I don't want a reference to reference because I don't want to modify data of caller. I just want to modify local reference to string that caller gave me with my string (eg. orig + '/'). I want to modify that value only in scope of current method. Look, this is perfectly valid in Java:

void printPlusOne(int i) {     i++;     System.out.println("i is: " + i);     System.out.println("and now it's same: " + i); } 

I don't have to create new variable and i don't have to compute i+1 twice.

like image 654
woky Avatar asked Mar 02 '12 15:03

woky


People also ask

Is VAR mutable in Scala?

Mutable Variable: These variables are those variables that allow us to change a value after the declaration of a variable. Mutable variables are defined by using the var keyword. The first letter of data type should be in capital letter because in Scala data type is treated as objects.

Is Scala pass by reference?

In Java and Scala, certain builtin (a/k/a primitive) types get passed-by-value (e.g. int or Int) automatically, and every user defined type is passed-by-reference (i.e. must manually copy them to pass only their value).

Why is scal preferred in Var Scala?

Favoring val over var increases immutability of the codebase which can facilitate its correctness, concurrency and understandability. I think Scala didn't take immutability to its final conclusion: constant pointer and constant data. Scala missed an opportunity to make objects immutable by default.

How do you pass variables in a Scala function?

Scala allows you to indicate that the last parameter to a function may be repeated. This allows clients to pass variable length argument lists to the function. Here, the type of args inside the print Strings function, which is declared as type "String*" is actually Array[String].


1 Answers

You can't.

You'll have to declare an extra var (or use a more functional style :-)).

Simplistic example:

def save(srcPath: String, destPath: String) {     val normalizedDestPath =       if (destPath.endsWith('/')) destPath       else destPath + '/'     // do something with normalizedDestPath  } 
like image 116
Jean-Philippe Pellet Avatar answered Sep 18 '22 18:09

Jean-Philippe Pellet