Merge: Make stream and iterators withable
authorJean Privat <jean@pryen.org>
Mon, 28 Sep 2015 14:36:59 +0000 (10:36 -0400)
committerJean Privat <jean@pryen.org>
Mon, 28 Sep 2015 14:36:59 +0000 (10:36 -0400)
As I wanted to use the `with` statement on Iterators in order to experiment with #1735, I realized that neither them not stream are usable with `with` since they lack the required methods.

Pull-Request: #1742
Reviewed-by: Lucas Bajolet <r4pass@hotmail.com>
Reviewed-by: Alexis Laferrière <alexis.laf@xymus.net>

1  2 
lib/core/stream.nit

diff --combined lib/core/stream.nit
@@@ -39,6 -39,26 +39,26 @@@ abstract class Strea
  
        # close the stream
        fun close is abstract
+       # Pre-work hook.
+       #
+       # Used to inform `self` that operations will start.
+       # Specific streams can use this to prepare some resources.
+       #
+       # Is automatically invoked at the beginning of `with` structures.
+       #
+       # Do nothing by default.
+       fun start do end
+       # Post-work hook.
+       #
+       # Used to inform `self` that the operations are over.
+       # Specific streams can use this to free some resources.
+       #
+       # Is automatically invoked at the end of `woth` structures.
+       #
+       # call `close` by default.
+       fun finish do close
  end
  
  # A `Stream` that can be read from
@@@ -425,13 -445,6 +445,13 @@@ interface Writabl
        end
  end
  
 +redef class Bytes
 +      super Writable
 +      redef fun write_to(s) do s.write_bytes(self)
 +
 +      redef fun write_to_string do return to_s
 +end
 +
  redef class Text
        super Writable
        redef fun write_to(stream) do stream.write(self)