Browse Source

Add ReadOnlyArray (#7467)

* Add ReadOnlyArray

* Don't expose the get method

* Expose length as field instead of forwarding

* Update ReadOnlyArray.hx

* Update ReadOnlyArray.hx
Kevin Leung 7 years ago
parent
commit
12ca3f289d
1 changed files with 36 additions and 0 deletions
  1. 36 0
      std/haxe/ds/ReadOnlyArray.hx

+ 36 - 0
std/haxe/ds/ReadOnlyArray.hx

@@ -0,0 +1,36 @@
+/*
+ * Copyright (C)2005-2018 Haxe Foundation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+ 
+package haxe.ds;
+
+/**
+	ReadOnlyArray is an abstract over an ordinary Array but only exposes
+ API's that don't modify the Array instance, hence read-only.
+ Notice that this doesn't necessarily mean that the instance is immutable.
+ Because other code holding the reference as ordinary Array can still modify it.
+**/
+@:forward(concat, copy, filter, indexOf, iterator, join, lastIndexOf, map, slice, toString)
+abstract ReadOnlyArray<T>(Array<T>) from Array<T> {
+  public var length(get,never):Int;
+  inline function get_length() return this.length;
+  @:arrayAccess inline function get(i:Int) return this[i];
+}