This page is no longer maintained — Please continue to the home page at www.scala-lang.org

composing !

1 reply
gbalcerek@echos...
Joined: 2009-10-21,
User offline. Last seen 31 weeks 2 days ago.
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  
jamesearldouglas
Joined: 2011-03-28,
User offline. Last seen 1 year 31 weeks ago.
Re: composing !

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

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland