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

Constraint checking on dictionary-class converter #31

Open
phdum opened this issue Jan 20, 2020 · 2 comments
Open

Constraint checking on dictionary-class converter #31

phdum opened this issue Jan 20, 2020 · 2 comments

Comments

@phdum
Copy link
Member

phdum commented Jan 20, 2020

Hi,

I wanted to run an idea by you to see if it would be interesting thing to implement.

I remember that when you have a convertor from python to cpp (via CPP2PY_ARG_AS_DICT), it does type checking of the input to make sure everything is well defined.

Would it also be possible to specify additional constraints / bounds / traits during this conversion?

For example, it would be nice to specify something that a parameter (e.g. temperature) should be > 0. This is then checked on conversion and gives an error message if it fails.

Or would this cause other problems?

Thanks!

@Wentzell
Copy link
Member

Wentzell commented Jul 9, 2020

Why would you want to make this part of the conversion and not check these things in the actual function body?

@phdum
Copy link
Member Author

phdum commented Jul 10, 2020

I'm sure are several design options here (including ones I've not thought of). I view this as a pre-condition (expects) / post-condition (ensures) question:

The way the current interface (CPP2PY_ARG_AS_DICT) seems designed with the idea of always passing a single parameters to a single function. Here it is natural a pre-condition (expects) check on that input in the function. I guess this is the way you phrased the question above.

However we could also think of the conversion as a (complicated) constructor from of the c++ class inputting a python dict. Then it makes sense to think about checking validity as a post-condition on the ctor (ensures). But right now, I don't know if the user can specify such a post-condition.

[Personally, I like to separate conversion (ctor) and passing the data to a separate function. This is also why I suggest the keyword changes in #30. But this is not so important here.]

Pre-condition/post-condition is in a sense a matter of taste.

  • The advantage of pre-conditions for a function, is that is where the input is used and the code logic is.
  • The advantage of post-conditions in the ctor is that if there is a failure due to invalid input, it fails very quickly, without anything else being invoked. Especially, if you pass a dictionary of data to the constructor of another class, you want to do the checks before resource allocation. For example, since there is no languages support for contracts, I actually don't know any nice way to call an expects function before allocating (potentially large) resources.

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

No branches or pull requests

2 participants