Is there any way to to automatically insert spaces between function definitions. E.g. my initial sources are:
void
func1()
{
// func1 body.
}
void
func2()
{
// func2 body.
}
I would like it to be reformatted to:
void
func1()
{
// func1 body.
}
void
func2()
{
// func2 body.
}
And if there are more line breaks, fixed number of them should be kept.
As far as I can tell, there's currently no way to force clang-format to insert blank lines between consecutive functions where there currently are none. IMHO this is a huge missing feature.
Your best bet is to set 'MaxEmptyLinesToKeep: 2' inside .clang-format file to let clang-format keep 2 lines intact.
As mentioned in this answer with clang-format 14, you can use the following in your config file:
SeparateDefinitionBlocks: Always
The other possible values are Leave, to leave the spacing of definition blocks as-is, or Never, to remove empty lines between definition blocks.
Related
Interested why does set method defined on Cell, on the last line explicitly drops old value.
Shouldn't it be implicitly dropped (memory freed) anyways when the function returns?
use std::mem;
use std::cell::UnsafeCell;
pub struct Cell<T> {
value: UnsafeCell<T>
}
impl<T> Cell<T> {
pub fn set(&self, val: T) {
let old = self.replace(val);
drop(old); // Is this needed?
} // old would drop here anyways?
pub fn replace(&self, val: T) -> T {
mem::replace(unsafe { &mut *self.value.get() }, val)
}
}
So why not have set do this only:
pub fn set(&self, val: T) {
self.replace(val);
}
or std::ptr::read does something I don't understand.
It is not needed, but calling drop explicitly can help make code easier to read in some cases. If we only wrote it as a call to replace, it would look like a wrapper function for replace and a reader might lose the context that it does an additional action on top of calling the replace method (dropping the previous value). At the end of the day though it is somewhat subjective on which version to use and it makes no functional difference.
That being said, the real reason is that it did not always drop the previous value when set. Cell<T> previously implemented set to overwrite the existing value via unsafe pointer operations. It was later modified in rust-lang/rust#39264: Extend Cell to non-Copy types so that the previous value would always be dropped. The writer (wesleywiser) likely wanted to more explicitly show that the previous value was being dropped when a new value is written to the cell so the pull request would be easier to review.
Personally, I think this is a good usage of drop since it helps to convey what we intend to do with the result of the replace method.
I'd like to turn this:
void check() {
if(condition){
doSomething();
}
}
Into this:
void check() => condition? doSomething();
Is there a canonical way of doing this?
[Edit]
I'm aware of the ternary approach: condition? doStuff() : null but this generates compiler warnings.
[Edit2]
The ternary with : null does not generate a warning when used in a single-line expression. So technically this works. I was hoping for something more readable, the dangling : null indicates some weird intent here which might not be a bit confusing to a future reader.
You could use conditional operators.
General example:
result = testCondition ? trueValue : falseValue
Specific example:
void check() => condition ? doSomething() : null;
Or you could just put the whole if statement on a single line
=> () {if(condition) doSomething();};
You are fighting with the Dart formatter here.
You can put anything on a single line, Dart doesn't treat newlines and spaces differently.
So:
void check() { if (condition) doSomething(); }
is valid Dart. The Dart formatter, on the other hand, will insist that it becomes:
void check() {
if (condition) doSomething();
}
because that's the canonical way to format a function with a block body.
Don't fight the formatter. It's not worth it.
You can do awkward rewrites like the mentioned:
void check() => condition ? doSomething() : null;
(at least if doSomething() really is an expression).
That will not make your code more readable.
There is no way to put a statement in a function body on the same line as the function parameters, the formatting rules will just not allow it. The formatter insists that statements start on their own line (with the one exception being a single statement else branch of an if statement, if it fits on one line).
A statement block, including a function body, always has a line break after the leading {.
There is one other thing you can do, even if it's as ugly as the conditional expression:
void check() => condition && doSomething() == null;
(If you can make doSomething() return a boolean, you can drop the == null, then it becomes more readable, but still not as readable as a proper if statement).
Look at the code:
let add_one = |&: x| { 1 + x };
I know x is the closure argument, but what is the meaning of &: in the closure?
This is an underdocumented (and obsolete, see comment) section of Rust right now. The best reference I know of is the blog post Purging proc:
Because the current inference scheme is limited, you will sometimes need to specify which of the three fn traits you want explicitly. (Some people also just prefer to do that.) The current syntax is to use a leading &:, &mut:, or :, kind of like an “anonymous parameter”:
// Explicitly create a `Fn` closure.
foo(|&:| { ... })
// Explicitly create a `FnMut` closure.
foo(|&mut:| { ... })
// Explicitly create a `FnOnce` closure.
foo(|:| { ... }) // (ERROR)
Caveat: It is still possible we’ll change the &:/&mut:/: syntax before 1.0; if we can improve inference enough, we might even get rid of it altogether.
And it looks like it was removed in #21843! Thanks for pointing that out, #swizard!
In java method we can return from the middle skipping the rest of the method code being executed. e.g.
public String doSomething(){
step 1
step 2
if(some condition){
return "Exited from the middle";
}
step 4
return "Whole code is executed"
}
Is there a way to do such things in a drools rule?
It's quite simple:
return;
Since there's no place of invocation for a single rule you can control, or write code doing that, a return with an expression is not vailable. You can collect values you'd like to return in a global variable, List<String> or, perhaps, Map<String,List<String>> with rule names acting as keys.
Clarification
A rule's right hand side results in a static method with void as result type. A return statement just acts naturally.
I can't understand how to define default values for functions in my library. Default values tend to be ignored and I get "wrong parameters count" error message.
Here is my example. I created simple test library experts\libraries\test.mq4:
void test(int i = 0) // Note the default value for "i"
{
}
Then I created .mqh file as experts\include\test.mqh:
#import "test.ex4"
void test(int i = 0); // Note the default value for "i"
#import
Now I create simple expert "experts\simpletest.mq4":
#include <test.mqh>
int start()
{
// Should be able to call test() function without providing any arguments,
// because it has default value.
// If I change this line to test(0), everything compiles correctly
test(); // Causes "wrong parameters count" compilation error
return(0);
}
And I get the following error for test() function call:
')' - wrong parameters count
If I change this function call to test(0), everything compiles, but I should be able to call test() function without providing any parameters, because I have default value for first parameter in .mqh file, like this: void test(int i = 0);
Why it doesn't use the default value?
I search google for any clue, but can't find any references about this problem. Anybody knows?
This is not possible as stated in the MQL Documentation:
MQL4-library functions imported within other modules cannot have parameters initialized by default values.