Skip to content

Commit

Permalink
add let example for functional interface coercion
Browse files Browse the repository at this point in the history
  • Loading branch information
puredanger committed Dec 14, 2023
1 parent 7b0f34e commit ac501f4
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion content/reference/special_forms.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ Evaluates the expressions __expr__s in order and returns the value of the last.

_binding_ => _binding-form_ _init-expr_

Evaluates the expressions __expr__s in a lexical context in which the symbols in the __binding-form__s are bound to their respective __init-expr__s or parts therein. The bindings are sequential, so each _binding_ can see the prior bindings. The __expr__s are contained in an implicit `do`. If a _binding_ symbol is annotated with a metadata tag, the compiler will try to resolve the tag to a class name and presume that type in subsequent references to the _binding_. The simplest _binding-form_ is a symbol, which is bound to the entire _init-expr_:
Evaluates the expressions __expr__s in a lexical context in which the symbols in the __binding-form__s are bound to their respective __init-expr__s or parts therein. The bindings are sequential, so each _binding_ can see the prior bindings. The __expr__s are contained in an implicit `do`. If a _binding_ symbol is annotated with a metadata tag, the compiler will try to resolve the tag to a class name and presume that type in subsequent references to the _binding_.


The simplest _binding-form_ is a symbol, which is bound to the entire _init-expr_:

[source,clojure]
----
Expand All @@ -101,6 +104,17 @@ Evaluates the expressions __expr__s in a lexical context in which the symbols in
-> 1
----

If the _binding_ symbol metadata tag is a Java interface annotated as a https://docs.oracle.com/javase/8/docs/api/java/lang/FunctionalInterface.html[FunctionalInterface], the __init-expr__ will be coerced (if necessary) from the Clojure `IFn` interface to the specified interface:

[source,clojure]
----
(let [coll (java.util.ArrayList. (range 10))
^java.util.function.Predicate pred even?]
(.removeIf coll pred) ;; mutate coll
coll)
-> [1 3 5 7 9]
----

See <<special_forms#binding-forms,Binding Forms>> for more information about binding forms.

*Locals created with `let` are not variables. Once created their values never change!*
Expand Down

0 comments on commit ac501f4

Please sign in to comment.