Coder Social home page Coder Social logo

Discussion: Defer Control Flow about cppfront HOT 2 CLOSED

hsutter avatar hsutter commented on June 14, 2024 3
Discussion: Defer Control Flow

from cppfront.

Comments (2)

lohika-denis-kotov avatar lohika-denis-kotov commented on June 14, 2024 2

@jdbener

The C stdio UFCS example got me thinking about a defer control flow construct which would run the statement or block after it when the current scope would be exited. This would allow the the C stdio example to be rewritten as:

main: () -> int = {
    s: std::string = "Fred";
    myfile := fopen("xyzzy", "w");
    defer myfile.fclose(); // Run at the end of the scope
    myfile.fprintf( "Hello %s with UFCS!", s.c_str() );
}

Which doesn't do much in this example, but in more complicated cases where error handling gets involved it could become useful. I know RAII wrappers are the "idiomatic" C++ solution to this problem, but there are times (especially while prototyping) where writing a wrapper is unnecessarily clunky. In the past I have used some macro and lambda wizardry along with some scope guard libraries to implement almost this exact functionality in Syntax1. Thus implementing backend support for this type of feature in the cpp2 supplemental library should be straightforward...

But with all of this factors considered, would this sort of feature even be worth including in Syntax2? Or is it just another unnecessary feature to steal from Go for the sake of stealing it?

C++ do not need the defer keyword, we have a RAII instead

from cppfront.

hsutter avatar hsutter commented on June 14, 2024

I agree, we have RAII.

We also have LFTS3 scope_exit and GSL's finally/final_action (the latter is partly in cpp2util.h already).

I just added a similar c_raii, we'll see if people find it useful.

from cppfront.

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.