[Python-Dev] PEP 3103: A Switch/Case Statement
Nick Coghlan
ncoghlan at gmail.com
Thu Jun 29 14:37:07 CEST 2006
Eric Sumner wrote:
>>> Forget subroutines for a moment - the main point of the thread was the
>>> idea that the dispatch table was built explicitly rather than
>>> automatically - that instead of arguing over first-use vs.
>>> function-definition, we let the user decide. I'm sure that my specific
>>> proposal isn't the only way that this could be done.
>> But anything that makes the build explicit is going to be so much more
>> ugly. And I still think you're trying to solve the wrong problem.
>
> Only if the programmer has to see it. The dispatch table need not
> include the behaviors of each of the cases; it only needs to define
> what the cases are. In most of the use cases I've seen, switch is
> used to define behavior for different values of an enumeration. The
> dispatch table for an enumeration can be built wherever the values for
> the enumeration are defined (such as in a module). Programmers don't
> need to bother with making a dispatch table unless they are defining
> enumeration values themselves.
You mean something like this?:
switch x in colours:
case RED:
# whatever
case GREEN:
# whatever
case BLUE:
# whatever
I think Guido's right. It doesn't solve the underlying problem because the
compiler still has to figure out how to build a dispatch table from the
possible values in colours to the actual bytecode offsets of the cases.
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
---------------------------------------------------------------
http://www.boredomandlaziness.org
More information about the Python-Dev
mailing list