Skip to content

Instantly share code, notes, and snippets.

@morrisonlevi
Last active October 25, 2018 16:24
Show Gist options
  • Save morrisonlevi/9c81313a550c1c1503ff092e5ec23735 to your computer and use it in GitHub Desktop.
Save morrisonlevi/9c81313a550c1c1503ff092e5ec23735 to your computer and use it in GitHub Desktop.
Maybe it could be like this, someday...
<?php
namespace PHP {
/* new semantic: $var[] = ; will look for ArrayAppendAccess first,
* then it will try ArrayWriteAccess with null key, which may fail.
*/
interface ArrayAppendAccess<-V> {
function offsetAppend(V $value): void;
}
interface ArrayWriteAccess<-K,-V> {
function offsetSet(K $key, V $value): void;
function offsetUnset(K $key): void;
}
interface ArrayReadAccess<-K,+V> {
function offsetGet(K $key): ?V;
function offsetExists(K $key): bool;
}
interface ArrayMutableReadAccess<K,V>
extends ArrayReadAccess
{
function &offsetGetRef(K $key): ?V;
}
interface MutableIterator<K, V>
extends Iterator<K, V>
{
function &currentRef(): V:
}
interface BidirectionalIterator<K,+V>
extends Iterator<K,V>
{
function end(): void;
function prev(): void;
}
interface RandomAccessIterator<K,+V>
extends BidirectionalIterator<K,V>, \Countable
{
function count(): int;
function seek(int $offset): void;
}
final class ArrayIterator<K, V>
implements
RandomAccessIterator<K,V>,
MutableIterator<K,V>,
ArrayWriteAccess<int,V>,
ArrayAppendAccess<int,V>,
ArrayMutableReadAccess<int,V>,
\Countable
{
// support optionally taking by reference
private function __construct();
static function new(array $input);
static function newByRef(array &$input);
function count(): int;
// iterator functions {{{
function seek(int $offset): void; // int is bc break
function rewind(): void;
function valid(): bool;
function key(): K;
function current(): V;
function &currentRef(): V;
function next(): void;
function end(): void;
function prev(): void;
// }}}
// note, always int keys! This is iterator, not array!
function offsetExists(int $offset): bool;
function offsetGet(int $offset): V;
function &offsetGetRef(int $offset): V;
function offsetSet(int $offset, V $value): void;
function offsetUnset(int $offset); // worst case O(n)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment