Skip to content

Commit

Permalink
Add getproperty method
Browse files Browse the repository at this point in the history
  • Loading branch information
juliohm committed Jul 15, 2020
1 parent 45882ab commit faeaff2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
26 changes: 15 additions & 11 deletions src/composition.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,21 @@ Names of parts in the composition `c`.
"""
names(c::Composition{D,SYMS}) where {D,SYMS} = SYMS

# """
# getproperty(c, name)
#
# Return the value of part with given `name` in the composition `c`.
# """
# function getproperty(c::Composition{D,SYMS}, S::Symbol) where {D,SYMS}
# for (i, SYM) in enumerate(SYMS)
# S == SYM && (return c.parts[i])
# end
# @error "invalid part $S"
# end
"""
getproperty(c, name)
Return the value of part with given `name` in the composition `c`.
"""
function getproperty(c::Composition{D,SYMS}, S::Symbol) where {D,SYMS}
if S == :parts
getfield(c, :parts)
else
for (i, SYM) in enumerate(SYMS)
S == SYM && (return c.parts[i])
end
@error "invalid part name '$S'"
end
end

# ------------
# IO methods
Expand Down
6 changes: 6 additions & 0 deletions test/compositions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,10 @@
@test names(-c) == names(c)
@test names(c - c) == names(c)
@test names(2c) == names(c)

# get part by name
c = Composition(a=3,b=2,c=1)
@test c.a == 3
@test c.b == 2
@test c.c == 1
end

0 comments on commit faeaff2

Please sign in to comment.