Operators ending with a Colon

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Operators ending with a Colon

Didi-3
Hello,

for lists the e.g. exists a :: operator. The colon at the end of the operator marks this method as right assostiative. So i can use it e.g. to and a string to a empty list
  = "A" :: nil

Now i want to write a own method in the same style:
if i write something like this

object Test{
  def main(args:Array[String]){
    var t  = new A("A");
    "B" test: t
  }  
}

class A(val name : String) {
  def test: ( anothername : String) : Unit  = {println("me: "+name+" other: "+anothername ); }
}


it does not work(=compile), but if i write it linke this


object Test{
  def main(args:Array[String]){
    var t  = new A("A");
    "B" test_: t
  }  
}

class A(val name : String) {
  def test_: ( anothername : String) : Unit  = {println("me: "+name+" other: "+anothername ); }
}

it does.

where is my mistake?

Thanks Christian
Reply | Threaded
Open this post in threaded view
|

Re: Operators ending with a Colon

Martin Odersky
There is no mistake. If you want to combine a symbol like `:' with an
alphanumeric identifier there needs to be an underscore between the
two.
So:

  test_:   is one symbol, but
  test:     are the two symbols `test', `:'

Hope this helps

 -- Martin



On Fri, Mar 14, 2008 at 7:53 PM, didi <[hidden email]> wrote:

>
>  Hello,
>
>  for lists the e.g. exists a :: operator. The colon at the end of the
>  operator marks this method as right assostiative. So i can use it e.g. to
>  and a string to a empty list
>   = "A" :: nil
>
>  Now i want to write a own method in the same style:
>  if i write something like this
>
>  object Test{
>   def main(args:Array[String]){
>     var t  = new A("A");
>     "B" test: t
>   }
>  }
>
>  class A(val name : String) {
>   def test: ( anothername : String) : Unit  = {println("me: "+name+" other:
>  "+anothername ); }
>  }
>
>
>  it does not work, but if i write it linke this
>
>
>  object Test{
>   def main(args:Array[String]){
>     var t  = new A("A");
>     "B" test_: t
>   }
>  }
>
>  class A(val name : String) {
>   def test_: ( anothername : String) : Unit  = {println("me: "+name+" other:
>  "+anothername ); }
>  }
>
>  it does.
>
>  where is my mistake?
>
>  Thanks Christian
>  --
>  View this message in context: http://www.nabble.com/Operators-ending-with-a-Colon-tp16049528p16049528.html
>  Sent from the Scala - User mailing list archive at Nabble.com.
>
>