Skip to content

Commit b88fb66

Browse files
committed
Define name function
1 parent 1660338 commit b88fb66

File tree

4 files changed

+20
-10
lines changed

4 files changed

+20
-10
lines changed

src/SymbolicUtils.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module SymbolicUtils
55

66
using DocStringExtensions
77

8-
export @syms, term, showraw, hasmetadata, getmetadata, setmetadata
8+
export @syms, term, showraw, hasmetadata, getmetadata, setmetadata, name
99

1010
using TermInterface
1111
using DataStructures

src/types.jl

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ function exprtype(x::BasicSymbolic)
6464
end
6565
end
6666

67+
function name(x::BasicSymbolic)
68+
x.impl.name
69+
end
70+
6771
# Same but different error messages
6872
@noinline error_on_type() = error("Internal error: unreachable reached!")
6973
@noinline error_sym() = error("Sym doesn't have a operation or arguments!")
@@ -308,7 +312,13 @@ end
308312
Base.one( s::Symbolic) = one( symtype(s))
309313
Base.zero(s::Symbolic) = zero(symtype(s))
310314

311-
Base.nameof(s::BasicSymbolic) = issym(s) ? s.impl.name : error("None Sym BasicSymbolic doesn't have a name")
315+
function Base.nameof(s::BasicSymbolic)
316+
if issym(s)
317+
name(s)
318+
else
319+
error("None Sym BasicSymbolic doesn't have a name")
320+
end
321+
end
312322

313323
## This is much faster than hash of an array of Any
314324
hashvec(xs, z) = foldr(hash, xs, init=z)
@@ -986,7 +996,7 @@ showraw(t) = showraw(stdout, t)
986996

987997
function Base.show(io::IO, v::BasicSymbolic)
988998
@match v.impl begin
989-
Sym(_...) => Base.show_unquoted(io, v.impl.name)
999+
Sym(_...) => Base.show_unquoted(io, name(v))
9901000
Const(_...) => print(io, v.impl.val)
9911001
_ => show_term(io, v)
9921002
end

test/basics.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ using Test
99
@syms a b::Float64 f(::Real) g(p, h(q::Real))::Int
1010

1111
@test issym(a) && symtype(a) == Number
12-
@test a.impl.name === :a
12+
@test name(a) === :a
1313

1414
@test issym(b) && symtype(b) == Float64
1515
@test nameof(b) === :b
1616

1717
@test issym(f)
18-
@test f.impl.name === :f
18+
@test name(f) === :f
1919
@test symtype(f) == FnType{Tuple{Real}, Number, Nothing}
2020

2121
@test issym(g)
22-
@test g.impl.name === :g
22+
@test name(g) === :g
2323
@test symtype(g) == FnType{Tuple{Number, FnType{Tuple{Real}, Number, Nothing}}, Int, Nothing}
2424

2525
@test isterm(f(b))
@@ -37,14 +37,14 @@ using Test
3737

3838
@syms (f::typeof(max))(::Real, ::AbstractFloat)::Number a::Real
3939
@test issym(f)
40-
@test f.impl.name == :f
40+
@test name(f) == :f
4141
@test symtype(f) == FnType{Tuple{Real, AbstractFloat}, Number, typeof(max)}
4242
@test isterm(f(a, b))
4343
@test symtype(f(a, b)) == Number
4444

4545
@syms g(p, (h::typeof(identity))(q::Real)::Number)::Number
4646
@test issym(g)
47-
@test g.impl.name == :g
47+
@test name(g) == :g
4848
@test symtype(g) == FnType{Tuple{Number, FnType{Tuple{Real}, Number, typeof(identity)}}, Number, Nothing}
4949
@test_throws "not a subtype of" g(a, f)
5050
@syms (f::typeof(identity))(::Real)::Number

test/types.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,11 @@ end
112112
@test typeof(s1) == BasicSymbolic{Int64}
113113
@test s1.metadata == SymbolicUtils.NO_METADATA
114114
@test s1.hash[] == SymbolicUtils.EMPTY_HASH
115-
@test s1.impl.name == :x
115+
@test name(s1) == :x
116116
@test typeof(s2) == BasicSymbolic{Float64}
117117
@test s2.metadata == SymbolicUtils.NO_METADATA
118118
@test s2.hash[] == SymbolicUtils.EMPTY_HASH
119-
@test s2.impl.name == :y
119+
@test name(s2) == :y
120120
end
121121
@testset "Term" begin
122122
s1 = _Sym(Float64, :x)

0 commit comments

Comments
 (0)