IMapInitializable INTERFACE

A class that implements this interface gains the ability of being instantiated with a map-like initializer trailing the constructor. For example, since Systen.Collection.Map implements this interface, we can create a new Map with a bunch of preset key-value pairs:

  Map map = new Map() {
    "a" = "xyz",
    12 = 'c',
    mykey = myval + fun(var1), // Treated as string literal, "mykey"
    (bar(5)) = obj2 // The outer () is required. See below.
  };

  // The above is equivalent to
  Map map = new Map();
  map.initByMap(
    new Entry("a", "xyz"),
    new Entry(12, 'c'),
    new Entry("mykey", myval + fun(var1)),
    new Entry(bar(5), obj2)
   };


As demonstrated by the equivalent form, for each key-value initializer, the script engine will evaluate the left side to a key, the right side a value, so that it can create a System.Util.Entry with them. This syntax sugar, however, requires that the left side be an identifier, literal value or any expression enclosed by parentheses, as otherwise it couldn't pinpoint the position of assignment operator (=) used to demarcate the key and the value.

While most keys are used as is, there is one exception. Any identifier will be reinterpreted as a string literal. This rule makes it more convenient to initialize Dynamic. If the key is indeed to be provided by a variable, enclose the variable with parentheses to force an evaluation.

  string country = "United States";
  Map capitals = new Map() {
    country = "Washington" // A key of "country" will be added!
  };
  Console.println(capitals["United States"]); // (null)
  Console.println(capitals["country"]); // Washington

  capitals = new Map() {
    (country) = "Washington" // A key of "United States" is added instead
  };
  Console.println(capitals["United States"]); // Washington

All Members


TypeNameSignature
methodinitByMappublic void initByMap(System.Util.Entry[])

Methods


public void initByMap(Entry[] entries)

Initialize the object with an array of key-value pairs. The implementation may decide how to handle nullness, key duplication, the legality of key/value types, etc.

Parameters

  • entries The key-value pairs.