Comments (2)
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.
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)
- [BUG] Variables in "block scope parameter" declarations should not default to `in` (const) HOT 5
- Move from last-use causes problems with RAII types, and types with member functions with side effects HOT 14
- [BUG] An `in` parameter in a statement scope does not work with CTAD
- [SUGGESTION] Ability to disable UFCS both via cppfront and for regions in source code HOT 14
- [BUG] Cppfront ICE when parsing invalid syntax in an unnamed function expression
- [BUG] Cannot chain a member function call after constructing an object HOT 1
- [BUG] read from inactive union member in cpp2::out.construct() HOT 3
- [BUG] Cannot deduce a braced-init-list (perhaps "parens-init-list" in Cpp2-speak) HOT 19
- Help requested: Identifying higher-priority open issues/PRs HOT 12
- [SUGGESTION] Support "dot index" access for tuples as alternative for `std::get` HOT 5
- [BUG] Variable declarations can refer to themselves HOT 8
- [SUGGESTION] Only apply move from last-use behaviour to `@basic_value` and well-known STL types (or appropriate types detected at compile-time) HOT 9
- [SUGGESTION] Make = in declaration optional when the RHS is a block
- [BUG] Apple Clang 13 doesn't support `std::copy_constructible` concept used in `cpp2util.h` HOT 2
- [BUG] Cannot initialize `std::map` with initializer list
- [BUG] Explicit discard requirement may be broken? HOT 3
- Citing cpp2 / cppfront HOT 5
- [BUG] *= operator ambiguity with postfix * HOT 3
- [SUGGESTION] Quite mode to run silently if compilation succeeds HOT 1
- [BUG] Initialization vs assignment in a loop HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cppfront.