From fb05e811b7aa77e34f0bda9194efce159aef22b7 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Thu, 15 Oct 2020 22:11:40 +0800 Subject: [PATCH] Map::keys and Map::values can be used method-call style. --- doc/src/language/for.md | 6 +++--- doc/src/language/object-maps.md | 31 +++++++++++++++++++++++++++++-- src/stdlib.rs | 2 +- tests/for.rs | 4 ++-- 4 files changed, 35 insertions(+), 8 deletions(-) diff --git a/doc/src/language/for.md b/doc/src/language/for.md index c60372a8..8fa560d2 100644 --- a/doc/src/language/for.md +++ b/doc/src/language/for.md @@ -79,7 +79,7 @@ for x in range(0, 50, 3) { // step by 3 Iterate Through Object Maps -------------------------- -Two functions, `keys` and `values`, return [arrays] containing cloned _copies_ +Two methods, `keys` and `values`, return [arrays] containing cloned _copies_ of all property names and values of an [object map], respectively. These [arrays] can be iterated. @@ -88,7 +88,7 @@ These [arrays] can be iterated. let map = #{a:1, b:3, c:5, d:7, e:9}; // Property names are returned in unsorted, random order -for x in keys(map) { +for x in map.keys() { if x > 10 { continue; } // skip to the next iteration print(x); @@ -97,7 +97,7 @@ for x in keys(map) { } // Property values are returned in unsorted, random order -for val in values(map) { +for val in map.values() { print(val); } ``` diff --git a/doc/src/language/object-maps.md b/doc/src/language/object-maps.md index 937b1818..7c7fba24 100644 --- a/doc/src/language/object-maps.md +++ b/doc/src/language/object-maps.md @@ -126,11 +126,11 @@ y.remove("a") == 1; // remove property y.len() == 2; y.has("a") == false; -for name in keys(y) { // get an array of all the property names via the 'keys' function +for name in y.keys() { // get an array of all the property names via 'keys' print(name); } -for val in values(y) { // get an array of all the property values via the 'values' function +for val in y.values() { // get an array of all the property values via 'values' print(val); } @@ -138,3 +138,30 @@ y.clear(); // empty the object map y.len() == 0; ``` + + +No Support for Property Getters +------------------------------ + +In order not to affect the speed of accessing properties in an object map, new property +[getters][getters/setters] cannot be registered because they conflict with the syntax of +property access. + +A property [getter][getters/setters] function registered via `Engine::register_get`, for example, +for a `Map` will never be found - instead, the property will be looked up in the object map. + +Therefore, _method-call_ notation must be used for built-in properties: + +```rust +map.len // access property 'len', returns '()' if not found + +map.len() // returns the number of properties + +map.keys // access property 'keys', returns '()' if not found + +map.keys() // returns array of all property names + +map.values // access property 'values', returns '()' if not found + +map.values() // returns array of all property values +``` diff --git a/src/stdlib.rs b/src/stdlib.rs index 5ec6c2a0..165b9068 100644 --- a/src/stdlib.rs +++ b/src/stdlib.rs @@ -16,7 +16,7 @@ mod inner { pub use core_error as error; pub mod collections { - pub use hashbrown::{HashMap, HashSet}; + pub use hashbrown::{hash_map, hash_set, HashMap, HashSet}; } } diff --git a/tests/for.rs b/tests/for.rs index e3b561d6..0de226f0 100644 --- a/tests/for.rs +++ b/tests/for.rs @@ -61,10 +61,10 @@ fn test_for_object() -> Result<(), Box> { let keys = ""; let map = #{a: 1, b: 2, c: 3}; - for key in keys(map) { + for key in map.keys() { keys += key; } - for value in values(map) { + for value in map.values() { sum += value; }