Skip to content

Commit

Permalink
Merge pull request #124 from Samy-33/feat/multimethods
Browse files Browse the repository at this point in the history
feat(multimethods): adds remaining related fns to core
  • Loading branch information
jeaye authored Nov 6, 2024
2 parents 6811789 + fcb67ed commit a20bd8a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 43 deletions.
1 change: 1 addition & 0 deletions compiler+runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ target_include_directories(
"$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/third-party/immer>"
"$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/third-party/magic_enum/include/magic_enum>"
"$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/third-party/cli11/include>"
"$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/third-party/fmt/include>"
"$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/third-party/libzippp/src>"
)
target_precompile_headers(
Expand Down
6 changes: 3 additions & 3 deletions compiler+runtime/src/cpp/jank/runtime/obj/multi_function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ namespace jank::runtime
{
throw std::runtime_error{ fmt::format(
"Preference conflict in multimethod '{}': {} is already preferred to {}",
name,
runtime::to_string(name),
runtime::to_string(y),
runtime::to_string(x)) };
}
Expand Down Expand Up @@ -304,7 +304,7 @@ namespace jank::runtime
if(target == obj::nil::nil_const())
{
throw std::runtime_error{ fmt::format("No method in multimethod '{}' for dispatch value: {}",
name,
runtime::to_string(name),
runtime::to_string(dispatch_val)) };
}
return target;
Expand Down Expand Up @@ -350,7 +350,7 @@ namespace jank::runtime
throw std::runtime_error{ fmt::format(
"Multiple methods in multimethod '{}' match dispatch value: {} -> {} and {}, and "
"neither is preferred",
name,
runtime::to_string(name),
runtime::to_string(dispatch_val),
runtime::to_string(entry_key),
runtime::to_string(best_entry->first())) };
Expand Down
71 changes: 31 additions & 40 deletions compiler+runtime/src/jank/clojure/core.jank
Original file line number Diff line number Diff line change
Expand Up @@ -3240,46 +3240,37 @@
[multifn dispatch-val & fn-tail]
`(defmethod* ~multifn ~dispatch-val (fn ~@fn-tail)))

;(defn remove-all-methods
; "Removes all of the methods of multimethod."
; {:added "1.2"
; :static true}
; [^clojure.lang.MultiFn multifn]
; (.reset multifn))
;
;(defn remove-method
; "Removes the method of multimethod associated with dispatch-value."
; {:added "1.0"
; :static true}
; [^clojure.lang.MultiFn multifn dispatch-val]
; (. multifn removeMethod dispatch-val))
;
;(defn prefer-method
; "Causes the multimethod to prefer matches of dispatch-val-x over dispatch-val-y
; when there is a conflict"
; {:added "1.0"
; :static true}
; [^clojure.lang.MultiFn multifn dispatch-val-x dispatch-val-y]
; (. multifn preferMethod dispatch-val-x dispatch-val-y))
;
;(defn methods
; "Given a multimethod, returns a map of dispatch values -> dispatch fns"
; {:added "1.0"
; :static true}
; [^clojure.lang.MultiFn multifn] (.getMethodTable multifn))
;
;(defn get-method
; "Given a multimethod and a dispatch value, returns the dispatch fn
; that would apply to that value, or nil if none apply and no default"
; {:added "1.0"
; :static true}
; [^clojure.lang.MultiFn multifn dispatch-val] (.getMethod multifn dispatch-val))
;
;(defn prefers
; "Given a multimethod, returns a map of preferred value -> set of other values"
; {:added "1.0"
; :static true}
; [^clojure.lang.MultiFn multifn] (.getPreferTable multifn))
(defn remove-all-methods
"Removes all of the methods of multimethod."
[multifn]
(native/raw "__value = try_object<obj::multi_function>(~{ multifn })->reset();"))

(defn remove-method
"Removes the method of multimethod associated with dispatch-value."
[multifn dispatch-val]
(native/raw "__value = try_object<obj::multi_function>(~{ multifn })->remove_method(~{ dispatch-val });"))

(defn prefer-method
"Causes the multimethod to prefer matches of dispatch-val-x over dispatch-val-y
when there is a conflict"
[multifn dispatch-val-x dispatch-val-y]
(native/raw "__value = try_object<obj::multi_function>(~{ multifn })->prefer_method(~{ dispatch-val-x }, ~{ dispatch-val-y });"))

(defn methods
"Given a multimethod, returns a map of dispatch values -> dispatch fns"
[multifn]
(native/raw "__value = try_object<obj::multi_function>(~{ multifn })->method_table;"))

(defn get-method
"Given a multimethod and a dispatch value, returns the dispatch fn
that would apply to that value, or nil if none apply and no default"
[multifn dispatch-val]
(native/raw "__value = try_object<obj::multi_function>(~{ multifn })->get_fn(~{ dispatch-val });"))

(defn prefers
"Given a multimethod, returns a map of preferred value -> set of other values"
[multifn]
(native/raw "__value = try_object<obj::multi_function>(~{ multifn })->prefer_table;"))

;; Hierarchies.
(defn make-hierarchy
Expand Down

0 comments on commit a20bd8a

Please sign in to comment.