1

Basically, my question is if the equivalent of this C++ code possible in Scala?

int x = 1;
int& rx = x;
rx++; // x = 2 now
std::cout << x;

I.e. to have two different variables both point to the same thing (and both change, when one of them is changed)?

Also, if it is, can a scala function return such a reference?

2
  • It would be helpful if you could explain what you are trying to do instead of just showing the C++ code. As it stands now, answering your questions requires expertise in both C++ and Scala, even though C++ is completely irrelevant to the problem and the answer and is only needed to understand what you are actually asking. In particular, my limited knowledge of C++ actually conflicts with your description since I think you are modifying the value but in your text you talk about modifying the variable, which I don't actually see in your code. Commented Apr 2, 2018 at 12:35
  • Do you actually have a good reason to reference mutable state? Think twice if this is what you want to do. Commented Apr 2, 2018 at 12:48

3 Answers 3

1

Well you can't. In Scala, Int, Double, Float, Boolean etc are AnyVal, you can't pass them by reference. You can pass references of AnyRef i.e. String, List, Set, Any user defined object etc.

Sign up to request clarification or add additional context in comments.

2 Comments

Hmm, so if I had an object, say class Node(data: Int, next: Node); and then did var p = new Node(1, null); var q = p; q = q.next; Now p remains the same, but q points to null Is it possible to have p and q always point to the same thing, and thus make changing q, change p as well?
Precisely, yes.
1

You can sort of emulate it:

class Ref[A](var value: A)
object Ref { def apply[A](a: A) = new Ref(a) }

val a = Ref(1)
val b = a
b.value += 1
println(a.value) // 2

Or even:

class Ref[A](get: => A)(set: A => Unit) {
  def value = get
  def value_=(a: A): Unit = set(a)
}

var a = 1
val b = new Ref(a)(a = _)
b.value += 1
println(a) // 2

You could probably write a macro to make constructing a Ref a bit prettier in this case (e.g. Ref(a) and the macro generates the appropriate closures).

Comments

0

The usual workaround in Java/Scala that should seem somewhat familiar to the C/C++ programmers is the following:

val x = Array[Int](41)
val rx = x
rx(0) += 1
println(x(0))

You cannot have references or pointers to primitive integer variables, but arrays kind of contain information equivalent to a pointer, so if you declare x to be an Array[Int] with a single element instead of an Int, you get roughly the same behavior: the above example will print 42.

You can think of it like this:

  • Array[X] roughly corresponds to X[] in C, which in turn roughly corresponds to X*
  • x(0) corresponds to accessing the value at the "pointer" x, so it's something like *x in C.

The crucial difference is that you can only declare an array right from the beginning. There is no way to get a "pointer" to an arbitrary location in memory.

Note however that the array will reside on the heap (usually, unless it is eliminated by some escape-analysis), you cannot keep it in the stack frame of a function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.