diff --git a/examples/src/array/MutableArray.jetl b/examples/src/array/MutableArray.jetl new file mode 100644 index 00000000000..a2c0dba2e90 --- /dev/null +++ b/examples/src/array/MutableArray.jetl @@ -0,0 +1,13 @@ +/** + These declarations are "shallow" in the sense that they are not really compiled, only the type-checker uses them +*/ + +interface class ReadOnlyArray : ISized { + [operator] fun get(index : Int) : T +} + +interface class WriteOnlyArray : ISized { // This is needed to keep IIterator's covariant + [operator] fun set(index : Int, value : T) +} + +class MutableArray : ReadOnlyArray, WriteOnlyArray {...} \ No newline at end of file diff --git a/examples/src/collections/IIterator.jetl b/examples/src/collections/IIterator.jetl index 7717e94062f..31469b269b3 100644 --- a/examples/src/collections/IIterator.jetl +++ b/examples/src/collections/IIterator.jetl @@ -1,4 +1,25 @@ interface class IIterator { fun next() : T val hasNext : Boolean + + fun toArray(buffer : WriteOnlyArray) : Int { // T is still an in-parameter + return fillBuffer(buffer, 0, buffer.size) + } + + fun toArray(buffer : WriteOnlyArray, from : Int, length : Int) : Int { // T is still an in-parameter + if (from < 0 || from > buffer.lastIndex || length < 0 || length > buffer.size - from) { + throw IndexOutOfBoundsException(); + } + + if (len == 0) return 0; + + var count = 0; + for (i in [from .. from + length - 1]) { + if (!hasNext) + return count + buffer[i] = next() + count++ + } + return count + } } diff --git a/examples/src/collections/IList.jetl b/examples/src/collections/IList.jetl index 9e03010ae58..70f1e2cfb2e 100644 --- a/examples/src/collections/IList.jetl +++ b/examples/src/collections/IList.jetl @@ -1,4 +1,4 @@ -interface class IList : IIterable, ISizable { +interface class IList : IIterable, ISized { [operator] fun get(index : Int) : T val isEmpty : Boolean } \ No newline at end of file diff --git a/examples/src/collections/IMutableIterator.jetl b/examples/src/collections/IMutableIterator.jetl index c00e01c3eaa..775980ccedc 100644 --- a/examples/src/collections/IMutableIterator.jetl +++ b/examples/src/collections/IMutableIterator.jetl @@ -1,3 +1,13 @@ interface class IMutableIterator : IIterator { fun remove() : T + +/* + Considerations: + pro: why not + non iteration breaking + con: counter-intuitive for, e.g., TreeSet + + + fun addBefore(item : T) : Boolean + fun addAfter(item : T) : Boolean +*/ } \ No newline at end of file diff --git a/examples/src/collections/LinkedList.jetl b/examples/src/collections/LinkedList.jetl index ca16db4d89a..43be0302cea 100644 --- a/examples/src/collections/LinkedList.jetl +++ b/examples/src/collections/LinkedList.jetl @@ -15,14 +15,14 @@ class LinkedList : IMutableList { if (index == 0) { newItem.next = head head = newItem - if (tail == null) { + if (tail === null) { tail = head } } else { var insertAfter = itemAt(index) newItem.next = insertAfter.next insertAfter.next = newItem - if (tail == insertAfter) { + if (tail === insertAfter) { tail = newItem } } @@ -37,13 +37,13 @@ class LinkedList : IMutableList { override fun remove(index : Int) : T { checkIndex(index) val item = itemAt(index) - if (item == head) { + if (item === head) { head = item.next - if (head == null) + if (head === null) tail= null } else { item.previous.next = item.next - if (item.next == null) { + if (item.next === null) { item.next.previous = item.previous } else { tail = tail.previous diff --git a/examples/src/io/IOSamples.jetl b/examples/src/io/IOSamples.jetl new file mode 100644 index 00000000000..8f0b10ce79f --- /dev/null +++ b/examples/src/io/IOSamples.jetl @@ -0,0 +1,62 @@ +interface class IAdder { + fun add(item : T) : Boolean +} + +interface class ICloseable { + fun close() +} + +abstract class JavaCloseableWrapper(closeable : java.io.Closeable) : ICloseable(closeable) + +fun streamCopy(from : IIterable, to : IAdder) { + for (item in from) t.add(item) +} + +class FileInput : IIterator, JavaCloseableWrapper { + private val stream : InputStream + private var next : Int + private var nextUsed = false + + this(file : File) : JavaCloseableWrapper(stream) { // implicitly throws IOException + stream = FileInputStream(file) // throws IOException + } + + override fun next() { + if (!nextUsed) { + nextUsed = true + return next.as + } + return + } + + override val hasNext { + get() { // implicitly throws IOException + if (nextUsed && next != -1) { + nextUsed = false + next = stream.read() // throws IOException + } + return next != -1 + } + } +} + +class FileOutput throws IOException : IAdder, JavaCloseableWrapper { + private val stream : OutputStream + + this(file : File) : JavaCloseableWrapper(stream) { + stream = FileOutputStream(file) + } + + override fun add(item : Byte) { + stream.write(item) + } +} + +fun example() { // this does not rethrow, no appropriate parameters given + + val f1 : File = ... + val f2 : File = ... + + streamCopy(FileInput(f1), f2) // throws IOException, you must catch or rethrow explicitly + +} \ No newline at end of file diff --git a/examples/src/io/IOSamplesChEx.jetl b/examples/src/io/IOSamplesChEx.jetl new file mode 100644 index 00000000000..087f8ed7db7 --- /dev/null +++ b/examples/src/io/IOSamplesChEx.jetl @@ -0,0 +1,71 @@ +interface class IIterable throws { + fun next() : T // implicitly throws + val hasNext : Boolean // implicitly throws , but actually may NOT!!! +} + +interface class IAdder throws { + fun add(item : T) : Boolean // implicitly throws +} + +interface class ICloseable throws { + fun close() +} + +abstract class JavaCloseableWrapper(closeable : java.io.Closeable) throws : ICloseable { // Maybe we can delegate here + override fun close() { + closeable.close() + } +} + +fun streamCopy(from : IIterable, to : IAdder) { // implicitly rethrows + for (item in from) t.add(item) // both implicitly throw +} + +class FileInput throws : IIterable, JavaCloseableWrapper { + private val stream : InputStream + private var next : Int + private var nextUsed = false + + this(file : File) : JavaCloseableWrapper(stream) { // implicitly throws IOException + stream = FileInputStream(file) // throws IOException + } + + override fun next() { + if (!nextUsed) { + nextUsed = true + return next.as + } + return + } + + override val hasNext { + get() { // implicitly throws IOException + if (nextUsed && next != -1) { + nextUsed = false + next = stream.read() // throws IOException + } + return next != -1 + } + } +} + +class FileOutput throws IOException : IAdder, JavaCloseableWrapper { + private val stream : OutputStream + + this(file : File) : JavaCloseableWrapper(stream) { + stream = FileOutputStream(file) + } + + override fun add(item : Byte) { + stream.write(item) + } +} + +fun example() { // this does not rethrow, no appropriate parameters given + + val f1 : File = ... + val f2 : File = ... + + streamCopy(FileInput(f1), f2) // throws IOException, you must catch or rethrow explicitly + +} \ No newline at end of file