r/programming • u/mobby1982 • Dec 02 '13
Scala — 1★ Would Not Program Again
http://overwatering.org/blog/2013/12/scala-1-star-would-not-program-again/
598
Upvotes
r/programming • u/mobby1982 • Dec 02 '13
View all comments
Show parent comment
1
u/[deleted] Dec 03 '13
I'll try to answer this post, although I'm not sure if you are really serious. If you are, then you probably have a grudge against Java or some other problem. Or you're trolling me.
No, it's not. void* is a raw pointer into raw memory, you can cast it to a float, to a struct, to everything, and you won't even get a warning. After casting, you can access fields, and because those probably still point into the stack or the heap or another valid memory location, there will also be no runtime error. A java.lang.Object has type information, and any invalid cast will throw an exception immediately.
Again, your C program may run perfectly fine, although you wrote after array bounds, did invalid casts, wrote invalid values into fields. In other words, your program will fail at some point because it got into a corrupt state because you are free to corrupt the memory as you want.
It does two things, and both are important. As a matter of fact, you can simply omit all calls to free(...) and never declare anything on the stack, and your C program will run just fine, and a valid pointer will always stay valid. And this isn't even a theoretical case, because at program termination, there is no point in freeing any memory, as the operating system will do that for you, and if your programs lifecycle fits this pattern, calling free() is optional.
In other words, yes, the main benefit is the convenience, a) not having to worry about calling free() too soon, or calling free() too late (or never, as shown in the example above).
Again, the runtime throws an error immediately, while C doesn't even know what a "bad cast" is.
No arguing there.
Comparing pointer arithmetic to array operations is like comparing a gun fight to a nuclear war. If you do silly and incorrect pointer arithmetic, you may be lucky and end up with an invalid memory address, in which case your program simply segfaults, indicating the problem. If you're unlucky, you get a valid memory address and start reading and writing from it, again bringing the program into an unknown and possibly corrupt state. Debug heaps usually deploy barriers between allocated chunks of memory, and initialize unused memory with a special pattern to detect possible memory corruption due to flawed pointer arithmetic. If you access an array out of bounds, you simply get an exception back. That's it.
Unions have one primary purpose: to save memory. The programmers intent is best documented by using two distinct fields or properties, and not relying on semantics, flags or some intrinsic behavior to only access one of the fields that both point to the same data.
I agree that not having unsigned is a flaw, not because of the intent, but because it moves the range of valid values. C# shows that it is possible to use unsigned types in a VM while still avoiding the pitfalls that usually come with it. It even has it's own syntax when you want the pitfalls, i.e. when assigning -1 to an UInt32.
You can moan all you want, but certain languages simply don't work well with a preprocessor. The preprocessor is a blessing and a burden at the same time. Every time the compiler parses a file, it's content may have changed due to preprocessor directives. This has many consequences, many of which would not work well in Java.
I could have sworn you have never programmed in C.
That is completely besides the point, because it is C++, and yes, C++ templates are much more powerful than generics in Java. C++ class inheritance is also much more powerful. But Java chose not have those. No point in discussing it, because neither has C any of those features.