Re:On the evolution of programming style: How far C++ has mo...
by sukuriant on Monday March 26, 2012 @07:14PM
(#3759094)
In a recent article on Hacker News regarding the evils of classes, one of the valid uses of classes were for makeshift namespaces. I would have to agree with this. The model of Java requires all functions to be inside classes. If you have a class full of statics, it functionally becomes a unique namespace. It's semantics, but at that point, they're interchangeable.
Re:On the evolution of programming style: How far C++ has mo...
by qu4z-2 on Monday March 26, 2012 @11:57PM
(#3759947)
Except that you can't import a class using "using" or "import", so you have to write Math.cos() everywhere.
EDIT: I see below that Java has added "import static", but at that point you're in the situation where you have two things that are almost the same but not quite, which is not a particularly clean design in my opinion.
Re:On the evolution of programming style: How far C++ has mo...
by SeanLuke on Monday March 26, 2012 @07:58PM
(#3759239)
You are conflating namespaces (a general and abstract concept) with Java packages. While it's true that static methods can have private static (essentially global) data, in in the Java standard libraries static methods largely exist as a mechanism to keep function names from polluting a common global namespace.
And Math is a particularly special case of this. Ponder this: have you ever had to say
import java.lang.Math
?
Re:On the evolution of programming style: How far C++ has mo...
by pauldino on Monday March 26, 2012 @06:40PM
(#3758994)
Thankfully Java 5 added 'import static' which lets you pretend classes are namespaces.
http://docs.oracle.com/javase/1.5.0/docs/guide/language/stat...
Re:On the evolution of programming style: How far C++ has mo...
by marshray on Monday March 26, 2012 @06:45PM
(#3759004)
Thanks for that info. Anyone know if C# has something similar?
From the linked docs: So when should you use static import? Very sparingly!
To me, it sort of confirms the point that these inelegant features are necessary because the language doesn't support fundamental primitives like plain functions and named constants without a superfluous object definition.
Re:On the evolution of programming style: How far C++ has mo...
by pauldino on Tuesday March 27, 2012 @12:44AM
(#3760054)
The CLI itself actually supports unadorned functions, but C# doesn't provide a way of writing them, or using directly them without resorting to reflection. I'm pretty sure C++/CLI lets you use and write standalone functions in managed code, and come to think of it I imagine F# would too. Also it's not an exact equivalent, but C# extension methods can often be used instead of static methods.
But in this case "Math" isn't even a namespace! It's a class of which cos() is a static member. http://docs.oracle.com/javase/6/docs/api/java/lang/Math.html...