@@ -56,8 +56,16 @@ symtype(x) = typeof(x)
5656
5757symtype (:: Symbolic{T} ) where {T} = T
5858
59+ """
60+ metadata(s)
61+ Get all the metadata of a term or `nothing` if no metadata is defined.
62+
63+ """
64+ metadata (s:: Symbolic ) = s. metadata
65+ metadata (s:: Any ) = nothing
66+
5967function hasmetadata (s:: Symbolic , ctx)
60- s . metadata isa AbstractDict && haskey (s . metadata, ctx)
68+ metadata (s) isa AbstractDict && haskey (metadata (s) , ctx)
6169end
6270
6371function getmetadata (s:: Symbolic , ctx)
@@ -388,7 +396,7 @@ function term(f, args...; type = nothing)
388396end
389397
390398"""
391- similarterm(t, f, args, symtype)
399+ similarterm(t, f, args, symtype; metadata=nothing )
392400
393401Create a term that is similar in type to `t`. Extending this function allows packages
394402using their own expression types with SymbolicUtils to define how new terms should
@@ -403,9 +411,9 @@ different type than `t`, because `f` also influences the result.
403411- The `symtype` of the resulting term. Best effort will be made to set the symtype of the
404412 resulting similar term to this type.
405413"""
406- similarterm (t, f, args, symtype) = f (args... )
407- similarterm (t, f, args) = similarterm (t, f, args, _promote_symtype (f, args))
408- similarterm (t:: Term , f, args) = Term {_promote_symtype(f, args)} (f, args)
414+ similarterm (t, f, args, symtype; metadata = nothing ) = f (args... )
415+ similarterm (t, f, args; metadata = nothing ) = similarterm (t, f, args, _promote_symtype (f, args); metadata = nothing )
416+ similarterm (t:: Term , f, args; metadata = nothing ) = Term {_promote_symtype(f, args)} (f, args; metadata = metadata )
409417
410418node_count (t) = istree (t) ? reduce (+ , node_count (x) for x in arguments (t), init= 0 ) + 1 : 1
411419
@@ -917,18 +925,18 @@ end
917925const NumericTerm = Union{Term{<: Number }, Mul{<: Number },
918926 Add{<: Number }, Pow{<: Number }}
919927
920- function similarterm (p:: NumericTerm , f, args, T= nothing )
928+ function similarterm (p:: NumericTerm , f, args, T= nothing ; metadata = nothing )
921929 if T === nothing
922930 T = _promote_symtype (f, args)
923931 end
924932 if f === (+ )
925- Add (T, makeadd (1 , 0 , args... )... )
933+ Add (T, makeadd (1 , 0 , args... )... ; metadata = metadata )
926934 elseif f == (* )
927- Mul (T, makemul (1 , args... )... )
935+ Mul (T, makemul (1 , args... )... ; metadata = metadata )
928936 elseif f == (^ ) && length (args) == 2
929- Pow {T, typeof.(args)...} (args... )
937+ Pow {T, typeof.(args)...} (args... ; metadata = metadata )
930938 else
931- Term {T} (f, args)
939+ Term {T} (f, args; metadata = metadata )
932940 end
933941end
934942
0 commit comments