Comments (6)
It works differently in C3, but covers the same thing.
- "Const" is only used on global declarations, and does not have a memory location. It's similar to "#define"
- All type qualifiers from C, including
const
is gone. - Declare a guarantee to have a parameter unchanged by using
@ensure const(foo)
in the docs. - If a function annotates a parameter as
@ensure const
but it's passed to a function that doesn't ensure const, then a warning/error is issued. This error may be suppressed as there are occasions where the annotation might be missing but the caller is sure that it is unchanged. In this case, altering the variable might lead to the variable having stale data when later used.
from c3c.
Reply to #15 (comment):
The major difference that I see is that:
-
@ensure const(foo)
is part of function definition, written in that single place. It is more/less C'sconst
alternative syntax. -
OTOH, the "
!argument
" could be used at multiple places, whenever the caller wanted to be sure of immutability. Function definition itself doesn't claim anything about its constness.Documentation generator/IDE could still discover and document the very strong property "function doesn't modify this parameter under any circumstances". This property is what C's
const
could have been, if only there were no type casts andconst
was transitive.
I argue, that the method (1) brings only little benefit, but marking code at the caller place - method (2) - gives the code reader more valuable hints.
from c3c.
I agree that marking at the call site is a more powerful thing, but in the end the code can't feel too busy either. Sigils in C3 are used to indicate variable types and in an integral part of the name. And obviously using "!" is out of the question since that brings the code far away from C. This needs to feel like an evolution of C, not a revolution syntactically.
Let's say we'd consider using const(var)
as call place annotation, that is:
int x = 0;
foo(const(&x));
There are a few things to consider:
- Verbosity
- There are actually 3 different things that can happen to x: "in", "out", "inout". "in": argument will only be read, "out": argument will be written to, but never read, "inout" argument will be both read from an written to. Const/not const actually only covers 2 cases. Consequently if one would explore this it is still insufficient.
- I deliberately want to avoid infecting everything with const annotations, and that's what happens in C, sort of a virus.
- Consider the situation where you use a method function.
bar.doSomething();
here bar may or may not be modified. Adding some annotation here to indicate things is getting painful.
BTW @ensure const(foo)
is transitive unlike C's const.
from c3c.
Reply to #15 (comment):
int x = 0;
foo(const(&x));
If most of these checks are concentrated in tests (which is what I suspect), then this would be good enough. Possibly even better than "!
", as it directs attention to what the test is supposed to be about.
With this annotation, do you rely on what the functions claims (@ensure const(foo)
) or do you do verify (or plan to verify) it with your own memory check? Because if I annotate certain piece of code, I want it to be true right here, not necessarily over there or even everywhere.
The above example gave me one idea: this kind of annotations (which I pressume would use runtime checking) could be restricted to the tests, to top level function calls in tests.
This would have following advantages:
- data used in tests are under much stricter control. It cannot happen, that the check would suddenly need to compare 1 GB again another 1 GB. If it happens, then you wrote a bad test.
- these checks would be restricted to test mode(s), when one doesn't care that much about performance. Normal debug mode w/o tests won't be in risk of uncontrolled slowdown.
"in", "out", "inout" things:
If function can have multiple return values, the "out" could be eliminated for good.
bar.doSomething();
would be annotated the same way:
!bar.doSomething();
or
const(bar.doSomething());
Once developer internalizes that !
/ const
means immutable, it feels natural.
from c3c.
I find it a bit disjointed to have all these discussions here at github issues. This is much more natural to discuss on the Discord, so could I perhaps ask if we could move the discussion there instead?
from c3c.
Please file each as an issue on c3docs
from c3c.
Related Issues (20)
- Incorrect 'const' name error line HOT 4
- Error when accessing single char of a String HOT 14
- Compiled standard library HOT 1
- There is no definition of "CT_CASTABLE" ? HOT 4
- Compiling in M2 throws lots of warnings HOT 24
- C's va_list in C3 HOT 3
- for..else & while..else HOT 7
- Allow trailing comma HOT 1
- When defining a "get" overload, it doesn't work to have the index < isz HOT 1
- Revisit naming for memory new, etc HOT 1
- Docker compilation fails due to lack of release file (Fixable)
- Proposal: Translating Python Tester Script to C for Enhanced Stack Trace Capturing in Meson Build HOT 1
- Extern const requires initializer HOT 1
- associative array (hashmap or RB tree) and strings HOT 4
- Fix line numbers on sigsegv
- Embed a file into the target HOT 7
- Standard library should implicitly use "weak odr"
- grammar.y issues HOT 6
- Compile time bitwise operators for bitstruct HOT 2
- Output directory option in project.json is not working properly 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 c3c.