From 35bcc0ab3ac754e9f2ce6465c3d5c060bd5d37aa Mon Sep 17 00:00:00 2001 From: Andrey Orst Date: Sun, 8 Nov 2020 22:02:54 +0300 Subject: feature(core): add metadata based type inspection for tables, and empty macro --- macros/core.fnl | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) (limited to 'macros') diff --git a/macros/core.fnl b/macros/core.fnl index def2f4c..df3f8b8 100644 --- a/macros/core.fnl +++ b/macros/core.fnl @@ -66,10 +66,13 @@ `(fn [tbl#] (let [t# (type tbl#)] (if (= t# :table) - (let [(k# _#) (next tbl#)] - (if (and (= (type k#) :number) (= k# 1)) :seq - (= k# nil) :empty - :table)) + (let [meta# (getmetatable tbl#) + table-type# (and meta# (. meta# :cljlib/table-type))] + (if table-type# table-type# + (let [(k# _#) (next tbl#)] + (if (and (= (type k#) :number) (= k# 1)) :seq + (= k# nil) :empty + :table)))) :else)))) (fn seq-fn [] @@ -84,6 +87,12 @@ (insert# res# [k# v#])) (if assoc# res# tbl#)))) +(fn& core.empty [tbl] + (let [table-type (table-type tbl)] + (if (= table-type :seq) `(setmetatable {} {:cljlib/table-type :seq}) + (= table-type :table) `(setmetatable {} {:cljlib/table-type :table}) + `(setmetatable {} {:cljlib/table-type (,(table-type-fn) ,tbl)})))) + (fn& core.into [to from] (let [to-type (table-type to) from-type (table-type from)] -- cgit v1.2.3