4 ms·
Undocumented downside: Java becomes a slightly harder language to learn, because now there is one more thing to explain to a beginner: "Where exactly should
by londons_explore 4y ago
Undocumented downside:
Java becomes a slightly harder language to learn, because now there is one more thing to explain to a beginner: "Where exactly should super() be put in the constructor? At the start, in the middle, or at the end? What are the benefits and downsides of each, and when does it matter? Is there a convention?"
- mcculley 4y agoAs someone who has been using Java since 1.0, I welcome these little improvements but agree that the language is now a mess for a beginner. All of the little rules and corners make sense to someone who learned them gradually. To a beginner, they must seem nonsensical and intimidating.
- londons_explore 4y agoThis is the evolution of nearly any language... It begins simple and easy, and over time more and more features and syntax are added, and it becomes very hard to learn from scratch, which in turn means beginners end up using other simpler languages. I think python is a classic example of this - python 2 was pretty simple. Python 3 just gets more and more complex.
- cratermoon 4y agoThe language formerly known as Perl 6 has entered the chat.
- amoe_ 4y agoYep. Pre-generics, Java was considered a good teaching language because of its simplicity and the ability to hold the entire language in your head. I think it's much more difficult to make that argument now. The same for Python. Moreover, while Python 3's unicode/byte string separation was a crucial step, IMV other features from Python 3 haven't made a big impact on how useful the language is.
- nayuki 4y agoPython 2 is NOT simple. Why does str + unicode return unicode (Python 3 clearly separates bytes and str)? Why is there int vs. long (Python 3 dropped long)? Why can an inner function not assign to a variable of an outer function (Python 3 allows nonlocal)? Why does print have special syntax (>>, trailing comma) and is not a function? Why range() vs. xrange() (Python 3 makes all of them lazy)? Why redundant operators like <> vs. != (Python 3 eliminated the former)? Why class Foo vs. class Foo(object) (Python 3 eliminated the former)? Why multiple syntaxes for catching an exception?
- deleted 4y ago[deleted]
- rcme 4y agoWhy does super need to go in a specific position? Isn’t “super calls the parent classes constructor” simpler than “ super calls the parent classes constructor and it must be the first statement in the child constructor.”
- revetkn 4y agoThat's how I view it as well, from an end user perspective this is removing an arbitrary rule that you're required to "just memorize" (or be surprised when your IDE complains).
- avereveard 4y agoit's simpler for the subclass. the parent class however now has to deal with an arbitrary set of operation that the sub class can perform between the initializer block and the constructor method, so it's contract is overall much harder to define, because you can no longer be sure of your internal state in the constructor.
- rcme 4y agoThis seems like a solvable problem with static analysis: don’t allow reads from instance variables / methods of the parent class until super is called.
- tsimionescu 4y agoThe problem begins when you want to do more complex things. For example, you can't call super() inside a try{} block as far as I understand, but that doesn't immediately follow from "super calls the parent class' constructor" - and it did follow from "either super() or this() must be the first statement in your constructor".
- grog454 4y agoWhat does "try {" compile to in machine instructions or any intermediate language? I always thought of it as an annotation on a scope but now I'm curious. Edit- from reading https://stackoverflow.com/questions/25007427/how-are-exceptions-caught-and-dealt-with-at-the-low-assembly-level https://stackoverflow.com/questions/25007427/how-are-excepti... it appears my suspicion was correct. try{ is effectively an annotation that has no impact on the sequence of instructions you are authoring. So it's surprising to me that it breaks the this/super first rule. OTOH it seems to me there should always be a rule to not allow execution paths that never call super nor this, and since try could break that one I'd always be leery of using it in constructors.
- sureglymop 4y agoArbitrarily placing those bits of code at the beginning of the constructor is also confusing for a beginner. I think the way Kotlin does it (special syntax for this) is less confusing.
- jeroenhd 4y agoThis is why I prefer the way C# did this: class Base { public int Count { get; set; } = 0; public Base(int counter) { Count = counter; } } class Sub : Base { public Sub(int number) : base(number) { System.Console.WriteLine($"This is the sub class constructor with {number} as an argument"); } public Sub() : this(123) { System.Console.WriteLine("This is the sub class constructor with no arguments"); } } Rather than being a magical function call at a magical position, the call to the super constructor is part of a special bit of syntax that explicitly tells you what kind of call is being done. You can't call any method at that point of the program, you have to call a constructor by its special reference. It also makes the reason why you can do `super(fn1(), fn2())` but not extract those two calls to variables; these calls are considered to be part of a different part of the execution flow than the rest of the constructor. A simple `base()` can be left out, of course; it only matters in cases where you need a specific super constructor.
- vbezhenar 4y agoI don't see how is that convenient? Java approach will be trivially extended with this JEP and this restriction will be lifted. C# approach probably will never be improved so it'll never be convenient to use.
- ziml77 4y agoIt's not convenient, but it's clear. I would love something akin to this JEP in C#, but I'd want it in a way that makes it very clear that you're executing code prior to the base class' fields being initialized. Something along this lines of this public Foo(int bar) { prector { var baz = bar * bar; base(baz); } ctor { // Foo's constructor code } }
- int_19h 4y agoSince this and base are both keywords, and since () is not overloadable in C#, it adopt Java-like syntax for such calls without breaking backwards compatibility if it ever wants to do the same thing Java did.
- vbezhenar 4y agosuper() was never forced to be placed at the start. Every expression that you call in super was called before it, obviously. So you always was writing code like MyCtor() { int arg1 = calcArg1(); int arg2 = calcArg2(); super(arg1, arg2); } And the fact that you wasn't able to write this code literally was confusing for every beginner. Right now the rule is simple as it should be: you can't read `this`, explicitly or implicitly, before calling `super()`. Now it actually makes sense. The only somewhat confusing thing about this JEP is that you can initialize fields before calling super. I don't see why this is needed and I'd treat it as code smell. I've read that example and I don't think that it's convincing. This code should be rewritten.
- pulse7 4y agoThis is just a refinement of the original specification...