On 6/21/06, Phillip J. Eby <[EMAIL PROTECTED]> wrote:
> At 01:16 PM 6/21/2006 -0700, Guido van Rossum wrote:
> >Yeah, but if you have names for your constants it would be a shame if
> >you couldn't use them because they happen to be defined in the same
> >scope.
>
> Maybe the real answer is to have a "const" declaration, not necessarily the
> way that Fredrik suggested, but a way to pre-declare constants e.g.:
>
>      const FOO = 27

The problem with this is that I don't see how to export this property
from one module to another. If module A has the above statement and
module B does "from A import FOO" then how does the parser know that
FOO is a constant when it's parsing B? (Remeber the parser only sees
one module at a time; I don't want to drop this separate compilation
facility.) It seems you would still end up with lots of duplicate
const declarations (e.g. "from A import const FOO"). And it should
also be possible to say "import A" and then use "A.FOO" as a constant.

I really don't think this ad-hoc solution is going to work.

> And then require case expressions to be either literals or constants.  The
> constants need not be computable at compile time, just runtime.  If a
> constant is defined using a foldable expression (e.g. FOO = 27 + 43), then
> the compiler can always optimize it down to a code level
> constant.  Otherwise, it can just put constants into cells that the
> functions use as part of their closure.  (For that matter, the switch
> statement jump tables, if any, can be put in a cell too.)
>
> >I don't like "first use" because it seems to invite tricks.
>
> Okay, then I think we need a way to declare a global as being constant.  It
> seems like all the big problems with switch/case basically amount to us
> trying to wiggle around the need to explicitly declare constants.

And I don't believe declaring constants is going to work; not without
a much bigger change to the language and the way we think about it.
This is because "const-ness" isn't a property that you can encode as a
new type of object. It is a compile-time property of *names*. The only
similar thing in Python is globals. But global declarations are
intentionally a bit clunky because we believe overuse of the feature
would be a mistake. But we wouldn't want to discourage constant
declaration if we had them, so having to repeat the 'constant' keyword
in every module that uses a particular constant would be a painful
wart.

Is your objection purely based on the problems of getting switch to
behave the same way inside and outside a function? I'd rather forbid
or cripple switch outside functions than either add constant
declarations or switch to first-use semantics.

-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to