Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add generic to FnType representing type of the function #648

Merged
merged 3 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,7 @@ promote_symtype(f, Ts...) = Any
#### Function-like variables
#---------------------------

struct FnType{X<:Tuple,Y} end
struct FnType{X<:Tuple,Y,Z} end

(f::Symbolic{<:FnType})(args...) = Term{promote_symtype(f, symtype.(args)...)}(f, Any[args...])

Expand Down Expand Up @@ -1021,8 +1021,16 @@ function _name_type(x)
lhs, rhs = x.args[1:2]
if lhs isa Expr && lhs.head === :call
# e.g. f(::Real)::Unreal
if lhs.args[1] isa Expr
func_name_and_type = _name_type(lhs.args[1])
name = func_name_and_type.name
functype = func_name_and_type.type
else
name = lhs.args[1]
functype = Nothing
end
type = map(x->_name_type(x).type, lhs.args[2:end])
return (name=lhs.args[1], type=:($FnType{Tuple{$(type...)}, $rhs}))
return (name=name, type=:($FnType{Tuple{$(type...)}, $rhs, $functype}))
else
return (name=lhs, type=rhs)
end
Expand Down
19 changes: 18 additions & 1 deletion test/basics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ using Test

@testset "@syms" begin
let
@syms a b::Float64 f(::Real) g(p, h(q::Real))::Int
@syms a b::Float64 f(::Real) g(p, h(q::Real))::Int

@test issym(a) && symtype(a) == Number
@test a.name === :a
Expand All @@ -16,9 +16,11 @@ using Test

@test issym(f)
@test f.name === :f
@test symtype(f) == FnType{Tuple{Real}, Number, Nothing}

@test issym(g)
@test g.name === :g
@test symtype(g) == FnType{Tuple{Number, FnType{Tuple{Real}, Number, Nothing}}, Int, Nothing}

@test isterm(f(b))
@test symtype(f(b)) === Number
Expand All @@ -32,6 +34,21 @@ using Test
# issue #91
@syms h(a,b,c)
@test isequal(h(1,2,3), h(1,2,3))

@syms (f::typeof(max))(::Real, ::AbstractFloat)::Number a::Real
@test issym(f)
@test f.name == :f
@test symtype(f) == FnType{Tuple{Real, AbstractFloat}, Number, typeof(max)}
@test isterm(f(a, b))
@test symtype(f(a, b)) == Number

@syms g(p, (h::typeof(identity))(q::Real)::Number)::Number
@test issym(g)
@test g.name == :g
@test symtype(g) == FnType{Tuple{Number, FnType{Tuple{Real}, Number, typeof(identity)}}, Number, Nothing}
@test_throws "not a subtype of" g(a, f)
@syms (f::typeof(identity))(::Real)::Number
@test symtype(g(a, f)) == Number
end
end

Expand Down
Loading