Scala is the best font

I often find myself in things like:

println(foo)

      

when I would like to do:

println foo

      

The compiler does not allow this.

Also, println is a gulp, I just want to say:

echo foo

      

So, in the base object of the package, I created an echo version of println:

def echo(x: Any) = Console.println(x)

      

Simple enough to have an echo app wide, great.

Now, how can I call echo without having to wrap Any to print in parens?

+5


source to share


5 answers


object ∊ {def cho(s: Any) {println(s)}}

∊cho "Hello world"

      

will save your fingers.

It works because Ξ΅ is a mathematical character in the Unicode Sm set, hence considered an operator in Scala , so it does not require spaces when placed next to alphanumeric characters.

you also can



object echo {def -(s: Any) {println(s)}}

echo-"Hello world"

      

which works very well with IMO.

YEARS LATER EDIT: Another almost solution using StringContext:

implicit class PimpMyString(sc: StringContext) {
  def echo(args: Any*) = println(sc.raw(args: _*))
}

echo"Hello World"

      

+11


source


Define

trait ShortCuts {
  def echo(x: Any) = Console.println(x)
  def trace[T](x: T): T = { echo(x); x }
  // ...
}

object ↬ extends ShortCuts

      



and use parentheses without parentheses:

↬ echo "hello!"

      

+5


source


Scalaz has an extended type Identity

that has println

.

scala> import scalaz._; import Scalaz._
import scalaz._
import Scalaz._

scala> val foo = 1
foo: Int = 1

scala> foo println
1

      

If you don't want to depend on scalaz, you can create your own sticky identifier and put them implicitly for it in the package object.

+4


source


What you are trying to achieve is not possible in Scala.

The parentheses can only be dropped in so called text syntax, where you must have the context object on the left side of the function, so in your case, you can only achieve the following, which doesn't do any anyway:

Console println x

      

While I can see why you want to achieve this, you might be better off thinking about the simpler syntax of other languages, my advice would be to just stick to the standard Scala way of doing things, so just use println(x)

or consider other languages. Making a delegate method for such a basic standard function will certainly only bring you problems in the future management of your projects - so definitely "no-no" for the method echo

.

There is an old saying for such cases: when in Rome do what the Romans do.

+3


source


An interesting set of answers here, ranging from not being able to do this, it can be done with this character-dependent hack or with this dependency (Scalaz)

@ Nikita correctly points out that it is just as easy to add snippet to their IDE (if that's the way you roll), which is what println does "legwork". While this is true, you usually have to stop typing ctrl-pr or whatever key combination you choose to use, which breaks your flow, IMO. So, in the spirit of making "better" println, here's where I take:

Create a base package object that your subpackages (model, view, dao, etc.) inherit (basically your PreDef)

package com
package object company {

  // echo(foo)
  def echo(x: Any) = Console.println(x)

  // foo.echo   
  class AnyProvidesEcho(x: Any) { def echo = Console.println(x) }
  @inline implicit def any2Echo(x: Any) = new AnyProvidesEcho(x)
}

      

Using:

val list = List(1,2,3)
val string = "c'est beacoup mieux mit butter"

list foreach echo
echo(string)
string.echo

      

+1


source







All Articles