3 ms·
I wasn't sure entirely what you were getting at with const variables in C that are initialized later – and this doesn't fit your bill, if you were referring to
by MaxGabriel 13y ago
I wasn't sure entirely what you were getting at with const variables in C that are initialized later – and this doesn't fit your bill, if you were referring to lazy evaluation – but you can do something like this (apparently works for Clang and GCC):
const int test = ({
int a = 1+1;
sqrt(a);
});
- zackmorris 13y agoOh wow I didn't know about that notation! For anyone stumbling onto this, I wanted to elaborate on what I was getting at with regard to functional programming in c. When I learned Scheme back in the 90s, they had us do a lot of expressions like this: (* 1 (+ 2 3)) => 5 But I always thought it was a kind of obfuscation. They didn't mention the "define" keyword or that we could break things up into multiple lines until later in the class than I would have liked. They wanted us to think in terms of transformations on the data instead of sequential operations on it. So we were writing these enormous functions enclosed in a single parentheses that were terribly hard to decipher. This can be written as: (define a (+ 2 3)) (* 1 a) => 5 So I always wanted something similar in c, being able to write code in a functional manner without having to have these unreadable one line functions. I just wanted to store the result of a computation in a temporary const variable to break things up a little. I also didn't want intermediate variables polluting my scope. For example: const int test; { const int a = 1+1; test = sqrt(a); } => "Read-only variable is not assignable" But your notation makes this possible (slightly embellished to show the power of it): const int someOtherResult; // 41 const int test = ({ const int a = 1+1; sqrt(a) + someOtherResult; }); => test = 42 Now we can break up our statements and as long as we use only const variables and don't use any global variables, we are programming in a functional manner, for the most part. Cool! Unit tests become trivial to write, static analysis finds most of our bugs, we don't have to use as many templates.. Then if we add lambdas/closures from C++11, we can pass around code as variables and we are getting most of the benefit of pure functional languages without being so restricted in syntax. The main drawback is that we lose lazy evaluation, but in the real world that hasn't hindered me except when I was dealing with databases. Supposedly java can retrieve database results lazily but I don't know quite how that works. Perhaps something like it could be adapted for c. P.S. does someone know the name for the "({" notation? I couldn't find it anywhere thanks!
- MaxGabriel 13y agoI don't know the name; I found it through issue 105 of iOS Dev Weekly. Here's the link to the article: http://cocoa-dom.tumblr.com/post/56517731293/new-thing-i-do-in-code?utm_source=iOS+Dev+Weekly&utm_campaign=iOS_Dev_Weekly_Issue_105&utm_medium=email http://cocoa-dom.tumblr.com/post/56517731293/new-thing-i-do-...