extension overriding and selflkuper/talks/rust-objects/... · 2011-09-10 · some pieces of the...
TRANSCRIPT
![Page 1: extension overriding and selflkuper/talks/rust-objects/... · 2011-09-10 · Some pieces of the Rust object system: extension, overriding, and self Lindsey Kuper Indiana University](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed2ddcaa079355bb26da18a/html5/thumbnails/1.jpg)
Some pieces of the Rust object system:extension, overriding, and self
Lindsey KuperIndiana University Programming Languages Group
September 9, 20111
![Page 2: extension overriding and selflkuper/talks/rust-objects/... · 2011-09-10 · Some pieces of the Rust object system: extension, overriding, and self Lindsey Kuper Indiana University](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed2ddcaa079355bb26da18a/html5/thumbnails/2.jpg)
2
What’s Rust?
a systems languagepursuing the trifecta:safe, concurrent, fast
![Page 3: extension overriding and selflkuper/talks/rust-objects/... · 2011-09-10 · Some pieces of the Rust object system: extension, overriding, and self Lindsey Kuper Indiana University](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed2ddcaa079355bb26da18a/html5/thumbnails/3.jpg)
You’re working on the what system?!
3
![Page 4: extension overriding and selflkuper/talks/rust-objects/... · 2011-09-10 · Some pieces of the Rust object system: extension, overriding, and self Lindsey Kuper Indiana University](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed2ddcaa079355bb26da18a/html5/thumbnails/4.jpg)
You’re working on the what system?!
■ I didn’t arrive with the intention of working on the object system, but...
3
![Page 5: extension overriding and selflkuper/talks/rust-objects/... · 2011-09-10 · Some pieces of the Rust object system: extension, overriding, and self Lindsey Kuper Indiana University](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed2ddcaa079355bb26da18a/html5/thumbnails/5.jpg)
You’re working on the what system?!
■ I didn’t arrive with the intention of working on the object system, but...
■ I was intrigued by the idea of a classless object model and flexible prototype-based objects
3
![Page 6: extension overriding and selflkuper/talks/rust-objects/... · 2011-09-10 · Some pieces of the Rust object system: extension, overriding, and self Lindsey Kuper Indiana University](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed2ddcaa079355bb26da18a/html5/thumbnails/6.jpg)
You’re working on the what system?!
■ I didn’t arrive with the intention of working on the object system, but...
■ I was intrigued by the idea of a classless object model and flexible prototype-based objects■ and was told, “None of that’s implemented yet; go for it!”
3
![Page 7: extension overriding and selflkuper/talks/rust-objects/... · 2011-09-10 · Some pieces of the Rust object system: extension, overriding, and self Lindsey Kuper Indiana University](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed2ddcaa079355bb26da18a/html5/thumbnails/7.jpg)
You’re working on the what system?!
■ I didn’t arrive with the intention of working on the object system, but...
■ I was intrigued by the idea of a classless object model and flexible prototype-based objects■ and was told, “None of that’s implemented yet; go for it!”
■ When I started: no object extension, method overriding, or self-dispatch
3
![Page 8: extension overriding and selflkuper/talks/rust-objects/... · 2011-09-10 · Some pieces of the Rust object system: extension, overriding, and self Lindsey Kuper Indiana University](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed2ddcaa079355bb26da18a/html5/thumbnails/8.jpg)
You’re working on the what system?!
■ I didn’t arrive with the intention of working on the object system, but...
■ I was intrigued by the idea of a classless object model and flexible prototype-based objects■ and was told, “None of that’s implemented yet; go for it!”
■ When I started: no object extension, method overriding, or self-dispatch
■ During my internship, I implemented those things
3
![Page 9: extension overriding and selflkuper/talks/rust-objects/... · 2011-09-10 · Some pieces of the Rust object system: extension, overriding, and self Lindsey Kuper Indiana University](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed2ddcaa079355bb26da18a/html5/thumbnails/9.jpg)
You’re working on the what system?!
■ I didn’t arrive with the intention of working on the object system, but...
■ I was intrigued by the idea of a classless object model and flexible prototype-based objects■ and was told, “None of that’s implemented yet; go for it!”
■ When I started: no object extension, method overriding, or self-dispatch
■ During my internship, I implemented those things■ and learned that they interact with each other in
interesting ways
3
![Page 10: extension overriding and selflkuper/talks/rust-objects/... · 2011-09-10 · Some pieces of the Rust object system: extension, overriding, and self Lindsey Kuper Indiana University](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed2ddcaa079355bb26da18a/html5/thumbnails/10.jpg)
Self-dispatch
4
![Page 11: extension overriding and selflkuper/talks/rust-objects/... · 2011-09-10 · Some pieces of the Rust object system: extension, overriding, and self Lindsey Kuper Indiana University](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed2ddcaa079355bb26da18a/html5/thumbnails/11.jpg)
Self-dispatch
4
![Page 12: extension overriding and selflkuper/talks/rust-objects/... · 2011-09-10 · Some pieces of the Rust object system: extension, overriding, and self Lindsey Kuper Indiana University](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed2ddcaa079355bb26da18a/html5/thumbnails/12.jpg)
Self-dispatch + object extension
5
![Page 13: extension overriding and selflkuper/talks/rust-objects/... · 2011-09-10 · Some pieces of the Rust object system: extension, overriding, and self Lindsey Kuper Indiana University](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed2ddcaa079355bb26da18a/html5/thumbnails/13.jpg)
Self-dispatch + object extension
5
![Page 14: extension overriding and selflkuper/talks/rust-objects/... · 2011-09-10 · Some pieces of the Rust object system: extension, overriding, and self Lindsey Kuper Indiana University](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed2ddcaa079355bb26da18a/html5/thumbnails/14.jpg)
A brainteaser...
6
After my first implementation attempt,
this assertion failed.Why?
![Page 15: extension overriding and selflkuper/talks/rust-objects/... · 2011-09-10 · Some pieces of the Rust object system: extension, overriding, and self Lindsey Kuper Indiana University](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed2ddcaa079355bb26da18a/html5/thumbnails/15.jpg)
A hint...
7
![Page 16: extension overriding and selflkuper/talks/rust-objects/... · 2011-09-10 · Some pieces of the Rust object system: extension, overriding, and self Lindsey Kuper Indiana University](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed2ddcaa079355bb26da18a/html5/thumbnails/16.jpg)
A brainteaser...
8
After my first implementation attempt,
this assertion failed.Why?
![Page 17: extension overriding and selflkuper/talks/rust-objects/... · 2011-09-10 · Some pieces of the Rust object system: extension, overriding, and self Lindsey Kuper Indiana University](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed2ddcaa079355bb26da18a/html5/thumbnails/17.jpg)
A brainteaser...
8
After my first implementation attempt,
this assertion failed.Why?
longcat’s vtablelongcat’s vtablelongcat’s vtable
0 ack forward to shortcat.ack()1 lol ret “lol”
2 meow forward to shortcat.meow()3 nyan ret “nyan”
4 zzz forward to shortcat.zzz()
![Page 18: extension overriding and selflkuper/talks/rust-objects/... · 2011-09-10 · Some pieces of the Rust object system: extension, overriding, and self Lindsey Kuper Indiana University](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed2ddcaa079355bb26da18a/html5/thumbnails/18.jpg)
A brainteaser...
8
After my first implementation attempt,
this assertion failed.Why?
longcat’s vtablelongcat’s vtablelongcat’s vtable
0 ack forward to shortcat.ack()1 lol ret “lol”
2 meow forward to shortcat.meow()3 nyan ret “nyan”
4 zzz forward to shortcat.zzz()
shortcat’s vtableshortcat’s vtableshortcat’s vtable
0 ack ret “ack”
1 meow ret “meow”
2 zzz ret self.meow()
![Page 19: extension overriding and selflkuper/talks/rust-objects/... · 2011-09-10 · Some pieces of the Rust object system: extension, overriding, and self Lindsey Kuper Indiana University](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed2ddcaa079355bb26da18a/html5/thumbnails/19.jpg)
How to fix it
9
longcat’s vtablelongcat’s vtablelongcat’s vtable
0 ack forward to shortcat.ack()1 lol ret “lol”
2 meow forward to shortcat.meow()3 nyan ret “nyan”
4 zzz forward to shortcat.zzz()
shortcat’s vtableshortcat’s vtableshortcat’s vtable
0 ack ret “ack”
1 meow ret “meow”
2 zzz ret self.meow()
![Page 20: extension overriding and selflkuper/talks/rust-objects/... · 2011-09-10 · Some pieces of the Rust object system: extension, overriding, and self Lindsey Kuper Indiana University](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed2ddcaa079355bb26da18a/html5/thumbnails/20.jpg)
How to fix it
9
longcat’s vtablelongcat’s vtablelongcat’s vtable
0 ack forward to shortcat.ack()1 lol ret “lol”
2 meow forward to shortcat.meow()3 nyan ret “nyan”
4 zzz forward to shortcat.zzz()
shortcat’s vtableshortcat’s vtableshortcat’s vtable
0 ack ret “ack”
1 meow ret “meow”
2 zzz ret self.meow()
shortcat’s backwarding vtableshortcat’s backwarding vtableshortcat’s backwarding vtable
0 ack backward to longcat.ack()
1 meow backward to longcat.meow()
2 zzz backward to longcat.zzz()
![Page 21: extension overriding and selflkuper/talks/rust-objects/... · 2011-09-10 · Some pieces of the Rust object system: extension, overriding, and self Lindsey Kuper Indiana University](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed2ddcaa079355bb26da18a/html5/thumbnails/21.jpg)
How to fix it
9
longcat’s vtablelongcat’s vtablelongcat’s vtable
0 ack forward to shortcat.ack()1 lol ret “lol”
2 meow forward to shortcat.meow()3 nyan ret “nyan”
4 zzz forward to shortcat.zzz()
shortcat’s vtableshortcat’s vtableshortcat’s vtable
0 ack ret “ack”
1 meow ret “meow”
2 zzz ret self.meow()
shortcat’s backwarding vtableshortcat’s backwarding vtableshortcat’s backwarding vtable
0 ack backward to longcat.ack()
1 meow backward to longcat.meow()
2 zzz backward to longcat.zzz()
self
![Page 22: extension overriding and selflkuper/talks/rust-objects/... · 2011-09-10 · Some pieces of the Rust object system: extension, overriding, and self Lindsey Kuper Indiana University](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed2ddcaa079355bb26da18a/html5/thumbnails/22.jpg)
Self-dispatch + object extension + overriding
10
![Page 23: extension overriding and selflkuper/talks/rust-objects/... · 2011-09-10 · Some pieces of the Rust object system: extension, overriding, and self Lindsey Kuper Indiana University](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed2ddcaa079355bb26da18a/html5/thumbnails/23.jpg)
Self-dispatch + object extension + overriding
10
![Page 24: extension overriding and selflkuper/talks/rust-objects/... · 2011-09-10 · Some pieces of the Rust object system: extension, overriding, and self Lindsey Kuper Indiana University](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed2ddcaa079355bb26da18a/html5/thumbnails/24.jpg)
Self-dispatch + object extension + overriding
10
![Page 25: extension overriding and selflkuper/talks/rust-objects/... · 2011-09-10 · Some pieces of the Rust object system: extension, overriding, and self Lindsey Kuper Indiana University](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed2ddcaa079355bb26da18a/html5/thumbnails/25.jpg)
Self-dispatch + object extension + overriding
10
Caveat: Some disagreement on whether it should work this way (see: Aldrich, “Selective Open Recursion”)
![Page 26: extension overriding and selflkuper/talks/rust-objects/... · 2011-09-10 · Some pieces of the Rust object system: extension, overriding, and self Lindsey Kuper Indiana University](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed2ddcaa079355bb26da18a/html5/thumbnails/26.jpg)
Self-dispatch + object extension + overriding
10
![Page 27: extension overriding and selflkuper/talks/rust-objects/... · 2011-09-10 · Some pieces of the Rust object system: extension, overriding, and self Lindsey Kuper Indiana University](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed2ddcaa079355bb26da18a/html5/thumbnails/27.jpg)
Self-dispatch + object extension + overriding
10
longercat’s vtablelongercat’s vtablelongercat’s vtable
0 ack forward to shortcat.ack()1 meow ret “zzz”
2 zzz forward to shortcat.zzz()
![Page 28: extension overriding and selflkuper/talks/rust-objects/... · 2011-09-10 · Some pieces of the Rust object system: extension, overriding, and self Lindsey Kuper Indiana University](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed2ddcaa079355bb26da18a/html5/thumbnails/28.jpg)
Self-dispatch + object extension + overriding
10
longercat’s vtablelongercat’s vtablelongercat’s vtable
0 ack forward to shortcat.ack()1 meow ret “zzz”
2 zzz forward to shortcat.zzz()
shortcat’s vtableshortcat’s vtableshortcat’s vtable
0 ack ret “ack”
1 meow ret “meow”
2 zzz ret self.meow()
![Page 29: extension overriding and selflkuper/talks/rust-objects/... · 2011-09-10 · Some pieces of the Rust object system: extension, overriding, and self Lindsey Kuper Indiana University](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed2ddcaa079355bb26da18a/html5/thumbnails/29.jpg)
Self-dispatch + object extension + overriding
10
longercat’s vtablelongercat’s vtablelongercat’s vtable
0 ack forward to shortcat.ack()1 meow ret “zzz”
2 zzz forward to shortcat.zzz()
shortcat’s vtableshortcat’s vtableshortcat’s vtable
0 ack ret “ack”
1 meow ret “meow”
2 zzz ret self.meow()
shortcat’s backwarding vtableshortcat’s backwarding vtableshortcat’s backwarding vtable
0 ack backward to longercat.ack()
1 meow backward to longercat.meow()
2 zzz backward to longercat.zzz()
![Page 30: extension overriding and selflkuper/talks/rust-objects/... · 2011-09-10 · Some pieces of the Rust object system: extension, overriding, and self Lindsey Kuper Indiana University](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed2ddcaa079355bb26da18a/html5/thumbnails/30.jpg)
Self-dispatch + object extension + overriding
10
longercat’s vtablelongercat’s vtablelongercat’s vtable
0 ack forward to shortcat.ack()1 meow ret “zzz”
2 zzz forward to shortcat.zzz()
shortcat’s vtableshortcat’s vtableshortcat’s vtable
0 ack ret “ack”
1 meow ret “meow”
2 zzz ret self.meow()
shortcat’s backwarding vtableshortcat’s backwarding vtableshortcat’s backwarding vtable
0 ack backward to longercat.ack()
1 meow backward to longercat.meow()
2 zzz backward to longercat.zzz()
self
![Page 31: extension overriding and selflkuper/talks/rust-objects/... · 2011-09-10 · Some pieces of the Rust object system: extension, overriding, and self Lindsey Kuper Indiana University](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed2ddcaa079355bb26da18a/html5/thumbnails/31.jpg)
Self-dispatch + object extension + overriding
1111
to arbitrary depth
![Page 32: extension overriding and selflkuper/talks/rust-objects/... · 2011-09-10 · Some pieces of the Rust object system: extension, overriding, and self Lindsey Kuper Indiana University](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed2ddcaa079355bb26da18a/html5/thumbnails/32.jpg)
Self-dispatch + object extension + overriding
1111
to arbitrary depth
![Page 33: extension overriding and selflkuper/talks/rust-objects/... · 2011-09-10 · Some pieces of the Rust object system: extension, overriding, and self Lindsey Kuper Indiana University](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed2ddcaa079355bb26da18a/html5/thumbnails/33.jpg)
Self-dispatch + object extension + overriding
1111
to arbitrary depth
![Page 34: extension overriding and selflkuper/talks/rust-objects/... · 2011-09-10 · Some pieces of the Rust object system: extension, overriding, and self Lindsey Kuper Indiana University](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed2ddcaa079355bb26da18a/html5/thumbnails/34.jpg)
Self-dispatch + object extension + overriding
1111
to arbitrary depth
![Page 35: extension overriding and selflkuper/talks/rust-objects/... · 2011-09-10 · Some pieces of the Rust object system: extension, overriding, and self Lindsey Kuper Indiana University](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed2ddcaa079355bb26da18a/html5/thumbnails/35.jpg)
Self-dispatch + object extension + overriding
1111
to arbitrary depth■ We need a way to temporarily pretend that self is the inner object, while still keeping track of what the extended self is
![Page 36: extension overriding and selflkuper/talks/rust-objects/... · 2011-09-10 · Some pieces of the Rust object system: extension, overriding, and self Lindsey Kuper Indiana University](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed2ddcaa079355bb26da18a/html5/thumbnails/36.jpg)
Self-dispatch + object extension + overriding
1111
to arbitrary depth■ We need a way to temporarily pretend that self is the inner object, while still keeping track of what the extended self is
■ Solution: create a stack of “fake selves” threaded through the run-time stack
![Page 37: extension overriding and selflkuper/talks/rust-objects/... · 2011-09-10 · Some pieces of the Rust object system: extension, overriding, and self Lindsey Kuper Indiana University](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed2ddcaa079355bb26da18a/html5/thumbnails/37.jpg)
Self-dispatch + object extension + overriding
1111
to arbitrary depth■ We need a way to temporarily pretend that self is the inner object, while still keeping track of what the extended self is
■ Solution: create a stack of “fake selves” threaded through the run-time stack
■ Every forwarding function allocates space in its frame for a “fake self” comprising a backwarding vtable and an inner object body
![Page 38: extension overriding and selflkuper/talks/rust-objects/... · 2011-09-10 · Some pieces of the Rust object system: extension, overriding, and self Lindsey Kuper Indiana University](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed2ddcaa079355bb26da18a/html5/thumbnails/38.jpg)
Possible formalisms for reasoning about this stuff
12
![Page 39: extension overriding and selflkuper/talks/rust-objects/... · 2011-09-10 · Some pieces of the Rust object system: extension, overriding, and self Lindsey Kuper Indiana University](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed2ddcaa079355bb26da18a/html5/thumbnails/39.jpg)
Possible formalisms for reasoning about this stuff
■ Abadi and Cardelli: Self-types
12
![Page 40: extension overriding and selflkuper/talks/rust-objects/... · 2011-09-10 · Some pieces of the Rust object system: extension, overriding, and self Lindsey Kuper Indiana University](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed2ddcaa079355bb26da18a/html5/thumbnails/40.jpg)
Possible formalisms for reasoning about this stuff
■ Abadi and Cardelli: Self-types■ A close cousin to recursive types
12
![Page 41: extension overriding and selflkuper/talks/rust-objects/... · 2011-09-10 · Some pieces of the Rust object system: extension, overriding, and self Lindsey Kuper Indiana University](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed2ddcaa079355bb26da18a/html5/thumbnails/41.jpg)
Possible formalisms for reasoning about this stuff
■ Abadi and Cardelli: Self-types■ A close cousin to recursive types
■ Can be encoded using a combination of recursive and existential types
12
![Page 42: extension overriding and selflkuper/talks/rust-objects/... · 2011-09-10 · Some pieces of the Rust object system: extension, overriding, and self Lindsey Kuper Indiana University](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed2ddcaa079355bb26da18a/html5/thumbnails/42.jpg)
Possible formalisms for reasoning about this stuff
■ Abadi and Cardelli: Self-types■ A close cousin to recursive types
■ Can be encoded using a combination of recursive and existential types
■ Fisher, Honsell, and Mitchell: “A lambda calculus of objects and method specialization”
12
![Page 43: extension overriding and selflkuper/talks/rust-objects/... · 2011-09-10 · Some pieces of the Rust object system: extension, overriding, and self Lindsey Kuper Indiana University](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed2ddcaa079355bb26da18a/html5/thumbnails/43.jpg)
Possible formalisms for reasoning about this stuff
■ Abadi and Cardelli: Self-types■ A close cousin to recursive types
■ Can be encoded using a combination of recursive and existential types
■ Fisher, Honsell, and Mitchell: “A lambda calculus of objects and method specialization”■ “a type system that allows methods to be specialized
appropriately as they are inherited”
12
![Page 44: extension overriding and selflkuper/talks/rust-objects/... · 2011-09-10 · Some pieces of the Rust object system: extension, overriding, and self Lindsey Kuper Indiana University](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed2ddcaa079355bb26da18a/html5/thumbnails/44.jpg)
Possible formalisms for reasoning about this stuff
■ Abadi and Cardelli: Self-types■ A close cousin to recursive types
■ Can be encoded using a combination of recursive and existential types
■ Fisher, Honsell, and Mitchell: “A lambda calculus of objects and method specialization”■ “a type system that allows methods to be specialized
appropriately as they are inherited”
■ ...
12
![Page 46: extension overriding and selflkuper/talks/rust-objects/... · 2011-09-10 · Some pieces of the Rust object system: extension, overriding, and self Lindsey Kuper Indiana University](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed2ddcaa079355bb26da18a/html5/thumbnails/46.jpg)
14Photo by jamesrbowe on Flickr. Thanks!
Questions?
Thanks to:Graydon Hoare and everyone on the Rust teamDave Herman and everyone at Mozilla Research