1

is there a way to create an alias for a scala keyword? in particular i have some boilerplate syntax that involves "val" and in order to make it easier to read i'd like to be able to type something "@@" instead and have that translated to val.

Edit:

In some cases, it might be very convenient to be able to replace "lazy val", not just "val". The use case has to do with a function that acts as a python decorator. It looks like this:

lazy val function = Decorate(function_ _)
def function_(x: Int, ...) = { ... }

it would be a lot nicer if it looked like this:

@ function = Decorate(function_ _)
def function_(x: Int, ...) = { ... }

just so that there's not a val stacked on top of a def, where both names are extremely similar. (the function_ name is not meant to be called, so it's the cleanest to make the names similar.)

Aaron Novstrup
  • 20,967
  • 7
  • 70
  • 108
Heinrich Schmetterling
  • 6,614
  • 11
  • 40
  • 56
  • It would be useful to have more details of the use-case. @@ is only one character shorter and not obviously easier to read. It sounds like something a text preprocessor could handle if you really need it. – The Archetypal Paul May 05 '11 at 07:20
  • @paul agreed, updated with an edit. thanks! – Heinrich Schmetterling May 05 '11 at 09:32
  • Related questions: [scala: memoize a function no matter how many arguments the function takes?](http://stackoverflow.com/questions/5875767/scala-memoize-a-function-no-matter-how-many-arguments-the-function-takes) and [scala: trait for a Function object with variable length arguments?](http://stackoverflow.com/questions/5798045/scala-trait-for-a-function-object-with-variable-length-arguments) – Aaron Novstrup May 05 '11 at 18:41
  • I edited out a redundant `val` keyword in your post. If it was there intentionally, note that the code would not have compiled. – Aaron Novstrup May 05 '11 at 18:54

4 Answers4

8

No, there isn't.

(filler so SO will let me post)

Daniel C. Sobral
  • 295,120
  • 86
  • 501
  • 681
1

Ouch! This isn't particularly idiomatic Scala.

To start with, you're naming a method "function_", they're not the same thing, a method is simply a member of some class, a Function is an object in its own right (although a method can be "lifted" to a function by the compiler, in a similar fashion to the autoboxing of primitives).

Second, what is Decorate? The initial uppercase letter suggests that it's a singleton, therefore an object and the only actual "Function" in that expression!

Could you post a bit more info as to what the method and decorator actually do, so that I can give you a better example as to how you might achieve the same in Scala?

Kevin Wright
  • 49,540
  • 9
  • 105
  • 155
  • Guessing from the poster's previous line of questions, `Decorate` is something like the `Memoize` object at the bottom of [this answer](http://stackoverflow.com/questions/5875767/scala-memoize-a-function-no-matter-how-many-arguments-the-function-takes/5877235#5877235) – Aaron Novstrup May 05 '11 at 18:50
  • Even `Decorate` is not really a function: its `apply` method has type parameters and an implicit argument. However, I suspect that the name of the method isn't really critical to the poster's question. It might just as well be `foo_` with an associated `foo` function. – Aaron Novstrup May 05 '11 at 19:24
  • @aaron - True, though I feel that a failure to appreciate the distinction, and lack of knowledge of methods such as `compose` on functions, coupled with the need to suffix `_` after a method name to force partial application are all contributing factors to the OP's problem here. – Kevin Wright May 05 '11 at 20:51
0

I guess one could write a Scala Compiler Plugin to achieve this. At least the Eclipse Plugin actually uses the original Scala Compiler, so it might actually work nicely with the IDE.

Other then that: Daniel C. Sobral is correct: No, there isn't.

Still it sounds like a lot of trouble for a little gain.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
0

If function_ is never meant to be called directly, why not write

lazy val function = Decorate { (x: Int, ...) => ... }

or even

/**
 * This version makes it more explicit that it's a function value.
 */
lazy val function: (Int, ...) => ReturnType =
   Decorate { (x, ...) => ... }

Some caution is advised: conciseness and terseness are two different things. Here, it looks like you're trying to buy a few keystrokes at a very high price in terms of readability.

Update:

If you really want to achieve a simpler syntax for this sort of thing, you will need a compiler plugin. If you're going to go that far, I'd suggest using an annotations-based syntax that's likely to be pretty intuitive for Java/Scala developers:

@decorate(Memoize)
def slowFn(i: Int) = { ... }
Aaron Novstrup
  • 20,967
  • 7
  • 70
  • 108
  • you're right that moving Decorate inside "function" eliminates this need. the downside is that people then have to write functions using two different syntaxes - one with val, and the other with the cleaner "def fn(arg: ...)". that might hurt readability a lot. – Heinrich Schmetterling May 05 '11 at 21:02
  • I can see your point of view, but this is really an inherent issue with the language itself. You can only declare *functions* (or other values) with `val`, and you can only declare *methods* with `def`. Programmers will therefore have to understand both syntaxes to comprehend the language. – Aaron Novstrup May 05 '11 at 21:18