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

@functor for parametric constructors #40

Open
theogf opened this issue Apr 19, 2022 · 5 comments
Open

@functor for parametric constructors #40

theogf opened this issue Apr 19, 2022 · 5 comments

Comments

@theogf
Copy link

theogf commented Apr 19, 2022

Suppose I have a type

using Functors
struct Foo{T1,T2}
  x::T2
  Foo{T1}(x::T2) where {T1,T2} = new{T1,T2}(x)
end

Then using

@functor Foo

will not manage to capture the T1 type:

x = Foo{Real}(2.0)
y, re = Functors.functor(x)
re(y)
ERROR: MethodError: no method matching Foo(::Float64)
Stacktrace:
 [1] (::var"#5#6")(y::NamedTuple{(:x,), Tuple{Float64}})
   @ Main ~/.julia/packages/Functors/qBIlC/src/functor.jl:23
 [2] top-level scope
   @ REPL[14]:1

using

@functor Foo{T} where {T}

is not helping more

@theogf
Copy link
Author

theogf commented Apr 19, 2022

I suppose the solution is to dispatch on the makefunctor function?

@DhairyaLGandhi
Copy link
Member

You could, although it is also possible to simply make a constructor that can additionally take the parametric types or not using internal constructors if possible.

@devmotion
Copy link

Can't you use the same approach as in #6 (comment)?

@theogf
Copy link
Author

theogf commented Apr 19, 2022

Yeah, that's cleaner than dispatching makefunctor.
Still it would be nice to accept the syntax @functor Foo{T} where {T}.

@ToucheSir
Copy link
Member

If any macro wizard knows how to do this, would be happy to take a PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants