From 838313fa9cb9784db1aa6f5c26bb06072ddbd79d Mon Sep 17 00:00:00 2001 From: Pekka Laiho Date: Fri, 19 Jun 2020 13:46:52 +0700 Subject: [PATCH] support for object and resource types --- README.md | 2 ++ src/Lib/Types.php | 20 ++++++++++++++++++++ src/Printer.php | 5 +++++ 3 files changed, 27 insertions(+) diff --git a/README.md b/README.md index 119f0fa..1cabbf1 100644 --- a/README.md +++ b/README.md @@ -275,6 +275,8 @@ vector? | Return true if the argument is a vector. seq? | Return true if the argument is a sequence (list or vector). hash? | Return true if the argument is a hash-map. symbol? | Return true if the argument is a symbol. +object? | Return true if the argument is an object. +resource? | Return true if the argument is a resource. bool? | Return true if the argument is a boolean value (strict comparison). true? | Return true if the argument evaluates to true (non-strict comparison). false? | Return true if the argument evaluates to false (non-strict comparison). diff --git a/src/Lib/Types.php b/src/Lib/Types.php index bf5d808..1b7ee99 100644 --- a/src/Lib/Types.php +++ b/src/Lib/Types.php @@ -1,6 +1,7 @@ $a instanceof Symbol )); + $env->set('object?', new CoreFunc('object?', 'Return true if argument is an object.', 1, 1, + function ($a) { + // Skip classes which have their own types + if ($a instanceof Func || $a instanceof Collection || $a instanceof Symbol) { + return false; + } + + return is_object($a); + } + )); + + $env->set('resource?', new CoreFunc('resource?', 'Return true if argument is a resource.', 1, 1, + fn ($a) => is_resource($a) + )); + $env->set('bool?', new CoreFunc('bool?', 'Return true if argument is a boolean.', 1, 1, fn ($a) => $a === true || $a === false )); diff --git a/src/Printer.php b/src/Printer.php index 34099bc..737e34d 100644 --- a/src/Printer.php +++ b/src/Printer.php @@ -21,6 +21,11 @@ class Printer array_keys($a->getData()), array_values($a->getData()))) . '}'; } elseif ($a instanceof Symbol) { return $a->getName(); + } elseif (is_object($a)) { + $class = get_class($a); + return ">"; + } elseif (is_resource($a)) { + return ''; } elseif ($a === true) { return 'true'; } elseif ($a === false) {