Coder Social home page Coder Social logo

Top-level syntax about patma HOT 6 OPEN

gvanrossum avatar gvanrossum commented on May 23, 2024
Top-level syntax

from patma.

Comments (6)

Tobias-Kohn avatar Tobias-Kohn commented on May 23, 2024 1

Agreed. The PEP already demonstrates quite extensively that we considered a range of variants. I just wanted to make sure we have thought of this variant in case, as you say, someone brings it up later on during the discussion.

from patma.

brandtbucher avatar brandtbucher commented on May 23, 2024

A slight variant that saves a line and a level of indentation.

match <expression> case <pattern> [if <expression>]: <block>
case <pattern> [if <expression>]: <block>
# repeated cases

+0, personally. It's not ugly to me, but I'm sure others have opinions about the first pattern not lining up.

from patma.

gvanrossum avatar gvanrossum commented on May 23, 2024

Can't say I like the first pattern hiding behind the match clause.

Some other variants that have been proposed:

  • Don't indent the cases. The downside is that it would probably trip up most auto-indent implementations in IDEs:
match <expression>:
case <pattern> [if <expression>]: <block>
...
  • Swap match and case keywords (really! e.g. Coconut):
case <expression>:
    match <pattern> [if <expression>]: <block>
    ...
  • No case keyword (could be combined with other variants too, e.g. case or no-indent):
match <expression>:
    <pattern> [if <expression>]: <block>
    ...
  • Other keywords, e.g.
from <expression>:
    in <pattern> [if <expression>]: <block>

See #3 about whether to have guards (I think we should).

from patma.

gvanrossum avatar gvanrossum commented on May 23, 2024

See also #27 -- the only seriously remaining open issue ('as' vs. 'case').

from patma.

Tobias-Kohn avatar Tobias-Kohn commented on May 23, 2024

Just to be complete here, there is one more variant we haven't considered so far:

match:
    <expression>
case <pattern>:
    <block>
case <pattern>:
    <block>

This would be very similar to how try/except/finally currently works (syntactically).

At first glance, this looks like ticking all the boxes. It saves that level of indentation, and it even has a similar structure to something that already exists in Python. However, there is one isse with this which I find quite significant: the <expression> above would look like a statement. Or, put differently: this would introduce a first case of where (parts of) Python would be treated as an expression-oriented language, rather than a statement-oriented one. In my view, this rather disqualifies this proposal and feels much more "unpythonic" than what we are doing now.

from patma.

gvanrossum avatar gvanrossum commented on May 23, 2024

@Tobias-Kohn I agree with your argument against that variant. I believe I've seen it proposed before (maybe in your 2018 summary or blog post?). I don't think it's worth mentioning in the PEP unless someone on python-dev brings it up.

from patma.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.