This page is no longer maintained — Please continue to the home page at www.scala-lang.org

aliases

2 replies
barden
Joined: 2010-03-30,
User offline. Last seen 33 weeks 5 days ago.

Hi,

Small thing: Is it possible to use aliases for methods in the following sense:

class A(name: String) {
val x = 5
... Missing bit ...
}

enabling the usage

val a = new A("fingers")
println(a.fingers)

giving 5

Cheers,
Volker.

Sciss
Joined: 2008-12-17,
User offline. Last seen 28 weeks 5 days ago.
Re: aliases

you can only use method names that are defined _somewhere_, e.g. you could lift A to another type, a wrapper ("pimp") that has fingers. but you cannot have a general doesNotUnderstand fall-back like in smalltalk, unfortunately...

Am 28.10.2010 um 16:55 schrieb Bardenhorst, Volker Dr.:

> Hi,
>
> Small thing: Is it possible to use aliases for methods in the following sense:
>
> class A(name: String) {
> val x = 5
> ... Missing bit ...
> }
>
> enabling the usage
>
> val a = new A("fingers")
> println(a.fingers)
>
> giving 5
>
> Cheers,
> Volker.

ichoran
Joined: 2009-08-14,
User offline. Last seen 2 years 3 weeks ago.
Re: aliases
The syntax for what you want is

  val a = new A { def fingers = x }

which will create a custom class which has x aliased to fingers.  (Actually it is a separate method, but the JVM will inline it for you, so there's no penalty.)

  --Rex

On Thu, Oct 28, 2010 at 10:55 AM, Bardenhorst, Volker Dr. <Volker.Bardenhorst@eon.com> wrote:
Hi,

Small thing: Is it possible to use aliases for methods in the following sense:

class A(name: String) {
 val x = 5
 ... Missing bit ...
}

enabling the usage

val a = new A("fingers")
println(a.fingers)

giving 5

Cheers,
Volker.

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland