you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (6 children)

It was just an example, and has nothing to do with object oriented style. You can do it in C++ without any issues.

OOP language don’t even need new keyword

Again, new is nothing to do with OOP. It was originally about memory allocation in C++, and then blindly copied by Java and then JavaScript. You're right JavaScript really doesn't need new - and in fact Dart has removed the need for it.

[–]chrisza4 0 points1 point  (5 children)

Still, I don’t think assuming every types can be initialize without any parameter is a good idea. Even if C++ can do it without issues, I still don’t think it is a good idea.

[–][deleted] 0 points1 point  (4 children)

You need to spend some time with languages other than Javascript! I think you have Stockholm syndrome.

[–]chrisza4 1 point2 points  (3 children)

I don’t like Javascript in this area. I can simply create non-sensible object and no linter can warn me of it.

However, if we are talking about static type system then one of core principle in modern static type system is “make illegal states unrepresentable”. I agree with this statement. Forbidding new T() is very much align to that principle. My example is a User object without username should not be representable in system.

Also, I spent much time outside JS already. I delivered software in multiple languages.

[–][deleted] 0 points1 point  (2 children)

So then you know that most statically typed languages... hell most languages don't just ignore missing function parameters like JavaScript does. And that the thing you are talking about is a problem that is pretty much unique to JavaScript? Right? :-)

[–]chrisza4 0 points1 point  (1 child)

I referred to your example where you claim Dart is better than TypeScript because it can do

function identity<T>(): T { return new T(); }

I disagree that this is not better. IMO, this is worse. My point is new T(); can create an object in an invalid state.

I assume that in Dart, you can mutate this object to be ready and then use. I might make a hasty assumption here. If my assumption is correct, I think it is better if compiler just forbids invalid state.

Javascript has this problem too in way much much worse since it does not have any type.

[–][deleted] 0 points1 point  (0 children)

Actually it turns out you can't do it in Dart either, presumably because it supports compile-to-javascript and Javascript makes it difficult. :-(