Skip to content

Commit ac501f4

Browse files
committed
add let example for functional interface coercion
1 parent 7b0f34e commit ac501f4

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

content/reference/special_forms.adoc

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,10 @@ Evaluates the expressions __expr__s in order and returns the value of the last.
9191

9292
_binding_ => _binding-form_ _init-expr_
9393

94-
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_:
94+
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_.
95+
96+
97+
The simplest _binding-form_ is a symbol, which is bound to the entire _init-expr_:
9598

9699
[source,clojure]
97100
----
@@ -101,6 +104,17 @@ Evaluates the expressions __expr__s in a lexical context in which the symbols in
101104
-> 1
102105
----
103106

107+
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:
108+
109+
[source,clojure]
110+
----
111+
(let [coll (java.util.ArrayList. (range 10))
112+
^java.util.function.Predicate pred even?]
113+
(.removeIf coll pred) ;; mutate coll
114+
coll)
115+
-> [1 3 5 7 9]
116+
----
117+
104118
See <<special_forms#binding-forms,Binding Forms>> for more information about binding forms.
105119

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

0 commit comments

Comments
 (0)