Skip to content

Latest commit

 

History

History
83 lines (63 loc) · 1.91 KB

ImmutableVector.md

File metadata and controls

83 lines (63 loc) · 1.91 KB

Immutable Vector

Examples

Initialization

const a = ImmutableVector([1, 2]);

Initialization Sugar

const a = #[1, 2];

Extension

const b = a.concat(#[3, 4]);

Extension Sugar

const b = #[...a, 3, 4];

Type

typeof #[]; // 'vector'

Syntax

VectorLiteral:

  • # [ (VectorElement (, VectorElement)*)? ]

VectorElement:

  • AssignmentExpression
  • ... AssignmentExpression

Comparison Semantics

== and === test value equality.

<, >= etc. test value order/equality in enumeration order.

Prototype

Value types are not objects and have no prototype. Member expressions are exposed on the Vector's prototype which is unique for each realm, just like other value types. This prototype is exposed on the realm's global ImmutableVector.prototype.

Notes

Enumeration always proceeds in numeric index order.

.length cannot be greater than 2^32 - 1

Vectors are never sparse. They cannot have holes.

Polyfill in Terms of Typed Objects

let ImmutableVector = (function() {
  let VectorSymbol = new Symbol('vector');
  let VectorConstructor = function(iterable) {
    let fields = {};
    let values = {};
    let i = 0;
    for (let value of iterable) {
      i++;
      fields[i] = any;
      values[i] = value;
    }
    // Note: This could probably be replaced with a value array but it's
    // unclear in the current ValueTypes proposal what that means.
    let type = ValueType(VectorSymbol, fields);
    // Note: We can't replace the ValueType's prototype object to
    // the shared object, so we have to do the next best thing.
    Object.setPrototypeOf(type.prototype, VectorPrototype);
    return new type(values);
  };
  let VectorPrototype = VectorConstructor.prototype;
  return VectorConstructor;
})();