Hacker News new | ask | show | jobs
by lmm 700 days ago
There are plenty of issues. You simply can't write a method that e.g. accepts a read-only collection but does not accept a mutable collection. You can't use a "marker" interface with no methods/members to indicate intent. I mean sure you can write programs in it and they'll work most of the time, but if that's what you want you might as well use an untyped language.
1 comments

Can you do that in C# now? Not with IReadOnlyCollection and the mutable collections implement IReadOnlyCollection.
You can accept e.g. IImmutableList. (Looking it up it seems like there isn't an IImmutableCollection, but you can see how the language makes it possible to implement one).
You flipped from looking for mutable to immutable and somewhat lost me.

How about we talk about the examples in the article. Stream and StreamReader? How should that be handled by making interfaces? You can extend those types but you can't apply new interfaces to the existing types.

> You flipped from looking for mutable to immutable and somewhat lost me.

If you want to write a function that takes immutable collections and does not accept mutable ones, that's generally impossible to do in a language with only structural typing. In a language like C# with nominal typing, you can have that function accept an interface that only immutable collection types implement, such as IImmutableList.

> How about we talk about the examples in the article. Stream and StreamReader?

I have no idea what they do or what they're meant for. It's certainly possible to define bad interfaces, I don't think anyone's denying that.

> You can extend those types but you can't apply new interfaces to the existing types.

Sure, that's a limitation and there are various ways to navigate that tradeoff (e.g. adapters, or a Haskell/Rust-style trait system where you can apply interfaces to existing types but you have to do so explicitly). My point is that structural typing is not a flawless approach that solves all your problems.

One shouldn't be mixing Stream and StreamReader. StreamReader is a subclass of TextReader that is used for reading strings. Stream works on bytes only.

Actually I think the point is kind of moot since the article claims these have identical Read() methods but that is not the case. One returns bytes and the other returns chars and so they have different signatures.