-
-
Notifications
You must be signed in to change notification settings - Fork 57
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
Error using generic with DB::Serializable
and @[DB::Field(converter: MyConverter(T))]
#189
Comments
DB::Serializable
and @[DB::Field(converter: T)]
DB::Serializable
and @[DB::Field(converter: MyConverter(T))]
I don't think annotations can reference generic type arguments. Not sure if that's just an omission or if there are any reasons that prevent implementintg this. |
They can. This code compiles and runs: annotation MyAnnotation
end
struct Foo(T)
@[MyAnnotation(converter: OMG(T))]
getter bar : T
def initialize(@bar)
end
end
pp Foo(String).new("asdf").bar
# => "asdf" My understanding of annotations is that they're arbitrary expressions for macros to inject into your code and the compiler doesn't seem to do anything with them otherwise. Whether it actually does, I can't tell — I'm mainly going off of observed behavior. Is there a way to output the full code in a file after all macros are expanded — like |
Right. It's not the annotation itself that errors, it's the code that's generated for it. The reported location is a bit confusing. I believe the problem is that the converter path expands in |
Oh, right, it's not defined in the constructor that gets defined in the |
A reduced example to demonstrate the issue looks like this: module Foo
def initialize
@id = T.new # Error: undefined constant T
end
end
class Bar(T)
include Foo
end
Bar(String).new In the case of module Foo
def initialize
{% for var in @type.instance_vars %}
@{{ var.name }} = {{ var.type }}.new
{% end %}
end
end
class Bar(T)
include Foo
@id : T
end
Bar(String).new The previous example doesn't make any sense because |
As a workaround you should be able to replace @[DB::Field(converter: Event::Converter(typeof(raw)))] |
I've got a DB model with some metadata and an arbitrary JSON payload (event data from webhooks) that looks something like this:
I'm getting this error at compile time:
I don't know what's causing it. This seems like it should work and the macro seems to be generating the correct code:
That's the only place the
value[:converter]
is used inDB::Serializer
.The text was updated successfully, but these errors were encountered: