-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
PHP type declarations #2027
Comments
Sounds a nice feature for PHP users. Is the plan to annotate using the PHP types rather than C/C++ ones? |
PHP type hints specify the allowed type(s). For PHP < 8, you can only specify one type, but there are some options to allow multiple types (e.g. My idea so far is to specify these via typemaps, so you might have:
(These |
@geographika After a quick look at Python annotations, perhaps the Python equivalent would look something like:
(Not sure how/if "None-able" types are specified/supported - PEP3107 seems to be mostly about the framework rather than how it's used and I couldn't see another PEP building on it.) If there's an approach that would work better for Python annotations and similarly well for PHP type hints I'm happy to discuss as having a common approach between languages has benefits, but soon would be good if this is to make SWIG 4.1. |
I've pushed some initial work to the php-type-hints branch. This annotates most of SWIG/PHP's More work is needed to properly handle overloaded functions (just enabling the current code would emit the type info for the last overloaded form of each overloaded function - that's incorrect, though doesn't cause the testsuite to fail - probably we need more We should also annotate with a particular class name rather than just "object" where appropriate. I've not attempted return type annotations yet - probably needs similar annotations on Probably And I've only attempted to do this for PHP8 so far as it seems to make sense to focus efforts there at least until we have this working for PHP8. PHP7 is less versatile as it doesn't allow alternatives in type specifications (only a single type though that can be nullable), or at least not if you write PHP code - I've been assuming that's true via the API though haven't checked. If that assumption is correct, to support this for PHP7 we'd have to essentially emit two arginfo versions for functions which take alternate types, and select the appropriate one at C/C++ compile time. PHP7.4 (the final 7.x) is now out of active upstream support and has security support for about 11 months (until 28 Nov 2022) so I'm leaning towards this being a PHP8+ only feature. |
Working on adding return type annotations, I've come across the issue that doing this can breaks PHP code subclasses wrapped classes, e.g.:
This clearly means we can't just turn this on all the time, and probably argues for allowing control of it per class or even per method. |
I've updated the branch and it now generates return type declarations for non-directed methods. To do:
|
It seems the PHP docs now call these "type declarations", which makes more sense as passing the wrong type gets you a TypeError exception, so they aren't just a "hint":
I wonder if we can support these for class properties. I'll add a note to the to do list above. |
I've gone for these being a So for example:
and
This is now handled internally via a lookup table from the PHP type name to the corresponding C constant name. |
I've got overloaded functions and automatically using class names (rather than just |
I've made the new generated code work with PHP 7 (tested with 7.4 locally as that's all I have to hand) and have re-enabled CI for all PHP versions. This works by simply defining the PHP API macros for specifying type declarations which were new in PHP 8.0 to the equivalents without type declarations if we're building for PHP 7.x. |
I should probably explicitly state that I'm currently viewing the php-type-hints branch as essentially a personal sandbox which I may rebase and force push without warning. I'm pushing it mostly to allow CI testing (particularly with more PHP versions than I have to hand) and to have an up-to-date remote backup of my work. I'd be delighted if anyone else is interested enough in this to collaborate (filling in missing |
1b22fef implements most of the missing |
After looking into it, I've concluded that |
I've investigated types properties, and as far as I can see these aren't going to be possible (unless PHP adds a mechanism). The key issue here is that we need to map to an existing place where the value is stored (e.g. a member variable in a struct or class. You can declare a typed property with The current way we implement PHP properties is by implementing the "magic" (PHP's term) methods An alternative way we could implement PHP properties is via the |
With C++ comments changed to C comments, these are now identical to the two cases just above, aside from the `2` suffix on the names. Follow-on for #2027.
Checking through the typemaps I spotted a few omissions in more obscure cases which I'll push fixes for. I also noticed that Looking at these typemaps there also looks to be a bug in |
Well, it's genuine, though not triggerable without writing a custom typemap as all the shipped It seems hard to fix without adding some sort of counting of how many |
152a24a adds support for The semantics of this are:
Currently I'm defaulting this in
@wsfulton Does that seem a reasonable approach? It seems to work, but I couldn't seem to find an existing I could make the feature work with the unset setting meaning "compat", but that seems less easy to understand than the semantics above. |
I've pushed a commit to document this in the PHP chapter of the manual. The remaining two items on the tick-list above don't need to block the merge of this - currently we're missing type declarations in these cases, but the fall back is that PHP accepts any type so everything works (the same as things work with PHP7 where we don't support type declarations at all). I'd really like to get feedback on how the feature to control this works, but that really just needs to be before 4.1.0 is released and doesn't need to block the merge of this, so I'll probably do a pass or two over these changes to look for anything I've missed and then merge so I can work on #2125 as that will touch some of the same places as these changes. |
(Splitting out from #1529)
Modern PHP supports type hints on function parameters and return types. It'd be nice if SWIG could add these (probably ideally automatically with potential to override manually.)
We require PHP7 now, so we don't need to worry about versions without type hints, but PHP8 added the ability to have or-ed hints, which has a different C API which complicates the code we'd need to generate.
I'm going to soft-target this at SWIG 4.1, as doing this in the same release as the other major PHP backend updates seems desirable. I may not have time, but we'll see. If the hints were opt-in it could probably go in any SWIG release, so maybe that's a better way to go.
The text was updated successfully, but these errors were encountered: