- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
aliases
Thu, 2010-10-28, 15:55
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.
Thu, 2010-10-28, 17:47
#2
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:
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.
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.