Skip to content

Commit c46caf7

Browse files
committed
Update NEWS, README, CI and REQUIRE
Also revert disabled IsConcrete-test
1 parent ee53060 commit c46caf7

File tree

8 files changed

+31
-37
lines changed

8 files changed

+31
-37
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ os:
33
- linux
44
- osx
55
julia:
6-
- 0.6
76
- nightly
87
# matrix:
98
# allow_failures:

NEWS.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# 2018-02-19
2+
3+
Transitioned to where-function syntax. Dropped Julia 0.6 support,
4+
with the last release supporting it v0.5.2.
5+
16
# 2017-11-23
27

38
Dropped Julia 0.5 support, last release supporting it is v0.5.1

README.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
[![Build Status](https://ci.appveyor.com/api/projects/status/github/mauro3/SimpleTraits.jl?branch=master&svg=true)](https://ci.appveyor.com/project/mauro3/simpletraits-jl/branch/master)
55
[NEWS](NEWS.md)
66

7-
[![SimpleTraits](http://pkg.julialang.org/badges/SimpleTraits_0.5.svg)](http://pkg.julialang.org/?pkg=SimpleTraits)
87
[![SimpleTraits](http://pkg.julialang.org/badges/SimpleTraits_0.6.svg)](http://pkg.julialang.org/?pkg=SimpleTraits)
8+
[![SimpleTraits](http://pkg.julialang.org/badges/SimpleTraits_0.7.svg)](http://pkg.julialang.org/detail/SimpleTraits)
99

1010
This package provides a macro-based implementation of traits, using
1111
[Tim Holy's trait trick](https://github.com/JuliaLang/julia/issues/2345#issuecomment-54537633).
@@ -78,15 +78,15 @@ Notes:
7878

7979
It can be checked whether a type belongs to a trait with `istrait`:
8080
```julia
81-
using Base.Test
81+
using Test
8282
@test istrait(IsNice{Int})
8383
@test !istrait(BelongTogether{Int,Int}) # only BelongTogether{Int,String} was added above
8484
```
8585

8686
Functions which dispatch on traits are constructed like:
8787
```julia
88-
@traitfn f{X; IsNice{X}}(x::X) = "Very nice!"
89-
@traitfn f{X; !IsNice{X}}(x::X) = "Not so nice!"
88+
@traitfn f(x::X) where {X; IsNice{X}} = "Very nice!"
89+
@traitfn f(x::X) where {X; !IsNice{X}} = "Not so nice!"
9090
```
9191
This means that a type `X` which is part of the trait `IsNice` will
9292
dispatch to the method returning `"Very nice!"`, otherwise to the one
@@ -101,11 +101,11 @@ function. Thus there is no extra mental gymnastics required for a
101101

102102
Similarly for `BelongTogether` which has two parameters:
103103
```julia
104-
@traitfn f{X,Y; BelongTogether{X,Y}}(x::X,y::Y) = "$x and $y forever!"
104+
@traitfn f(x::X,y::Y) where {X,Y; BelongTogether{X,Y}} = "$x and $y forever!"
105105
@test f(5, "b")=="5 and b forever!"
106106
@test_throws MethodError f(5, 5)
107107

108-
@traitfn f{X,Y; !BelongTogether{X,Y}}(x::X,y::Y) = "$x and $y cannot stand each other!"
108+
@traitfn f(x::X,y::Y) where {X,Y; !BelongTogether{X,Y}} = "$x and $y cannot stand each other!"
109109
@test f(5, 5)=="5 and 5 cannot stand each other!"
110110
```
111111

@@ -172,8 +172,8 @@ Example:
172172
@traitdef Tr{X}
173173

174174
fn(x::Integer) = 1 # a normal method
175-
@traitfn fn{X<:AbstractFloat; Tr{X}}(x::X) = 2
176-
@traitfn fn{X<:AbstractFloat; !Tr{X}}(x::X) = 3
175+
@traitfn fn(x::X) where {X<:AbstractFloat; Tr{X}} = 2
176+
@traitfn fn(x::X) where {X<:AbstractFloat; !Tr{X}} = 3
177177

178178
@traitimpl Tr{Float32}
179179
@traitimpl Tr{Int} # this does not impact dispatch of `fn`
@@ -188,15 +188,15 @@ one trait. Continuing above example, this *does not work* as one may
188188
expect:
189189
```julia
190190
@traitdef Tr2{X}
191-
@traitfn fn{X<:AbstractFloat; Tr2{X}}(x::X) = 4
191+
@traitfn fn(x::X) where {X<:AbstractFloat; Tr2{X}} = 4
192192

193193
@traitimpl Tr2{Float16}
194194
fn(Float16(5)) # -> 4; dispatch through traits
195195
fn(Float32(5)) # -> MethodError; method defined in previous example
196196
# was overwritten above
197197
```
198198
This last definition of `fn` just overwrites the definition `@traitfn
199-
f{X; Tr{X}}(x::X) = 2` from above.
199+
f(x::X) where {X; Tr{X}} = 2` from above.
200200

201201
If you need to dispatch on several traits in a single trait-method,
202202
then you're out of luck. But please voice your grievance over in pull
@@ -327,7 +327,7 @@ Julia 0.5 one could use a generated function but not anymore in Julia 0.6.)
327327

328328
Note also that trait functions can be generated functions:
329329
```julia
330-
@traitfn @generated fg{X; IsNice{X}}(x::X) = (println(x); :x)
330+
@traitfn @generated fg(x::X) where {X; IsNice{X}} = (println(x); :x)
331331
```
332332

333333
# Innards
@@ -346,16 +346,16 @@ julia> macroexpand(:(@traitimpl Tr{Int}))
346346
SimpleTraits.trait{X1 <: Int}(::Type{Tr{X1}}) = Tr{X1}
347347
SimpleTraits.istrait{X1 <: Int}(::Type{Tr{X1}}) = true # for convenience, really
348348

349-
julia> macroexpand(:(@traitfn g{X; Tr{X}}(x::X)= x+1))
349+
julia> macroexpand(:(@traitfn g(x::X) where {X; Tr{X}}= x+1))
350350

351-
@inline g{X}(x::X) = g(trait(Tr{X}), x) # this is Tim's trick, using above grouping-function
352-
g{X}(::Type{Tr{X}},x::X) = x + 1 # this is the logic
351+
@inline g(x::X) where {X} = g(trait(Tr{X}), x) # this is Tim's trick, using above grouping-function
352+
g(::Type{Tr{X}},x::X) where {X} = x + 1 # this is the logic
353353

354-
julia> macroexpand(:(@traitfn g{X; !Tr{X}}(x::X)= x+1000))
354+
julia> macroexpand(:(@traitfn g(x::X) where {X; !Tr{X}}= x+1000))
355355

356356
# the trait dispatch helper function needn't be defined twice,
357357
# only the logic:
358-
g{X}(::Type{ Not{Tr{X}} }, x::X) = x + 1000
358+
g(::Type{ Not{Tr{X}} }, x::X) where {X} = x + 1000
359359
```
360360

361361
For a detailed explanation of how Tim's trick works, see
@@ -372,8 +372,8 @@ definitions.
372372
Example, dispatch on whether an argument is immutable or not:
373373

374374
```julia
375-
@traitfn f{X; IsImmutable{X}}(x::X) = X(x.fld+1) # make a new instance
376-
@traitfn f{X; !IsImmutable{X}}(x::X) = (x.fld += 1; x) # update in-place
375+
@traitfn f(x::X) where {X; IsImmutable{X}} = X(x.fld+1) # make a new instance
376+
@traitfn f(x::X) where {X; !IsImmutable{X}} = (x.fld += 1; x) # update in-place
377377

378378
# use
379379
mutable struct A; fld end

REQUIRE

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
julia 0.6
2-
Compat 0.26.0
1+
julia 0.7
32
MacroTools 0.3.2

appveyor.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
environment:
22
matrix:
3-
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x86/0.6/julia-0.6-latest-win32.exe"
4-
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x64/0.6/julia-0.6-latest-win64.exe"
53
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x86/julia-latest-win32.exe"
64
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x64/julia-latest-win64.exe"
75

src/SimpleTraits.jl

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ __precompile__()
22

33
module SimpleTraits
44
using MacroTools
5-
using Compat
65
const curmod = nameof(@__MODULE__)
76

87
import InteractiveUtils
@@ -189,7 +188,7 @@ end
189188
dispatch_cache = Dict() # to ensure that the trait-dispatch function is defined only once per pair
190189
let
191190
global traitfn
192-
function traitfn(tfn, cur_module)
191+
function traitfn(tfn, macro_module)
193192
# Need
194193
# f(x::X,Y::Y) where {X,Y} = f(trait(Tr1{X,Y}), x, y)
195194
# f(::False, x, y)= ...
@@ -338,7 +337,7 @@ let
338337
end
339338
# Create the trait dispatch function
340339
ex = fn
341-
key = (cur_module, fname, typs0, strip_kw(args0), trait0_opposite)
340+
key = (macro_module, fname, typs0, strip_kw(args0), trait0_opposite)
342341
if !(key keys(dispatch_cache)) # define trait dispatch function
343342
if !haskwargs
344343
ex = if oldfn_syntax
@@ -413,11 +412,7 @@ Defines a function dispatching on a trait. Examples:
413412
Note that the second example is just syntax sugar for `@traitfn f(x::X,y::Y) where {X,Y; Not{Tr1{X,Y}}} = ...`.
414413
"""
415414
macro traitfn(tfn)
416-
@static if isdefined(Base, Symbol("@__MODULE__"))
417-
esc(traitfn(tfn, __module__))
418-
else
419-
esc(traitfn(tfn, current_module()))
420-
end
415+
esc(traitfn(tfn, __module__))
421416
end
422417

423418
######

test/base-traits-inference.jl

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# Dict with base-traits to check using value[1] as type and value[2]
66
# as number of lines allowed in llvm code
77
cutoff = 5
8-
basetrs = [VERSION<v"0.7-" ? :IsLeafType=>:Int : :IsConcrete=>:Int,
8+
basetrs = [#VERSION<v"0.7-" ? :IsLeafType=>:Int : :IsConcrete=>:Int,
99
:IsBits=>:Int,
1010
:IsImmutable=>:Int,
1111
:IsContiguous=>:(SubArray{Int64,1,Array{Int64,1},Tuple{Array{Int64,1}},false}),
@@ -15,7 +15,6 @@ basetrs = [VERSION<v"0.7-" ? :IsLeafType=>:Int : :IsConcrete=>:Int,
1515
:IsCallable=>:(typeof(sin)),
1616
:IsIterator=>:(Dict{Int,Int})]
1717

18-
# for (bt, tp) in basetrs
19-
# @show bt
20-
# @test @eval @check_fast_traitdispatch $bt $tp true
21-
# end
18+
for (bt, tp) in basetrs
19+
@test @eval @check_fast_traitdispatch $bt $tp true
20+
end

test/base-traits.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using SimpleTraits.BaseTraits
2-
using Compat: view
32

43
@test istrait(IsAnything{Any})
54
@test istrait(IsAnything{Union{}})

0 commit comments

Comments
 (0)