Title: Constructor failing argument matching, silently selects default constructor · Issue #238 · pythonnet/pythonnet · GitHub
Open Graph Title: Constructor failing argument matching, silently selects default constructor · Issue #238 · pythonnet/pythonnet
X Title: Constructor failing argument matching, silently selects default constructor · Issue #238 · pythonnet/pythonnet
Description: (Version 2.1) Given .NET class: public class A { public A() { a=1.0;} public A(double ax) { a=ax;} public double a; } then in python: from Xxx import A a1 = A(2) # surprise! default constructor is selected, silently, basically because # ...
Open Graph Description: (Version 2.1) Given .NET class: public class A { public A() { a=1.0;} public A(double ax) { a=ax;} public double a; } then in python: from Xxx import A a1 = A(2) # surprise! default constructor is ...
X Description: (Version 2.1) Given .NET class: public class A { public A() { a=1.0;} public A(double ax) { a=ax;} public double a; } then in python: from Xxx import A a1 = A(2) # surprise! default constructor is ...
Opengraph URL: https://github.com/pythonnet/pythonnet/issues/238
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Constructor failing argument matching, silently selects default constructor","articleBody":"(Version 2.1)\n\nGiven .NET class:\n\n``` C#\n public class A {\n\n public A() { a=1.0;}\n\n public A(double ax) { a=ax;}\n\n public double a;\n\n }\n```\n\nthen in python:\n\n``` python\nfrom Xxx import A\n\na1 = A(2) # surprise! default constructor is selected, silently, basically because \n # there is no int-\u003edouble type promotion in constructor binder\n # causing first bind to fail, then second bind with no args (that was a fix\n # to allow .NET objects to be super-classes (or be sub-classed) from\n # python succeeds..\na2 = A(2.0) # Ok, now we get the second constructor, because it's a perfect match.\n```\n\nI am fully aware that super-classes, argument-matching, (automatic) type-conversion,\n and bridging the gap between python and .NET (C#) , is very complex.\nAnd providing a full implementation that 'just do the thing we want' is not trivial.\n\nIt might be that some of the issues related to sub-classing, as well as argument matching/conversion is related, - but I hope the simple example above illustrates that\nsome minor improvement at this stage could be quite useful.\n\nThere is a few fixes, that could improve this slightly:\n\n(a.) Make simple type-promotion/argument conversion happen during constructor argument matching\n(b.) If construction of a .NET object is attempted, and argument match fail, then always raise Type error\n\nI will provide a PR for (a.) since it's a simple fix to verify a simple type-promotion from int-\u003edouble etc.\n\nFixing (b.), raise error if no .NET constructor matches the arguments is easy, but will then break the sub-class mechanism. If we knew, by the time we are calling the .NET constructor, that it's a super-class, then we _could_ either have a relaxed matching (as is to day, default to the default-ct, if fail), and then insist on match for all other cases.\n\nAfter all, providing something that fixes (b.), and at the same time supports, and possibly extends super()/sub-classing mechanism seems harder to me, since we need to know the context in which the .NET object is constructed.\n\nIf anyone see a easy solution to (b.) , - as in knowing how to get the context of the .NET class construction, that would be great.\n\nWe could then provide a fix where \n\n' you succeed with your intention creating a new object with the supplied parameters, and get the expected result, or we raise Type-exception notifying about the problem' \n","author":{"url":"https://github.com/sigbjorn","@type":"Person","name":"sigbjorn"},"datePublished":"2016-07-03T22:09:47.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":9},"url":"https://github.com/238/pythonnet/issues/238"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:c3ef884b-8961-c13a-92bf-11ca3bf5050c |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E3CE:35935A:2AFE75:39AB07:697155A7 |
| html-safe-nonce | 80ee83b78e75768f03816e9fb4fd0a40a603f42eb9b6ecac99d8653aa01eaaf5 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFM0NFOjM1OTM1QToyQUZFNzU6MzlBQjA3OjY5NzE1NUE3IiwidmlzaXRvcl9pZCI6IjUzODAzMjY4Nzk0NTU2OTYyOTUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | e47338c95c4f77efa14bc428b72d0dc378ae7ddfb3291cbdfc9eefafdcfdd4f3 |
| hovercard-subject-tag | issue:163582259 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/pythonnet/pythonnet/238/issue_layout |
| twitter:image | https://opengraph.githubassets.com/d074ce529fec93adcc7ebb2fc02cb44a069d6150efeeb592ad5b31c4993fd651/pythonnet/pythonnet/issues/238 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/d074ce529fec93adcc7ebb2fc02cb44a069d6150efeeb592ad5b31c4993fd651/pythonnet/pythonnet/issues/238 |
| og:image:alt | (Version 2.1) Given .NET class: public class A { public A() { a=1.0;} public A(double ax) { a=ax;} public double a; } then in python: from Xxx import A a1 = A(2) # surprise! default constructor is ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | sigbjorn |
| hostname | github.com |
| expected-hostname | github.com |
| None | fd5876ec68f4a61d42f4010f6d1b2764d8ae314777de44028a7708a1d7bac1b5 |
| turbo-cache-control | no-preview |
| go-import | github.com/pythonnet/pythonnet git https://github.com/pythonnet/pythonnet.git |
| octolytics-dimension-user_id | 6050430 |
| octolytics-dimension-user_login | pythonnet |
| octolytics-dimension-repository_id | 14748123 |
| octolytics-dimension-repository_nwo | pythonnet/pythonnet |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 14748123 |
| octolytics-dimension-repository_network_root_nwo | pythonnet/pythonnet |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8f48ed2345879fb640360c3a24f1d1780c3ed3cb |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width