Closed paulswartz closed 1 year ago
Hi, @paulswartz!
Thanks for raising this! It's an interesting case since it highlights that it's possible to define a function with the ::/2
name in a module with the use
/__using__
mechanism, but it's not possible to import such a function from another module. I'm not sure yet what the long-term solution to this should be... or if anything has to be done about it at all 🤔 We probably have to gather some more experience of using this in practice.
A workaround for the short-term, though, is to import with the except
keyword:
defmodule AnnotationTest.ImportExample do
import AnnotationTest, except: ["::": 2]
end
If we still want to use annotations in AnnotationTest.ImportExample
, we can use Gradient.TypeAnnotation
to enable them.
This pointed me at another potential solution: what about making ::/2
and :::/2
defp
instead?
# lib/gradient/type_annotation.ex
@compile {:inline, "::": 2, ":::": 2}
defp unquote(:"::")(expr, _type), do: expr
defp unquote(:":::")(expr, _type), do: expr
Good idea, @paulswartz! Indeed, it seems to fix the problem. Thanks again for raising this and for coming up with a fix 🌟
I wrote up a quick example in https://github.com/paulswartz/gradient/commit/64ba4f5f74f32e3b3426a285e8bf55165d730277, but I get this compilation error trying to run
import Annotations.ShouldPass
:Thank you for this tool; I'm excited to see where it goes!