5 ms·
[edited because apparently I can't read; kenjackson did mention interfaces] I think it's still a bad idea - as you note, isa is available without necessarily h
by neilk 13y ago
[edited because apparently I can't read; kenjackson did mention interfaces]
I think it's still a bad idea - as you note, isa is available without necessarily having inheritance; we know inheritance is dangerous, so why include that?
In languages like C++, C# and Java, it has long been expert advice to avoid inheritance where possible, and use interfaces. But the language and language culture is leading everyone astray. Defining interfaces is usually harder or stranger-looking, and is taught as an adjunct to inheritance.
Other languages get this more right. Haskell has what it calls typeclasses, but they are more similar to interfaces in the aforementioned languages.
We seem to be making exactly the same mistake in the ECMAScript standard, except it's even worse; you can't define an interface even if you wanted to.
- jkrems 13y agoWhat would "defining an interface" even mean in a non-type checked language? I'd say: ECMAScript already has better interface support than Java has because it supports implicit interfaces. In Java two classes can only be interface-compatible if they have a shared dependency/are at least indirectly coupled. In a language with duck typing and without static typing interfaces are present implicitly.
- neilk 13y agoYou're correct that adding interfaces to JavaScript would also require adding optional type declarations. But there are lots of languages based on JavaScript, or which compile to JavaScript, which offer optional type declarations. It seems to me they could have gone down this path. I don't agree that it's better to have an "interface" which exists purely in the head of every programmer working on the code. Languages can be doing more for us, and it doesn't have to be the kind of verbosity that Java offers.
- jkrems 13y agoMaybe that was a misunderstanding: I didn't mean to say that interfaces should exist purely in the head of the programmer. I did say that implicit interfaces/protocols (Go, Objective-C) are better than interfaces that need shared dependencies (Java/pure virtuals in C++). If I had to order it I'd say: duck typing better than explicit interfaces; implicit interfaces that are backed by static type checks "better than" duck typing. The "better than" in quotes for the latter because it really is more about how much static analysis the language supports and it's more a matter of opinion. But I'm relatively convinced that interfaces/protocols that require coupling of implementations are clearly inferior to the ones that are implicit.
- chc 13y agoProbably a lot like Clojure's protocols, which are precisely that. Effectively, functions declared in a protocol are just lookup tables that resolve to a particular implementation based on an object's type at runtime. You can query a value at runtime for whether it implements a protocol, too.
- straws 13y agoA statically typed language enforces an interface, but an interface is just a concept. All it does is ask: does this object respond to the following methods? You and your team can ask that question, or the type checker can.
- ryanatkn 13y ago> Haskell has what it calls types I think you mean typeclasses? One of the things that always bothered me about interfaces was the inability to define a default implementation, especially when developing UIs. A proper mixin system (like what's used by React's components) has solved this particular problem for me.
- neilk 13y ago> typeclasses? Fixed. Thanks. I agree with you about Haskell. I almost went into a discussion of how Rust does this. They don't have subtypes, but they do have code reuse if you explicitly declare it. Caution, I'm just an admirer of Rust, not a user. So maybe it sucks too, but this seems closer to how things ought to be. http://static.rust-lang.org/doc/master/tutorial.html#default-method-implementations-in-trait-definitions http://static.rust-lang.org/doc/master/tutorial.html#default...
- tel 13y agoHaskell does allow you to define default implementations, though. In at least two different ways.
- cpeterso 13y agobtw, Rust will likely add virtual functions with inherited implementations because it makes some Servo code easier to write and more efficient: https://mail.mozilla.org/pipermail/rust-dev/2014-March/008952.html https://mail.mozilla.org/pipermail/rust-dev/2014-March/00895...
- kenjackson 13y agoHow am I conflating them when I specifically say you can get isa without inheritance?
- neilk 13y agoOkay I reread this and I don't know how I missed the second section. I assume you didn't add that later. Sorry I am jet-lagged. Revising comment.
- kenjackson 13y agoIt was in the original. No prob. I misread all the time too, without jet lag as an excuse even. 😊