From c707c5df80d28e1baf3e01860bfdaf88a89fe492 Mon Sep 17 00:00:00 2001 From: Clark McCauley Date: Wed, 2 Oct 2024 13:12:06 -0600 Subject: [PATCH] Allow `.size()` method on types (#88) --- interpreter/src/functions.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/interpreter/src/functions.rs b/interpreter/src/functions.rs index 87dc470..69f48d2 100644 --- a/interpreter/src/functions.rs +++ b/interpreter/src/functions.rs @@ -70,8 +70,11 @@ impl<'context> FunctionContext<'context> { /// ```skip /// size([1, 2, 3]) == 3 /// ``` -pub fn size(ftx: &FunctionContext, value: Value) -> Result { - let size = match value { +/// ```skip +/// 'foobar'.size() == 6 +/// ``` +pub fn size(ftx: &FunctionContext, This(this): This) -> Result { + let size = match this { Value::List(l) => l.len(), Value::Map(m) => m.map.len(), Value::String(s) => s.len(), @@ -565,6 +568,8 @@ mod tests { ("size of map", "size({'a': 1, 'b': 2, 'c': 3}) == 3"), ("size of string", "size('foo') == 3"), ("size of bytes", "size(b'foo') == 3"), + ("size as a list method", "[1, 2, 3].size() == 3"), + ("size as a string method", "'foobar'.size() == 6"), ] .iter() .for_each(assert_script);