Maybe a good substitution for postfixOps

60 views
Skip to first unread message

Zhranklin

unread,
Jul 26, 2016, 10:08:38 PM7/26/16
to scala-internals

Hi everyone:

I have an idea about using an method without parameter as an infix operator. Maybe we can introduce a symbol like ".." or a new identifier to show that the method before it need no parameter.


My idea is from manipulating collections from java by JavaConverters(JavaConversions is deprecated):

import scala.collection.JavaConverters._
import java.util.stream.Collectors

def xxx = someJavaStream collect Collectors.toList() asScala


and then, use it as a scala collection, but i can't continue directly because the method asScala can't be used as an infix operator, I should change it to:

def xxx = (someJavaStream collect Collectors.toList() asScala) map ......

or:
def xxx = someJavaStream.collect(Collectors.toList()).asScala.map ....

or:
def xxx = {
  val sList
= someJavaStream.collect(Collectors.toList()).asScala
  sList map
....
}


But Introducing the syntax I mentioned seems to fix this:

import scala.language.xxx

someJavaStream collect
Collectors.toList() asScala .. map someFunc

or:

import xxx.??

someJavaStream collect
Collectors.toList() asScala ?? map someFunc


I have tried on REPL:

scala> implicit class Test(s: String) {
     
| def foo = "bar"
     
| def foo0() = "bar"
     
| def foo1(s: String) = "bar" + s
     
| def foo2(s1: String, s2: String) = "bar" + s1 + s2
     
| }
defined class Test

scala
> "" foo0 () foo1 "s1" foo2 ("s1", "s2")
res0
: String = bars1s2

scala
> "" foo () foo1 "s1" foo2 ("s1", "s2")
<console>:13: error: not enough arguments for method apply: (index: Int)Char in class StringOps.
Unspecified value parameter index.
       
"" foo () foo1 "s1" foo2 ("s1", "s2")
         
^
scala
> "aaa" toLowerCase () toList
warning
: there was one feature warning; re-run with -feature for details

res2
: List[Char] = List(a, a, a)


It shows that the method with parentheses work well with infix notation besides non-parameter method from Java . Only methods without parentheses aren't compatible to this feature.


And Dale Wijnand showed us an example at the comment here that we have to add parentheses for enabling infix chaining in sbt. But as I know void parentheses is to show side-effect of the method? And how about existing scala libraries?


I know that not all people support to encourage the usage of infix notation. But I think it's not very probable that only methods without parentheses can't be used as infix operation. Is it possible to improve orthogonality and integrity of syntax in this way? Besides, some problem with postfixOps will be fixed.


I'm not native speaker. Sorry for my bad expression.


Thanks!

Reply all
Reply to author
Forward
0 new messages