Skip to content

Commit c70db59

Browse files
authored
doc: add section about function types in the Types section (#41855)
1 parent e8faf9d commit c70db59

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

doc/src/manual/types.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,6 +1108,50 @@ julia> NoFieldsParam{Int}() === NoFieldsParam{Int}()
11081108
true
11091109
```
11101110

1111+
## Types of functions
1112+
1113+
Each function has its own type, which is a subtype of `Function`.
1114+
1115+
```jldoctest foo41
1116+
julia> foo41(x) = x + 1
1117+
foo41 (generic function with 1 method)
1118+
1119+
julia> typeof(foo41)
1120+
typeof(foo41) (singleton type of function foo41, subtype of Function)
1121+
```
1122+
1123+
Note how `typeof(foo41)` prints as itself. This is merely a convention for printing, as it is a first-class object that can be used like any other value:
1124+
1125+
```jldoctest foo41
1126+
julia> T = typeof(foo41)
1127+
typeof(foo41) (singleton type of function foo41, subtype of Function)
1128+
1129+
julia> T <: Function
1130+
true
1131+
```
1132+
1133+
Types of functions defined at top-level are singletons. When necessary, you can compare them with [`===`](@ref).
1134+
1135+
[Closures](@id man-anonymous-functions) also have their own type, which is usually printed with names that end in `#<number>`. Names and types for functions defined at different locations are distinct, but not guaranteed to be printed the same way across sessions.
1136+
1137+
```jldoctest; filter = r"[0-9\.]+"
1138+
julia> typeof(x -> x + 1)
1139+
var"#9#10"
1140+
```
1141+
1142+
Types of closures are not necessarily singletons.
1143+
1144+
```jldoctest
1145+
julia> addy(y) = x -> x + y
1146+
addy (generic function with 1 method)
1147+
1148+
julia> Base.issingletontype(addy(1))
1149+
false
1150+
1151+
julia> addy(1) === addy(2)
1152+
false
1153+
```
1154+
11111155
## [`Type{T}` type selectors](@id man-typet-type)
11121156

11131157
For each type `T`, `Type{T}` is an abstract parametric type whose only instance is the

0 commit comments

Comments
 (0)