Go and Rust – objects without class
1–10 of 60 posts
Re: Go and Rust – objects without class
#2Re: Go and Rust – objects without class
#3This is a good article, although I'm not sure its appropriate to post a subscriber link.
Re: Go and Rust – objects without class
#4This is a good article, although I'm not sure its appropriate to post a subscriber link.
Re: Go and Rust – objects without class
#5If you think of a type as a set of possible values, then a subclass is actually a supertype—a superset of the possible values of its parent class—and likewise a superclass is a subtype. The “is-a” relationship is saying “for all instances I of X, I is an instance of Y” which is essentially the definition of superset. That is what the Liskov Substitution Principle is about.
In C++ and other languages with support for object-oriented programming, a subclass is not necessarily a supertype: virtual functions can throw “I don’t make sense in this derived class”. And a supertype is not necessarily a subclass: templates offer the benefit of the doubt when it comes to static interfaces.
This mismatch leads, in my experience, to interfaces that are ill-specified at best and buggy at worst—so it’s nice to see those issues addressed.
Re: Go and Rust – objects without class
#6The value of Go and Rust, I think, is that they are object-oriented languages that try to avoid conflating classes with types. If you think of a type as a set of possible values, then a subclass is actually a supertype—a superset of the possible values of its parent class—and likewise a superclass is a subtype. The “is-a” relationship is saying “for all instances I of X, I is an instance of Y” which is essentially th…
This is one reason why so many have problems with prototype inheritance in JavaScript, which is also not the only language having it.
I find positive that so many OO models exist to choose from.
Re: Go and Rust – objects without class
#7The value of Go and Rust, I think, is that they are object-oriented languages that try to avoid conflating classes with types. If you think of a type as a set of possible values, then a subclass is actually a supertype—a superset of the possible values of its parent class—and likewise a superclass is a subtype. The “is-a” relationship is saying “for all instances I of X, I is an instance of Y” which is essentially th…
For example, given the article's GO code:
type file interface {
Read(b Buffer) bool
Write(b Buffer) bool
Close()
}
my first thought was: wait, not all files are writable, that interface is ill specified.I do think subtyping and subclassing should be different things, but it's not a panacea against bad specifications.
Re: Go and Rust – objects without class
#8The value of Go and Rust, I think, is that they are object-oriented languages that try to avoid conflating classes with types. If you think of a type as a set of possible values, then a subclass is actually a supertype—a superset of the possible values of its parent class—and likewise a superclass is a subtype. The “is-a” relationship is saying “for all instances I of X, I is an instance of Y” which is essentially th…
I am not sure the issue of “I don’t make sense in this derived class” is solved by decoupling types and inheritance. For example, given the article's GO code: type file interface { Read(b Buffer) bool Write(b Buffer) bool Close() } my first thought was: wait, not all files are writable, that interface is ill specified. I do think subtyping and subclassing should be different things, but it's not a panacea against bad…
The Go standard io library has a Reader interface:
type Reader interface {
Read(p []byte) (n int, err error)
}
a Writer interface: type Writer interface {
Write(p []byte) (n int, err error)
}
and a ReadWriter interface which uses type embedding to combine both: type ReadWriter interface {
Reader
Writer
}
Idiomatic Go will break interfaces up into pretty discrete bits of functionality and use type embedding to build up more complex interfaces. The article does show this a little bit with its seekable interface but fails in some other areas and I kind of wish it just used the real io package interfaces or else examples of something not covered better by the actual Go standard library.Re: Go and Rust – objects without class
#9The value of Go and Rust, I think, is that they are object-oriented languages that try to avoid conflating classes with types. If you think of a type as a set of possible values, then a subclass is actually a supertype—a superset of the possible values of its parent class—and likewise a superclass is a subtype. The “is-a” relationship is saying “for all instances I of X, I is an instance of Y” which is essentially th…
You can't do that! You fall foul of Russell's paradox - sets as types simply don't work!
Re: Go and Rust – objects without class
#10The value of Go and Rust, I think, is that they are object-oriented languages that try to avoid conflating classes with types. If you think of a type as a set of possible values, then a subclass is actually a supertype—a superset of the possible values of its parent class—and likewise a superclass is a subtype. The “is-a” relationship is saying “for all instances I of X, I is an instance of Y” which is essentially th…
I am not sure the issue of “I don’t make sense in this derived class” is solved by decoupling types and inheritance. For example, given the article's GO code: type file interface { Read(b Buffer) bool Write(b Buffer) bool Close() } my first thought was: wait, not all files are writable, that interface is ill specified. I do think subtyping and subclassing should be different things, but it's not a panacea against bad…