Wiki

Ticket #115 (assigned enhancement)

Opened 9 years ago

Last modified 9 years ago

Support StringBuilder as a target for "print to X, ..."

Reported by: Chuck Owned by: Chuck
Priority: minor Milestone:
Component: Cobra Compiler Version: 0.8.0
Keywords: print, StringBuilder Cc:

Description

print to X, 'Hello, [name].'

Cobra supports X being a TextWriter. It should also support X being a StringBuilder.

Change History

Changed 9 years ago by hopscc

  • owner set to Chuck
  • status changed from new to assigned

Probably unnecessary as can get print to a Stringbuilder (equivalent) by using a
StringWriter which is a TextWriter subclass.

String sb
using z = StringWriter()
	print to z, ' Hello StringWrtr'
	# and more print to z....       
	sb = z.toString

Actually the constraint that the print to destination has to be a TextWriter is
probably overly restrictive in that all thats being used is a write or writeLine
method and TextWriter subclassing demands an implementation of 'System.IO.TextWriter.Encoding.get' which is overhead to the use of print to.
Would be better to just enforce existence of write and writeLine methods in the dest object ...
This makes rolling your own destination object ( for logging or output duplication say) really simple...

Heres a diff

--- Statements.cobra    (revision 1834)
+++ Statements.cobra    (working copy)
@@ -827,8 +827,8 @@
                if .destination
                        .destination.bindImp
                        t = .destination.type
-                       if not t.isDynamicOrPassThrough and not t.isDescendantOf(.compiler.libraryType('System.IO.TextWriter'))  # TODO-BACKEND
-                               .recordError('Invalid destination of type "[t.name]" for "print". Use a TextWriter or subclass thereof instead.')
+                       if not t.isDynamicOrPassThrough and (not t.memberForName('writeLine') or not  t.memberForName('write')) # TODO-BACKEND
+                               .recordError('Invalid destination of type "[t.name]" for "print". "[t.name]" needs to provide a writeLine and write methods.')
                num = 1
                for arg in _args
                        try
Note: See TracTickets for help on using tickets.