46
Some pieces of the Rust object system: extension, overriding, and self Lindsey Kuper Indiana University Programming Languages Group September 9, 2011 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

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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 45: 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

Go check it out!

13

http://rust-lang.org

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

14Photo by jamesrbowe on Flickr. Thanks!

Questions?

Thanks to:Graydon Hoare and everyone on the Rust teamDave Herman and everyone at Mozilla Research