- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Disambiguating a parameter name
I often find myself with the problem of disambiguating a parameter name against a class member of the same name:
trait Foo { def bar: String }
def createFoo(theBar: String): Foo = new Foo {
def bar: String = theName
}
Here I've added a meaningless syllable to the parameter name to make it unique. However, since the "named parameters" feature makes parameter names part of the external API of a class, I really don't want to do this. Also we're assuming here that we want to keep Foo as a trait, and not make it a class with a constructor.
Is there a way around this without picking silly names? There seems no way to specify the parameter over the member.
Cheers,
Ken
trait Foo { def bar: String }
def createFoo(theBar: String): Foo = new Foo {
def bar: String = theName
}
Here I've added a meaningless syllable to the parameter name to make it unique. However, since the "named parameters" feature makes parameter names part of the external API of a class, I really don't want to do this. Also we're assuming here that we want to keep Foo as a trait, and not make it a class with a constructor.
Is there a way around this without picking silly names? There seems no way to specify the parameter over the member.
Cheers,
Ken










Disambiguating a parameter name
def createX(y: String): X = { def tmp = y new X {
def y: String = tmp
}}
On Tuesday, February 7, 2012, Ken Scambler wrote:
Re: Disambiguating a parameter name
On 9 February 2012 10:52, Naftoli Gugenheim <naftoligug [at] gmail [dot] com> wrote:
Re: Disambiguating a parameter name
On Wednesday, February 8, 2012, Ken Scambler wrote:
Disambiguating a parameter name
class MyClass(theFooby: => Something) {
lazy val fooby: Something = theFooby
// everything else can use fooby; theFooby should not be run more than once.
}
Cheers,
Ken
On 9 February 2012 13:51, Naftoli Gugenheim <naftoligug [at] gmail [dot] com> wrote: