- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
composing !
Wed, 2011-12-14, 03:30
Hi,
Why the scala REPL does not let me create the hi_!
function in the following session?
A similar function hi_? is created and
works.
Does it have anything to do with the fact that !
can be a prefix operator?
Please explain.
scala> val hi: (String=>String) = "Hi " +
_
hi: String => String = <function1> scala> val ! : (String=>String) = _ + "!"
!: String => String = <function1> scala> val ? : (String=>String) = _ + "?"
?: String => String = <function1> scala> val hi_! = hi andThen !
|
|
You typed two blank lines. Starting a new command. scala> scala> val hi_? = hi andThen ?
hi_?: String => String = <function1> scala> hi_?("John")
res0: String = Hi John? Regards, Grzegorz Balcerek
hi: String => String = <function1> scala> val ! : (String=>String) = _ + "!"
!: String => String = <function1> scala> val ? : (String=>String) = _ + "?"
?: String => String = <function1> scala> val hi_! = hi andThen !
|
|
You typed two blank lines. Starting a new command. scala> scala> val hi_? = hi andThen ?
hi_?: String => String = <function1> scala> hi_?("John")
res0: String = Hi John? Regards, Grzegorz Balcerek
It seems like you can't avoid referencing the unary ! function from
the REPL.
Check this out: http://stackoverflow.com/questions/1991240/scala-method-operator-overloa...
I was able to get around it by wrapping it in an object and using
this.! to reference the ! function:
object Foo extends App {
val hi: (String=>String) = "Hi " + _
val ! : (String=>String) = _ + "!"
val ? : (String=>String) = _ + "?"
val hi_? = hi andThen ?
println(hi_?("John"))
val hi_! = hi andThen this.!
println(hi_!("John"))
}
On Dec 13, 6:30 pm, "Grzegorz Balcerek" wrote:
> Hi,
>
> Why the scala REPL does not let me create the hi_! function in the following session?
> A similar function hi_? is created and works.
> Does it have anything to do with the fact that ! can be a prefix operator?
> Please explain.
>
> scala> val hi: (String=>String) = "Hi " + _
> hi: String => String =
>
> scala> val ! : (String=>String) = _ + "!"
> !: String => String =
>
> scala> val ? : (String=>String) = _ + "?"
> ?: String => String =
>
> scala> val hi_! = hi andThen !
> |
> |
> You typed two blank lines. Starting a new command.
>
> scala>
>
> scala> val hi_? = hi andThen ?
> hi_?: String => String =
>
> scala> hi_?("John")
> res0: String = Hi John?
>
> Regards,
> Grzegorz Balcerek