default methods in rust
play

Default Methods in Rust Michael Sullivan August 14, 2013 1 / 30 - PowerPoint PPT Presentation

Introduction Rust Fixing Default Trait Methods Other Conclusion Default Methods in Rust Michael Sullivan August 14, 2013 1 / 30 Introduction Rust Fixing Default Trait Methods Other Conclusion Outline Introduction Rust Fixing Default


  1. Introduction Rust Fixing Default Trait Methods Other Conclusion Default Methods in Rust Michael Sullivan August 14, 2013 1 / 30

  2. Introduction Rust Fixing Default Trait Methods Other Conclusion Outline Introduction Rust Fixing Default Trait Methods Other 2 / 30

  3. ❼ ❼ ❼ Introduction Rust Fixing Default Trait Methods Other Conclusion Disclaimer 3 / 30

  4. ❼ ❼ Introduction Rust Fixing Default Trait Methods Other Conclusion Disclaimer ❼ Rust is under heavy development. 3 / 30

  5. ❼ Introduction Rust Fixing Default Trait Methods Other Conclusion Disclaimer ❼ Rust is under heavy development. ❼ The things described in this talk may not be true tomorrow. 3 / 30

  6. Introduction Rust Fixing Default Trait Methods Other Conclusion Disclaimer ❼ Rust is under heavy development. ❼ The things described in this talk may not be true tomorrow. ❼ What I discuss and how I present issues reflect my personal biases in language design. 3 / 30

  7. ❼ ❼ ❼ ❼ Introduction Rust Fixing Default Trait Methods Other Conclusion Goals What do we want in a programming language? 4 / 30

  8. ❼ ❼ ❼ Introduction Rust Fixing Default Trait Methods Other Conclusion Goals What do we want in a programming language? ❼ Fast: generates efficient machine code 4 / 30

  9. ❼ ❼ Introduction Rust Fixing Default Trait Methods Other Conclusion Goals What do we want in a programming language? ❼ Fast: generates efficient machine code ❼ Safe: type system provides guarantees that prevent certain bugs 4 / 30

  10. ❼ Introduction Rust Fixing Default Trait Methods Other Conclusion Goals What do we want in a programming language? ❼ Fast: generates efficient machine code ❼ Safe: type system provides guarantees that prevent certain bugs ❼ Concurrent: easy to build concurrent programs and to take advantage of parallelism 4 / 30

  11. Introduction Rust Fixing Default Trait Methods Other Conclusion Goals What do we want in a programming language? ❼ Fast: generates efficient machine code ❼ Safe: type system provides guarantees that prevent certain bugs ❼ Concurrent: easy to build concurrent programs and to take advantage of parallelism ❼ “Systemsy”: fine grained control, predictable performance characteristics 4 / 30

  12. ❼ ❼ ❼ ❼ Introduction Rust Fixing Default Trait Methods Other Conclusion Goals What do have? ❼ Firefox is in C++, which is Fast and Systemsy 5 / 30

  13. ❼ ❼ ❼ Introduction Rust Fixing Default Trait Methods Other Conclusion Goals What do have? ❼ Firefox is in C++, which is Fast and Systemsy ❼ ML is (sometimes) fast and (very) safe 5 / 30

  14. ❼ ❼ Introduction Rust Fixing Default Trait Methods Other Conclusion Goals What do have? ❼ Firefox is in C++, which is Fast and Systemsy ❼ ML is (sometimes) fast and (very) safe ❼ Erlang is safe and concurrent 5 / 30

  15. ❼ Introduction Rust Fixing Default Trait Methods Other Conclusion Goals What do have? ❼ Firefox is in C++, which is Fast and Systemsy ❼ ML is (sometimes) fast and (very) safe ❼ Erlang is safe and concurrent ❼ Haskell is (sometimes) fast, (very) safe, and concurrent 5 / 30

  16. Introduction Rust Fixing Default Trait Methods Other Conclusion Goals What do have? ❼ Firefox is in C++, which is Fast and Systemsy ❼ ML is (sometimes) fast and (very) safe ❼ Erlang is safe and concurrent ❼ Haskell is (sometimes) fast, (very) safe, and concurrent ❼ Java and C# are fast and safe 5 / 30

  17. Introduction Rust Fixing Default Trait Methods Other Conclusion Rust a systems language pursuing the trifecta safe, concurrent, fast -lkuper 6 / 30

  18. Introduction Rust Fixing Default Trait Methods Other Conclusion Rust Design Status 7 / 30

  19. Introduction Rust Fixing Default Trait Methods Other Conclusion Design Type system features ❼ Algebraic data type and pattern matching (no null pointers!) ❼ Polymorphism: functions and types can have generic type parameters ❼ Type inference on local variables ❼ A somewhat idiosyncratic typeclass system (“traits”) ❼ Data structures are immutable by default ❼ Region pointers allow safe pointers into non-heap objects 8 / 30

  20. Introduction Rust Fixing Default Trait Methods Other Conclusion Design Other features ❼ Lightweight tasks with no shared state ❼ Control over memory allocation ❼ Move semantics, unique pointers 9 / 30

  21. Introduction Rust Fixing Default Trait Methods Other Conclusion Design ...What? “It’s like C++ grew up, went to grad school, started dating Haskell, and is sharing an office with Erlang.” 10 / 30

  22. ❼ ❼ Introduction Rust Fixing Default Trait Methods Other Conclusion Status rustc ❼ Self-hosting rust compiler 11 / 30

  23. ❼ Introduction Rust Fixing Default Trait Methods Other Conclusion Status rustc ❼ Self-hosting rust compiler ❼ Uses LLVM as a backend 11 / 30

  24. Introduction Rust Fixing Default Trait Methods Other Conclusion Status rustc ❼ Self-hosting rust compiler ❼ Uses LLVM as a backend ❼ Handles polymorphism and typeclasses by monomorphizing 11 / 30

  25. ❼ ❼ ❼ Introduction Rust Fixing Default Trait Methods Other Conclusion Status The catch ❼ Not quite ready for prime time 12 / 30

  26. ❼ ❼ Introduction Rust Fixing Default Trait Methods Other Conclusion Status The catch ❼ Not quite ready for prime time ❼ Lots of bugs and exposed sharp edges 12 / 30

  27. ❼ Introduction Rust Fixing Default Trait Methods Other Conclusion Status The catch ❼ Not quite ready for prime time ❼ Lots of bugs and exposed sharp edges ❼ Language still evolving 12 / 30

  28. Introduction Rust Fixing Default Trait Methods Other Conclusion Status The catch ❼ Not quite ready for prime time ❼ Lots of bugs and exposed sharp edges ❼ Language still evolving ❼ But getting really close! 12 / 30

  29. Introduction Rust Fixing Default Trait Methods Other Conclusion Traits What are traits? ❼ Traits are interfaces that specify a set of methods for types to implement ❼ Functions can be parameterized over types that implement a certain trait ❼ Like typeclasses in Haskell 13 / 30

  30. Introduction Rust Fixing Default Trait Methods Other Conclusion Traits Trait example trait ToStr { fn to_str (& self) -> ~str; } impl ToStr for int { fn to_str (& self) -> ~str { int:: to_str (* self) } } fn exclaim <T: ToStr >(x: T) -> ~str { x.to_str () + ~"!" } 14 / 30

  31. Introduction Rust Fixing Default Trait Methods Other Conclusion Traits More trait example impl <T: ToStr > ToStr for ~[T] { fn to_str (& self) -> ~str { let strs = self.map(|x| x.to_str ()); fmt!("[%s]", strs) } } impl <T: ToStr > ToStr for Option <T> { fn to_str (& self) -> ~str { match self { &None => ~"None", &Some(ref t) => fmt!("Some (%s)", t.to_str ( } } } 15 / 30

  32. Introduction Rust Fixing Default Trait Methods Other Conclusion Default methods A solution ❼ Sometimes you have a method that has a straightforward “default” ❼ But want to be able to override it 16 / 30

  33. Introduction Rust Fixing Default Trait Methods Other Conclusion Default methods A simple example: equality trait Eq { fn eq(&self , other: &Self) -> bool; fn ne(&self , other: &Self) -> bool { !self.eq(other) } } 17 / 30

  34. Introduction Rust Fixing Default Trait Methods Other Conclusion Default methods An implementation without overriding ❼ Implementations can choose to use the default implementation... impl Eq for int { fn eq(&self , other: &int) -> bool { *self == *other } } 18 / 30

  35. Introduction Rust Fixing Default Trait Methods Other Conclusion Default methods An implementation with overriding ❼ ... or to override it impl Eq for int { fn eq(&self , other: &int) -> bool { *self == *other } fn ne(&self , other: &Self) -> bool { *self != *other } } 19 / 30

  36. ❼ Introduction Rust Fixing Default Trait Methods Other Conclusion Default methods Why override? ❼ Overriding can be useful for performance 20 / 30

  37. Introduction Rust Fixing Default Trait Methods Other Conclusion Default methods Why override? ❼ Overriding can be useful for performance ❼ And is sometimes semantically necessary (the default implementation is not correct for floating point) 20 / 30

  38. ❼ Introduction Rust Fixing Default Trait Methods Other Conclusion Problems The state at the start of the summer ❼ The above examples worked... 21 / 30

  39. Introduction Rust Fixing Default Trait Methods Other Conclusion Problems The state at the start of the summer ❼ The above examples worked... ❼ But anything much more complicated didn’t 21 / 30

  40. ❼ Introduction Rust Fixing Default Trait Methods Other Conclusion Problems Type parameters... trait A<T> { fn g(&self , x: T) -> T { x } } impl A<int > for int { } fn main () { assert !(0i.g(2i) == 2i); } ❼ Triggered an ICE 22 / 30

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend