8

I have a method in Scala that returns a tuple, let's say the method is called 'test'. Then I want to do

val (X,Y) = test()

However, the pattern-matching only works in Scala when the variable names are lowercase, ie:

val(_X,_Y) = test(); val X = _X; val Y = _Y

... works ok, but is ugly, and not terse. Since X and Y are matrices, I don't really want to have to use lowercase variables. (In scipy and matlab, I wouldn't have such a restriction for example).

I think there is some way to make sure lowercase variables behave like uppercase ones, ie by doing `x`. Perhaps there is some way of making uppercase variables behave like lowercase ones? So, that is my question: is there some way of pattern matching directly into uppercase variables in Scala?

Hugh Perkins
  • 7,975
  • 7
  • 63
  • 71

2 Answers2

5

The short answer is don't.

Syntax conventions make your code readable and understandable for others. Scala's convention is that variables start with lower-case and constants and classes start with upper-case. By violating this, not only you get problems like pattern-matching issues, your code becomes less readable. (Believe me, if you ever have to read code written by someone else who didn't care for such conventions, you'll be cursing that person.)

If you want to emphasize that the variables are matrices, I suggest you to use xMatrix and yMatrix or something like that. This will make clear that they're variables and that they represent matrices.

Or create a convention specific to your project that all matrix variables will end with let's say "M", like xM and yM.

It's worth typing a few more characters if it makes your code readable.

Petr
  • 62,528
  • 13
  • 153
  • 317
  • 5
    I think there is something a little odd about a language that lets you use uppercase variable names for the first 20 hours of discovering the language, then suddenly for one little tiny, but useful, usage, it suddenly decides that, no, uppercase variables, not allowed! – Hugh Perkins Sep 28 '12 at 13:48
  • @HughPerkins I agree to that. And it gets even messier: Scala allows you to pattern-match on constants, which are defined as variables with upper-case names. Something like `val MyConst = "Text"; someString match { case MyConst => ...; ... }`. This causes a lot of confusion. The most sensible rule is to use upper-case for top-level constants only and use lower-case at all other places. – Petr Sep 28 '12 at 14:04
  • Ok, well, no-one has provided a positive answer to the question, so I will mark your answer as the answer for now, particularly as you provided some constructive ideas for how to work around the lack of being able to specify the type (constant vs variable) of a variable in Scala. – Hugh Perkins Sep 29 '12 at 11:31
  • @HughPerkins I'm afraid there is no positive answer (as also Kim Stebel mentions) - Scala's pattern matching determines constant/variable depending on its case. – Petr Sep 29 '12 at 13:34
  • I'm having an annoying time because all 한굴 characters are considered uppercase by Scala. – Owen Oct 08 '13 at 21:06
  • I also disagree with your interpretation of what Scala's case convention is. In a pattern match, letter case doesn't tell you what the identifier refers to (case class, variable, etc), it determines the identifier's syntactic role. This behavior is borrowed from Haskell. But it is by no means the only option; Scala could have, for example, decided to use `?x` for variable patterns. As for case convention for actual declarations, it is more like an ultra-thin version of Hungarian notation, which, in my opinion appropriately, Scala does not *ever* enforce (even in pattern matches). – Owen Oct 08 '13 at 22:23
1

There is no way to do this and there shouldn't be. You already have the type of the variable to tell you that it is a matrix, so there is no need to make variable names uppercase.

Kim Stebel
  • 41,826
  • 12
  • 125
  • 142
  • That might be true for general purpose software development(I'm not saying it is, but I'm not going to argue that it isn't) but I'm targeting usage for machine learning, where X means 'all data points', and x means 'a single data point'. This is standard notation, eg see Bishop, "Pattern Recognition and Machine Learning", page 141. Sure, one could shift the way one codes to match the language, but isn't the language there to make our life easier, not the other way around? scipy and matlab and java and c++ all allow the use of X as the name of a matrix. – Hugh Perkins Sep 28 '12 at 11:06
  • of course it is standard notation in math, just as x and xs are standard notation in scala. However, none of this changes the simple fact that what you want to do won't work. – Kim Stebel Sep 28 '12 at 11:16
  • Really I would argue that the variable should have a type to tell the compiler/runtime whether it is a constant or a variable. I'm not sure if using upper/lowercase to distinguish type is common in the FP world but it seems a little ... unusual... to me in the oo world. – Hugh Perkins Sep 29 '12 at 11:30