Page 1 of 2

print vs Console.writeLine

PostPosted: Tue Sep 16, 2008 7:40 am
by relez
Code: Select all
class Program
   def main is shared
      Console.writeLine('Hello, world.')


Code: Select all
class Program
   def main is shared
      print 'Hello, world.'


Both these little programs works. Obviously it's not the same using one or the other. Is there one way "better" of the other? Why?

Re: print vs Console.writeLine

PostPosted: Tue Sep 16, 2008 8:45 am
by Charles
You're free to use either one, but consider:

-- Only print is "redirectable":
print to someTextWriter
.doThis
.doThat

Within the execution of the "print to" block, all output will go to someTextWriter instead of to the console. It's roughly equivalent to:
CobraImp.pushPrintDestination(someTextWriter)
try
.doThis
.doThat
finally
CobraImp.popPrintDestination

-- As Cobra migrates to other platforms such as Java, Parrot, Android, etc. only "print" will be guaranteed to continue to work.

For the above reasons, I think "print" is a better choice.

And just for reference:
# to point just one print statement to a text writer:
print to someTextWrite, "hello", foo, bar

# to avoid the new line
print "hello" stop

Re: print vs Console.writeLine

PostPosted: Tue Sep 16, 2008 10:47 am
by Charles
I forgot to mention that "print" has nicer output for collections (post 0.8).

You can access this magic directly via CobraCore.printStringMaker.makeString(t). You can also assign to CobraCore.printStringMaker and thereby take over the creation of strings for "print" statements and for string substitution expressions ('Hi [name]'). You would need to read code in CobraLang.cobra to learn how (search for instances of "StringMaker").

Outside of "print" and string substitution, expressions are not normally wrapped by CobraCore.printStringMaker.makeString which is why Console.writeLine shows the ugly default .toString of the list.

If that's too much information, just remember that "print" gives nicer output.

Re: print vs Console.writeLine

PostPosted: Wed Sep 17, 2008 2:44 pm
by relez
Thx Chuck, now it is all quite clear.
On the contrary, to read from standard input we have to use Console.readLine and nothing other, it seems.... is there another way?

Re: print vs Console.writeLine

PostPosted: Wed Sep 17, 2008 4:11 pm
by Charles
Yes, Console.readLine is how to read from std input. I suppose eventually we'll want a platform neutral way of doing that too. Could be "CobraCore.readLine". Or could be a statement if there were compelling advantages to having that at the language level like there is for "print".

Re: print vs Console.writeLine

PostPosted: Wed Sep 17, 2008 4:18 pm
by relez
Chuck wrote:Yes, Console.readLine is how to read from std input. I suppose eventually we'll want a platform neutral way of doing that too. Could be "CobraCore.readLine". Or could be a statement if there were compelling advantages to having that at the language level like there is for "print".


"symmetrically" I would say that it would be optimal ;)

Re: print vs Console.writeLine

PostPosted: Wed Sep 17, 2008 4:23 pm
by Charles
Yes that's one consideration. Feel free to put forth a design. What are the possibilities and what is the behavior?
# musing:
input name
i = 0
input i
input x, y
input 'Enter your choice:', x
# Can you input just a single character?
# If the variable is an int but the input cannot be converted to an int, what happens?
# Can the var be declared with the statement?
input age as int
# Can you target a property?
input customer.name
input obj.count

Re: print vs Console.writeLine

PostPosted: Wed Sep 17, 2008 4:48 pm
by relez
Chuck wrote:Yes that's one consideration. Feel free to put forth a design. What are the possibilities and what is the behavior?
# musing:
input name
i = 0
input i
input x, y
input 'Enter your choice:', x
# Can you input just a single character?
# If the variable is an int but the input cannot be converted to an int, what happens?
# Can the var be declared with the statement?
input age as int
# Can you target a property?
input customer.name
input obj.count


Good point... time to check....

Re: print vs Console.writeLine

PostPosted: Wed Jan 14, 2009 4:06 am
by jonathandavid
[Edited to make it shorter]

Chuck wrote:Yes that's one consideration. Feel free to put forth a design. What are the possibilities and what is the behavior?
# musing:
input name
i = 0
input i
input x, y
input 'Enter your choice:', x
# Can you input just a single character?
# If the variable is an int but the input cannot be converted to an int, what happens?
# Can the var be declared with the statement?
input age as int
# Can you target a property?
input customer.name
input obj.count


My suggestions, in case this debate is still open:

# FIRST FORM OF INPUT
# Declares new var called name of type int, and assigns user input to it
input name as int

# Equivalent to:
name = int.parse(Console.readLine)

# SECOND FORM OF INPUT
# Reads a variable that already exists
input num

# Equivalent to:
num = float32.parse(Console.readLine)


# REFINEMENTS

# Many variables could be read at once
input a as int, b as float32

# Properties can be read
input foo.bar # equivalent to: foo.bar = type_of(bar).parse(Console.readLine)

# A prompt can be specified:
input 'enter an integer', x as int

# Note that we have to write "prompt" if the message is in a variable:
msg = "enter an integer"
input prompt msg, x as int

# A source stream can be specified:
input from source, x as int

Re: print vs Console.writeLine

PostPosted: Wed Jan 14, 2009 5:40 am
by jonathandavid
I forgot something. It would also be nice to have a way to specify what action to take in case something goes wrong. This is specially important when input defines the variable as well. Suppose I have the following requirement: read an int into a new variable called x, or assign 0 to x if the read operation fails. This is how this could be supported in Cobra:

input x as int(0)   #  x takes user specified value or 0 in case of error



This handles multiple input operations gracefully:
input x as String ("error"), i(-2)


If the "(defaultValue)" part is not specified, an exception is thrown in case of error.

Another common use is to loop in case of error.

We could achieve this with a try catch:

x as int
while true
try
input x
catch
print 'try again'
success
break