3 ms·
I just want to focus on writing functions. Here's a trivial example. I would like: // math/adder.cs public static add(int x, int y) : int { return x +
by jeswin 1y ago
I just want to focus on writing functions. Here's a trivial example.
I would like:
// math/adder.cs
public static add(int x, int y) : int {
return x + y;
}
Instead of:
// math/adder.cs
namespace math {
class adder {
public static add(int x, int y) : int {
return x + y;
}
}
}
Both are callable as:
math.adder.add(10, 20);
- politician 1y agoConsider: // math/adder.cs package math; …code…
- metaltyphoon 1y agoI agree with the first example. I wish C# could do “free” floating functions which just lives in the namespace itself. Your second example could do with the extra namespace indentation by doing namespace math;
- tester756 1y agoBut why? Having to use class/struct instead of free floating funcs tries to force some hierarchy, architecture, etc Which eventually makes the code better *globally*
- jeswin 1y agoI was arguing that the hierarchy is already in your path. The explicit namespace (and even the class name) is redundant if what you want are mostly functions. Currently, you're forced to define the hierarchy in the directory structure, and then again with namespaces. There should be a way to opt out of this.
- tester756 1y agoOk, I see As of now you can opt out of physical hierarchy (file patches) I think logical hierarchy is better because you avoid stupid things like moving file to other folder causing compilation errors. I've witnessed too much of such issues in C++/cmake world to want it.
- zvrba 1y agoC# allows file-level namespaces so you can write namespace Math; static class Adder { public static int Add(...) { ... } } (one nesting level less). Next, elsewhere you can write namespace Whatever; using static Math.Adder; class CC { void M() { var z = Add(10, 20); // no NS qualification needed due to using static above } } Java enforces directory structure to reflect package names, and this feature is not universally popular.