Skip to content

Commit

Permalink
- Handled unconstrained class type parameter
Browse files Browse the repository at this point in the history
- Add test for change above
- Remove deprecated @method calls
  • Loading branch information
rjplevin committed Mar 5, 2019
1 parent 532fd65 commit e909155
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
3 changes: 2 additions & 1 deletion src/Classes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,8 @@ function _constructors(clsname, super, super_info, local_fields, all_fields, whe
push!(inits, init_local)
end

params = [clause.args[1] for clause in all_wheres] # extract parameter names from where clauses
# extract parameter names from where clauses
params = [(clause isa Expr ? clause.args[1] : clause) for clause in all_wheres]
has_params = length(params) != 0

args = _argnames(all_fields)
Expand Down
26 changes: 14 additions & 12 deletions test/test_classes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ end
end
end

# emitted by class Foo
# @method foo(obj::Foo) = obj.foo

@method function sum(obj::Bar)
function sum(obj::AbstractBar)
return obj.foo + obj.bar
end

Expand All @@ -63,31 +60,31 @@ z = Baz(100, 101, 102)
@test fieldnames(Bar) == (:foo, :bar)
@test fieldnames(Baz) == (:foo, :bar, :baz)

@method foo(x::Foo) = x.foo
foo(x::AbstractFoo) = x.foo

@test foo(x) == 1
@test foo(y) == 10
@test foo(z) == 100

@test sum(y) == 21

@method get_bar(x::Bar) = x.bar
get_bar(x::AbstractBar) = x.bar

@test get_bar(y) == 11
@test get_bar(z) == 101

@test_throws Exception get_bar(x)

# Mutable
@method set_foo!(x::Foo, value) = (x.foo = value)
set_foo!(x::AbstractFoo, value) = (x.foo = value)
set_foo!(z, 1000)
@test foo(z) == 1000

# Immutable
@test_throws Exception x.foo = 1000

# test that where clause is amended properly
@method zzz(obj::Foo, bar::T) where {T} = T
zzz(obj::AbstractFoo, bar::T) where {T} = T

@test zzz(x, :x) == Symbol
@test zzz(y, 10.6) == Float64
Expand Down Expand Up @@ -133,10 +130,6 @@ sub = SubTupleHolder{NT}(z, nt)
xyz = SubTupleHolder(sub, 10, 20, 30, (foo=111, bar=222))
@test xyz.nt.foo == 111 && xyz.nt.bar == 222 && xyz.foo == 10

# Test method structure)
# "First argument of method whatever must be explicitly typed"
@test_throws(LoadError, eval(Meta.parse("@method whatever(i) = i")))

@class Parameterized{T1 <: Foo, T2 <: Foo} begin
one::T1
two::T2
Expand All @@ -147,6 +140,15 @@ end
y::Float64
end

# non-constrained struct type parameter
@class mutable AbstractLog{T} begin
pos::Matrix{T}
end

obj = AbstractLog{Float64}([1. 2.; 3. 4.])

@test obj.pos == [1. 2.; 3. 4.]

# TBD: add tests on these

# Generated: needs work on parameterized types (T1, T2 are not defined)
Expand Down

0 comments on commit e909155

Please sign in to comment.