1

In limited sense it is very easy to write out and ref classes on your own, but my question is not how to do it -- but are there some features (or classes) ready to use?

The closest thing I found is Reference trait (but it is a trait).

I need those, not tuple, not Option, and not Either as pure result, because only ref/out makes chaining ifs elegant.

greenoldman
  • 16,895
  • 26
  • 119
  • 185
  • 5
    Could you please provide some sample code to illustrate which chaining you are are trying to archive? Perhaps the answers to a another question help: http://stackoverflow.com/questions/6896118/basic-scala-oop-question-pass-by-reference – Augustus Kling Jan 11 '12 at 21:47
  • The simplest example is `if (foo(ref/out bar)) return bar`. Thank you very much for the link, I don't think I will use setter, rather wrapper, but anyway this was interesting piece. +1 – greenoldman Jan 12 '12 at 06:15

1 Answers1

5

No, Scala supports parameter passing by value or by name. Parameter passing by reference is actually quite difficult to accomplish correctly in the JVM, which is probably one reason why none of the popular JVM languages have it. Additionally, out and ref parameters encourage programming via side-effect, something the at design of Scala attempts to avoid wherever possible.

As for chaining of if's, there are a variety of ways to achieve some effects like that in Scala. "match" expressions are the most obvious, and you might also look into monadic compositions using Option or Either.

Dave Griffith
  • 20,435
  • 3
  • 55
  • 76
  • Hmm, "match"? How would you use it to mimick ref/out? – greenoldman Jan 12 '12 at 06:24
  • It wouldn't be to mimic ref or out directly, but rather to allow a construct to both test for a condition and bind one or more variables. I assumed you were trying to have conditional checks which also bound appropriate variables for use in the condition body, assuming the condition passed. This sort of destructuring test is what pattern matching is perfect for. To include arbitrary logic in a pattern match, look into either gaurds (for simple cases) or extractors (for full power). – Dave Griffith Jan 12 '12 at 13:51