[Python-ideas] 'const' and 'require' statements (original) (raw)

Haoyi Li haoyi.sg at gmail.com
Fri Jan 18 08:06:41 CET 2013


Compiler-enforced immutability is one of those really hard problems which, if you manage to do flexibly and correctly, would be an academically publishable result, not something you hack into the interpreter over a weekend.

If you go the dumb-and-easy route, you end up with a simple "sub this variable with constant" thing, which isn't very useful (what about calculated constants?)

If you go the slightly-less-dumb route, you end up with some mini-language to work with these const values, which has some operations but not the full power of python. This basically describes C Macros, which I don't think you'd want to include in python!

If you go the "full python" route, you basically branch into two possibilities.

In any case, either you get a crappy implementation that nobody wants (C Macros) something that doesn't really give the guarantees you'd hope for (java final/c++ const) or you would have a publishable result w.r.t. either effect-tracking (!) or multi-stage computations (!!!).

Even though it is very easy to describe the idea (it just stops it from changing, duh!) and how it would work in a few trivial cases, doing it properly will likely require some substantial theoretical breakthroughs before it can actually happen.

On Thu, Jan 17, 2013 at 10:31 PM, Chris Angelico <rosuav at gmail.com> wrote:

On Fri, Jan 18, 2013 at 3:52 PM, Steven D'Aprano <steve at pearwood.info> wrote: > Another question: what happens if the constant expression can't be > evaluated until runtime? > > x = random.random() > const k = x + 1 > > y = k - 1 > > What value should the compiler substitute for y?

That should be disallowed. In the declaration of a constant, you have to use only what can be handled by the constants evaluator. As a rule of thumb, it'd make sense to be able to use const with anything that could safely be evaluated by ast.literaleval. As to the issues of rebinding, I'd just state that all uses of a particular named constant evaluate to the same object, just as would happen if you used any other form of name binding. I don't have the post to hand, but wasn't there a project being discussed recently that would do a lot of that work automatically? ChrisA


Python-ideas mailing list Python-ideas at python.org http://mail.python.org/mailman/listinfo/python-ideas -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130117/3b8cf182/attachment.html>



More information about the Python-ideas mailing list