Skip to content

Conversation

ennru
Copy link
Contributor

@ennru ennru commented May 16, 2017

@felixmulder One more and I call it a day.

val msg = s"${if (mutable) "`var'" else "`val'"} parameters may not be call-by-name"
val kind = "Syntax"
val explanation =
hl"""Parameters of classes and traits may no be call-by-name. In case you
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They can be as long as they don't have a getter e.g, this compiles:

class Foo(x: => Int)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, will be more specific.

|want the parameter to be evaluated on demand, consider using a function
|and a lazy value in the class such as
| ${s"class MyClass(${name}Tick: () => String) {"}
| ${s" lazy val $name = ${name}Tick"}
Copy link
Member

@smarter smarter May 16, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But that's different from what a by-name would do, the closest thing would be to use a def, not a lazy val:

class MyClass(fooFun: () => Int)) {
  def foo() = fooFun()
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or even:

class MyClass(myFoo: => Int) {
  def foo() = myFoo
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, that's better. Will update.

@felixmulder felixmulder merged commit f556b04 into scala:master May 17, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants