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

Add "return_type()" to Constructor #1490

Closed

Conversation

arg0d
Copy link
Contributor

@arg0d arg0d commented Mar 10, 2023

This makes Constructor more nominal with Function, i.e. the constructor can be used as a function in macros:

{% macro(func) %}
    {{ func.return_type() }}
{% endmacro %}

{% call macro(constructor) %}

This is very useful for generating Go bindings, because in Go errors are handled via multiple return values, instead of exceptions. That means that codegen for function return signatures get quite complicated, and being able to re-use the same code for constructors as for regular functions makes it much easier to generate code.

This makes Constructor more nominal with Function, i.e. the constructor
can be used as a function in macros:
```
{% macro(func) %}
    {{ func.return_type() }}
{% endmacro %}

{% call macro(constructor) %}
```

This is very useful for generating Go bindings, because in Go errors
are handled via multiple return values, instead of exceptions. That
means that codegen for function return signatures get quite
complicated, and being able to re-use the same code for constructors as
for regular functions makes it much easier to generate code.
@arg0d arg0d requested a review from a team as a code owner March 10, 2023 15:53
@arg0d arg0d requested review from tarikeshaq and removed request for a team March 10, 2023 15:53
@bendk
Copy link
Contributor

bendk commented Mar 10, 2023

I was going to mention this when I looked at the C# bindings, but what do you think about adding a Callable trait to try to unify functions, methods, and constructors? I have some code to do that in an in-progress PR. A trait is nicer to use in Rust code, like the filter functions.

@arg0d
Copy link
Contributor Author

arg0d commented Mar 13, 2023

Callable trait sounds good. I'm gonna try and add it.

@bendk
Copy link
Contributor

bendk commented Mar 15, 2023

Looks good to me, but I don't want to merge it myself, since that's sort-of like reviewing my own code. @tarikeshaq what do you think of this approach?

Copy link
Contributor

@tarikeshaq tarikeshaq left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank's a ton for contributing back, I'm super excited to see more backends (and a go backends sounds phenomenal!)

Had a few nit but this Makes sense to me as long as it's useful for external backends!

@@ -290,6 +295,9 @@ impl Constructor {
self.ffi_func.name = format!("{ci_prefix}_{obj_name}_{}", self.name);
self.ffi_func.arguments = self.arguments.iter().map(Into::into).collect();
self.ffi_func.return_type = Some(FfiType::RustArcPtr(obj_name.to_string()));

// this is a bit of a dirty place to put this, but there isn't another "general" pass
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we expand why this is not an ideal place? Inline comment is great, but if we have a more actionable way of making it more ideal then a bug filed with more context would be phenomenal!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#1469 adds an object_name field to Constructor, which would avoid this. Does that make sense for the long-term plan?

@@ -249,6 +249,42 @@ impl APIConverter<Argument> for weedle::argument::SingleArgument<'_> {
}
}

/// Implemented by function-like types (Function, Method, Constructor)
pub trait Callable {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

out of curiosity, why is the arguments API return a vec of references where the other two return owned Type?

Is it because it was hard to have then all return references without overcomplicating the code?

Just curious!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additionally, would we like to add a name function too?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

out of curiosity, why is the arguments API return a vec of references where the other two return owned Type?

I think that was copied from the #1469 implementation. I didn't put a lot of thought into it, but that seemed like the path of least resistance to me. In general, it seems like we tend to clone Type rather than try to manage a reference to it. I'm not sure if that's the best policy going forward or not.

Additionally, would we like to add a name function too?

Maybe? I'm not totally sure though, there could be callables that don't have a name. For example a destructor. Also, the primary constructor currently has a name field, but it's just hard coded to "new". That said, I'm not really against it I just didn't put the time into defining it.

@bendk
Copy link
Contributor

bendk commented Mar 31, 2023

#1469 ended up being merged before this one, should we just close this PR?

@arg0d
Copy link
Contributor Author

arg0d commented Apr 3, 2023

Yeap, #1469 looks good to me.

@arg0d arg0d closed this Apr 3, 2023
@arg0d arg0d deleted the krsitupas/add-constructor-return-type branch October 5, 2023 12:02
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

Successfully merging this pull request may close these issues.

3 participants