| 1 | = Print = |
| 2 | |
| 3 | Print the given expression or expressionlist. |
| 4 | e.g. |
| 5 | {{{ |
| 6 | print 'My name is Cobra.' |
| 7 | }}} |
| 8 | |
| 9 | Lacking any 'to DEST' clause the expressions provided are printed to the standard output with a trailing |
| 10 | line terminator (\n or \r\n depending on platform). |
| 11 | |
| 12 | This trailing line terminator may be suppressed with a terminating 'stop' |
| 13 | token after the expression list. ( There is no ',' before this token) |
| 14 | {{{ |
| 15 | print 'No newline' stop |
| 16 | }}} |
| 17 | |
| 18 | Multiple comma separated expressions are printed each separated by a |
| 19 | single space. |
| 20 | {{{ |
| 21 | print 'No moa', 'No moa', 'in all aotearoa' |
| 22 | # emits 'No moa No moa in all aotearoa\n' |
| 23 | }}} |
| 24 | |
| 25 | If a 'to DEST' clause is given the expression list is printed to that |
| 26 | destination which must be a !TextWriter or subclass. |
| 27 | |
| 28 | The final form of the print statement given below does not print anything but sets up |
| 29 | a (!TextWriter or subclass) redirection destination for any print |
| 30 | statements in the subsequent block. |
| 31 | |
| 32 | Any and all of string expression interpolation and String.format |
| 33 | processing can be specified within the expressions of the expression list. |
| 34 | |
| 35 | == Grammar == |
| 36 | {{{ |
| 37 | print |
| 38 | |
| 39 | print [to DEST,] <expression> [, expression ...] [ stop] |
| 40 | |
| 41 | print to DEST |
| 42 | BLOCK |
| 43 | }}} |
| 44 | |
| 45 | == Platform == |
| 46 | .Net: the destination must be a System.IO.!TextWriter or subclass. |
| 47 | |
| 48 | == Examples == |
| 49 | {{{ |
| 50 | print # emit a blank line |
| 51 | print 'Hello World' |
| 52 | }}} |
| 53 | |
| 54 | {{{ |
| 55 | print 'Prompt:' stop |
| 56 | input = Console.readline |
| 57 | |
| 58 | print a,b,c stop |
| 59 | print d |
| 60 | # a b c d all end up on the same line |
| 61 | }}} |
| 62 | |
| 63 | {{{ |
| 64 | print "one", '2', 'free' |
| 65 | # emits 'one 2 free\n' |
| 66 | |
| 67 | i = 2 |
| 68 | s = 'free' |
| 69 | print 'one [i] [s]' # same output as above |
| 70 | }}} |
| 71 | |
| 72 | {{{ |
| 73 | |
| 74 | # print to std error |
| 75 | print to Console.error, 'error: all froobits bedazzled' |
| 76 | |
| 77 | sw = StringWriter() |
| 78 | print to sw |
| 79 | print "Redirected to the StringWriter' |
| 80 | s = sw.toString |
| 81 | assert s.contains('Redirected to the StringWriter') |
| 82 | }}} |
| 83 | |
| 84 | |
| 85 | == Notes == |
| 86 | |
| 87 | Non string expressions are converted to a string representation for printing |
| 88 | (presumably using their toString methods). |
| 89 | |
| 90 | Collections are expanded so their contents are displayed using |
| 91 | {{{CobraCore.PrintStringMaker.makeString}}} |
| 92 | (++ This needs expansion/explanation ++ ) |
| 93 | |
| 94 | See discussion forum [http://cobra-language.com/forums/viewtopic.php?f=4&t=160 Print vs Console.Writeline] |