Repository navigation
Description
Following up on discussion with @molpopgen and @jeromekelleher regarding possibility of making tskit 1.0 const-correct (const-correct in the API being use of "const" in pointer parameter types declaration when API functions do not change the data pointed to).
I can't think of any downside for the callers of the API to have those parameter types get const'ed and I don't believe it will break any users (unless they are doing something unusual like keeping pointers to functions and assigning API functions to those function pointers).
In the public API, this is convenient to API users who use const often (e.g. @molpopgen and many C++ programmers). It is less annoying too. They don't have to cast away const-ness when calling the API.
However, for tskit implementors this can become a pain. This page has a nice description of the trade-off and uses the term
"const poisoning":
https://wiki.sei.cmu.edu/confluence/display/c/DCL00-C.+Const-qualify+immutable+objects
I can suggest initially only focusing on making the public API const-correct and not worrying about the broader use of const in the implementation of tskit. If there is a const issue introduced by this const type change, one can just blindly do something like this:
int public_api_func(double *values)
{
... code not "speaking in const" ...
}
goes to
int public_api_func(double const *_values)
{
double *values = (double*)_values;
... same code not "speaking in const" ...
}
And if there is no const issue, then no need to do this.
In a sense, this is putting the burden of casting away const-ness on the implementors of tskit rather than users who call it. Which seems to be the right place to put that burden.
Activity
benjeffery commentedon Sep 10, 2020
Yep, I agree this is a good thing to do. I've caught issues before by "constifiying" some C++ code. I think it also helps the API become a bit more self-documenting.
jeromekelleher commentedon Sep 10, 2020
OK, added to the C 1.0 milestone.
molpopgen commentedon Sep 10, 2020
While I am in favor of this in general, it could be a big load of work that goes nowhere. In my experience, it is hard to
constify a code base after the fact.C allows 3 distinct uses of
constin a function prototype. If the goal is to document to the user that theirinput data are guaranteed to be unmodified by the function, only one of the 3 uses satisfies that goal and is also
compatible with a C++ compiler.
Personally, I prefer
foo(const double * x)tofoo(double * const x). Given that msprime uses the GSL, and the former convention is used there, it may be confusing to have different flavors of const in play for people new to looking at the code.For the latter case, the user's input data are still mutable.
castedo commentedon Sep 16, 2020
Thanks @molpopgen for the example. I've incorporated it with some corrections into a reference page for "CNP #11" ... pronounced "see-nip" :)
http://www.castedo.com/cnp/11/
This page is half-humour, half-serious and I hope 100% fun for computational biologists.
There are some interesting facts about the
foo(const int * x)vsfoo(int const * x)variation, especially the Italian interpretation.Y'all have the best sense what typical msprime C library users might be most familiar with. I'm guessing it's the 'ancestral variant'. But the mutant variation is quite invasive and there seems to be some selection pressure for its spread. :)
If any of you would like a change or addition, just let me know. The source is currently at https://github.com/castedo/tskit-bud/tree/main/cnp
castedo commentedon Oct 15, 2020
So far there is one vote for "English style" adjective-noun
const double *and none for "Mediterranean style" noun-adjectivedouble const *. This probably matches best what is most commonly understood by most users of the C API.@benjeffery feel free to assign this to me.
castedo commentedon Nov 3, 2020
Done! Whew! Four merges! Thanks @jeromekelleher for reviewing. Not exactly the most exciting line code changes to read through. :-)
jeromekelleher commentedon Nov 3, 2020
Not exactly the most exciting code changes to make either, so thanks for the dedication!