Skip to content

Conversation

@FunFunFine
Copy link
Contributor

@FunFunFine FunFunFine commented May 30, 2020

It is unclear that istead of writing full function one can use ??? to use underscore notation.
For example,

case class Person(name: String, age: Int) { def sayHello(other: Person) = s"Hello ${other.name}!" } //most obvious way println(nameOf[Person](p => p.sayHello(_)) //more clear way println(nameOf[Person](_.sayHello(???)) //note that this doesnt throw exception

This PR adds this example to README, but I guess it would be more convenient if this library would provide some other object of type Nothing to use instead of ???

dwickern added a commit that referenced this pull request Feb 6, 2021
@dwickern dwickern merged commit 4c93cdb into dwickern:master Feb 6, 2021
@dwickern
Copy link
Owner

dwickern commented Feb 6, 2021

Thank you.

This PR adds this example to README, but I guess it would be more convenient if this library would provide some other object of type Nothing to use instead of ???

That's an interesting idea. We could use a placeholder marked compileTimeOnly to ensure that it's being used in a nameOf block. Anywhere else you would get a compile time error, which is an improvement over the the runtime error you'd get using ???.

@compileTimeOnly("`?` placeholder must be used in a `nameOf` block") def ? : Nothing = ??? println(nameOf[Person](_.sayHello(?)) // "sayHello" println(nameOf(func(?, ?, ?))) // "func" println(func(?, ?, ?)) // compile error
@dwickern dwickern mentioned this pull request Feb 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants