How to println(something) but doesn't print anything to the console

72 views
Skip to first unread message

Minh Triet Pham Tran

unread,
Sep 21, 2012, 4:04:46 AM9/21/12
to scala...@googlegroups.com
I have a requirement to println(something) but doesn't println anything and no new line printed, how can I do that in Scala?
What should 'something' above in println be so that it satisfies my requirement?

Dennis Haupt

unread,
Sep 21, 2012, 4:09:06 AM9/21/12
to Minh Triet Pham Tran, scala...@googlegroups.com
huh?

-------- Original-Nachricht --------
> Datum: Fri, 21 Sep 2012 01:04:46 -0700 (PDT)
> Von: Minh Triet Pham Tran <minhtrie...@gmail.com>
> An: scala...@googlegroups.com
> Betreff: [scala-user] How to println(something) but doesn\'t print anything to the console

AGYNAMIX Torsten Uhlmann

unread,
Sep 21, 2012, 4:17:10 AM9/21/12
to Minh Triet Pham Tran, scala...@googlegroups.com
println in Scala is like the System.out.println in Java, it requires a String as input.

println("something") would print something (the string 'something') to the console. You could also do:

println("My phone number: "+phoneNumber) which does call toString on "phoneNumber" if it's not a String already, then concatenates the two and finally passes one String into println.

Torsten.

-- 
AGYNAMIX(R). Passionate Software.
Inh. Torsten Uhlmann | Buchenweg 5 | 09380 Thalheim
Phone:       +49 3721 273445
Fax:             +49 3721 273446
Mobile:       +49 151 12412427
Web:           http://www.agynamix.de

pagoda_5b

unread,
Sep 21, 2012, 4:19:04 AM9/21/12
to scala...@googlegroups.com
println(throw new Exception)

does it fulfill the requirements?

pagoda_5b

unread,
Sep 21, 2012, 4:20:17 AM9/21/12
to scala...@googlegroups.com
also

println(System.exit(0))

Minh Triet Pham Tran

unread,
Sep 21, 2012, 7:57:57 AM9/21/12
to scala...@googlegroups.com
I'm afraid not, it throws out an exception and the program could not continue :( .

pagoda_5b

unread,
Sep 21, 2012, 8:28:07 AM9/21/12
to scala...@googlegroups.com
Sorry to be fussy, but you didn't specify as a requirement that the program flow couldn't be interrupted.
Just to make things clearer: is it a theoretical exercise or whatever?

Bye,
Ivano

Dennis Haupt

unread,
Sep 21, 2012, 8:39:50 AM9/21/12
to pagoda_5b, scala...@googlegroups.com
i assume he just wants no linebreak and is using bad english so we don't get it

-------- Original-Nachricht --------
> Datum: Fri, 21 Sep 2012 05:28:07 -0700 (PDT)
> Von: pagoda_5b <ivano....@gmail.com>
> An: scala...@googlegroups.com
> Betreff: [scala-user] Re: How to println(something) but doesn\'t print anything to the console

pagoda_5b

unread,
Sep 21, 2012, 8:40:28 AM9/21/12
to scala...@googlegroups.com
with continuation plugin enabled (-p:continuations:enable)


import scala.util.continuations._

def f() = {
    reset {
        println( shift { k: (Unit => Unit) => } )
    }
}


ivano


On Friday, September 21, 2012 10:04:46 AM UTC+2, Minh Triet Pham Tran wrote:

Dennis Haupt

unread,
Sep 21, 2012, 8:51:24 AM9/21/12
to pagoda_5b, scala...@googlegroups.com
maybe he just wants to disable logging?

-------- Original-Nachricht --------
> Datum: Fri, 21 Sep 2012 05:40:28 -0700 (PDT)
> Von: pagoda_5b <ivano....@gmail.com>
> An: scala...@googlegroups.com
> Betreff: [scala-user] Re: How to println(something) but doesn\'t print anything to the console

pagoda_5b

unread,
Sep 21, 2012, 9:00:32 AM9/21/12
to scala...@googlegroups.com
Is it something like this acceptable?

case object PrintOff

def printlnSwitch(x: Any): Unit = if (x == PrintOff) () else println(x)



scala> switchPrintln(PrintOff)

scala> switchPrintln("Coool")
Coool

On Friday, September 21, 2012 10:04:46 AM UTC+2, Minh Triet Pham Tran wrote:

Minh Triet Pham Tran

unread,
Sep 21, 2012, 9:11:43 AM9/21/12
to scala...@googlegroups.com
I'm having a language lexer assignment (lexer.scala) and I need to regconized and ignored the comments from the language but the main.scala forced to use println the regconized tokens, this is fixed by my teacher and the test case result is fixed by him too :( .

Som Snytt

unread,
Sep 21, 2012, 9:23:40 AM9/21/12
to Minh Triet Pham Tran, scala...@googlegroups.com
This is not as stylish as other suggestions:

val out = new java.io.ByteArrayOutputStream
Console.setOut(out)
println("Quiet.")

Alex Repain

unread,
Sep 21, 2012, 10:01:14 AM9/21/12
to Som Snytt, Minh Triet Pham Tran, scala...@googlegroups.com
You could redefine println in your project's package :

def println(s: String) = if (s startsWith "//") scala.Predef.println("") else scala.Predef.println(s)

It's a hack, but your teacher constraints are really weird, so ...

scala> def println(s: String) = if (s startsWith "//") scala.Predef.println("") else scala.Predef.println(s)
println: (s: String)Unit
scala> println("yo!")
yo!
scala> println("//yo!")




2012/9/21 Som Snytt <som....@gmail.com>

Dennis Haupt

unread,
Sep 21, 2012, 11:05:21 AM9/21/12
to Alex Repain, som....@gmail.com, scala...@googlegroups.com, minhtrie...@gmail.com
may i talk to your teacher? i want to ask what this is all about. it really sounds like he meant something completely different than what all the guys here think it means

-------- Original-Nachricht --------
> Datum: Fri, 21 Sep 2012 16:01:14 +0200
> Von: Alex Repain <alex....@gmail.com>
> An: Som Snytt <som....@gmail.com>
> CC: Minh Triet Pham Tran <minhtrie...@gmail.com>, scala...@googlegroups.com
> Betreff: Re: [scala-user] Re: How to println(something) but doesn\'t print anything to the console

Seth Tisue

unread,
Sep 21, 2012, 11:34:52 AM9/21/12
to Minh Triet Pham Tran, scala...@googlegroups.com
If you post the same question to a mailing list and to Stack Overflow,
it's polite to link the two to each other so the community doesn't
waste effort writing redundant answer in two different places.

http://stackoverflow.com/questions/12526622/how-to-printlnsomething-but-doesnt-print-anything-to-the-console

--
Seth Tisue | Northwestern University | http://tisue.net
lead developer, NetLogo: http://ccl.northwestern.edu/netlogo/

Florian Hars

unread,
Sep 22, 2012, 4:41:07 PM9/22/12
to scala...@googlegroups.com
Am 21.09.2012 15:11, schrieb Minh Triet Pham Tran:
> I'm having a language lexer assignment (lexer.scala) and I need to
> regconized and ignored the comments from the language but the main.scala
> forced to use println the regconized tokens, this is fixed by my teacher

Then write your lexer in a way that it ignores the comments. That seems
to be the real assignment, not to try to subvert println.

But that still looks slightly funny, the usual approach is to generate
lexer tokens for comments and deal with them in the parser.

- Florian.
Reply all
Reply to author
Forward
0 new messages