2 ms·
In at least Java, and probably more languages, you can also use braces to limit variable scope. E.g.: List<String> lines; { int whatever; lines = n
by sbov 9y ago
In at least Java, and probably more languages, you can also use braces to limit variable scope. E.g.:
List<String> lines;
{
int whatever;
lines = new ArrayList<>();
// modify lines
}
List<Object> processed_data;
{
int whatever;
processed_data = new ArrayList<>();
// modify processed_data, using lines
}
- maxxxxx 9y agoI do this a lot in unit tests where a pattern is repeated.
- fishywang 9y agoIn Go something similar to this is actually more common because of `defer`, e.g.: if err := func() error { f, err := os.Open(filename) if err != nil { return err } defer f.Close() // work with f return nil }(); err != nil { return err }