Forums

print vs Console.writeLine

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

print vs Console.writeLine

Postby relez » Tue Sep 16, 2008 7:40 am

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?
relez
 
Posts: 69

Re: print vs Console.writeLine

Postby Charles » Tue Sep 16, 2008 8:45 am

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
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: print vs Console.writeLine

Postby Charles » Tue Sep 16, 2008 10:47 am

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.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: print vs Console.writeLine

Postby relez » Wed Sep 17, 2008 2:44 pm

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?
relez
 
Posts: 69

Re: print vs Console.writeLine

Postby Charles » Wed Sep 17, 2008 4:11 pm

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".
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: print vs Console.writeLine

Postby relez » Wed Sep 17, 2008 4:18 pm

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 ;)
relez
 
Posts: 69

Re: print vs Console.writeLine

Postby Charles » Wed Sep 17, 2008 4:23 pm

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
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: print vs Console.writeLine

Postby relez » Wed Sep 17, 2008 4:48 pm

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....
relez
 
Posts: 69

Re: print vs Console.writeLine

Postby jonathandavid » Wed Jan 14, 2009 4:06 am

[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
Last edited by jonathandavid on Wed Jan 14, 2009 11:19 am, edited 2 times in total.
jonathandavid
 
Posts: 159

Re: print vs Console.writeLine

Postby jonathandavid » Wed Jan 14, 2009 5:40 am

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
Last edited by jonathandavid on Wed Jan 14, 2009 11:23 am, edited 2 times in total.
jonathandavid
 
Posts: 159

Next

Return to Discussion

Who is online

Users browsing this forum: No registered users and 106 guests