I want to know how to avoid the same argument name and method name for anonymous classes when creating anonymous classes.

Asked 2 years ago, Updated 2 years ago, 76 views

When creating an anonymous class with a method, if the argument and the method name of the class are the same

trite User {val name:String}

def newUser(name:String) = newUser{
  val name = name
}

I'd like to, but warning: value name does not other than call itself recurring, or val name = name is interpreted as val name = this.name, so it doesn't work as intended.

I would like to use the argument name of the function as a named argument, so I would like to use it as much as possible in the class.In that case

def newUser(name:String)={
  valxname = name
  new User {
    val name = xname
  }
}

Is there no alternative but to temporarily substitute another name argument as shown in ?

scala

2022-09-29 21:55

1 Answers

That seems to be the only thing I can do.

Also in the style guide, regarding name conflicts

The Scala compiler will complain that the name field and the name method name conflict

and then

class Company {
  private val_name —String=_

  def name=_name

  def name_=(name:String){
    _name = name
  }
}

indicates the code.
The translation says there are many ways to avoid it, but the original text seems to have been deleted now.

Style Guide:

http://yanana.github.io/scala-style/naming_conventions/methods/accessors_mutators.html

http://docs.scala-lang.org/style/naming-conventions.html

For your reference, I will list the same questions at home.
The shadowing in the scala guide does not mention the implicit scope here.Some of the respondents also commented that

https://stackoverflow.com/questions/7438025/in-scala-is-there-a-way-to-access-a-symbol-variable-defined-in-an-outer-scope


2022-09-29 21:55

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.