-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathbase-traits.jl
66 lines (49 loc) · 1.8 KB
/
base-traits.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
module BaseTraits
using SimpleTraits
using Compat
export IsLeafType, IsBits, IsImmutable, IsContiguous, IsIndexLinear,
IsAnything, IsNothing, IsCallable, IsIterator
"Trait which contains all types"
@traitdef IsAnything{X}
@traitimpl IsAnything{X} <- (x->true)(X)
"Trait which contains no types"
@compat const IsNothing{X} = Not{IsAnything{X}}
"Trait of all isbits-types"
@traitdef IsBits{X}
@traitimpl IsBits{X} <- isbits(X)
"Trait of all immutable types"
@traitdef IsImmutable{X}
@traitimpl IsImmutable{X} <- (X->!X.mutable)(X)
"Trait of all callable objects"
@traitdef IsCallable{X}
@traitimpl IsCallable{X} <- (X->(X<:Function || length(methods(X))>0))(X)
"Trait of all leaf types types"
@traitdef IsLeafType{X}
@traitimpl IsLeafType{X} <- isleaftype(X)
"Types which have contiguous memory layout"
@traitdef IsContiguous{X} # https://github.com/JuliaLang/julia/issues/10889
@traitimpl IsContiguous{X} <- Base.iscontiguous(X)
"Array indexing trait."
@traitdef IsIndexLinear{X} # https://github.com/JuliaLang/julia/pull/8432
function isindexlinear(X)
if IndexStyle(X)==IndexLinear()
return true
elseif IndexStyle(X)==IndexCartesian()
return false
else
error("Not recognized")
end
end
@traitimpl IsIndexLinear{X} <- isindexlinear(X)
# TODO
## @traitdef IsArray{X} # use for any array like type in the sense of container
## # types<:AbstractArray are automatically part
## @traitdef IsMartix{X} # use for any LinearOperator
## # types<:AbstractArray are automatically part
Base.@deprecate_binding IsFastLinearIndex IsIndexLinear
"Trait of all iterator types"
@traitdef IsIterator{X}
@generated function SimpleTraits.trait{X}(::Type{IsIterator{X}})
method_exists(start, Tuple{X}) ? :(IsIterator{X}) : :(Not{IsIterator{X}})
end
end # module