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 Trait Methods Other 2 / 30
❼ ❼ ❼ Introduction Rust Fixing Default Trait Methods Other Conclusion Disclaimer 3 / 30
❼ ❼ Introduction Rust Fixing Default Trait Methods Other Conclusion Disclaimer ❼ Rust is under heavy development. 3 / 30
❼ 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
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
❼ ❼ ❼ ❼ Introduction Rust Fixing Default Trait Methods Other Conclusion Goals What do we want in a programming language? 4 / 30
❼ ❼ ❼ Introduction Rust Fixing Default Trait Methods Other Conclusion Goals What do we want in a programming language? ❼ Fast: generates efficient machine code 4 / 30
❼ ❼ 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
❼ 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
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
❼ ❼ ❼ ❼ Introduction Rust Fixing Default Trait Methods Other Conclusion Goals What do have? ❼ Firefox is in C++, which is Fast and Systemsy 5 / 30
❼ ❼ ❼ 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
❼ ❼ 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
❼ 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
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
Introduction Rust Fixing Default Trait Methods Other Conclusion Rust a systems language pursuing the trifecta safe, concurrent, fast -lkuper 6 / 30
Introduction Rust Fixing Default Trait Methods Other Conclusion Rust Design Status 7 / 30
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
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
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
❼ ❼ Introduction Rust Fixing Default Trait Methods Other Conclusion Status rustc ❼ Self-hosting rust compiler 11 / 30
❼ Introduction Rust Fixing Default Trait Methods Other Conclusion Status rustc ❼ Self-hosting rust compiler ❼ Uses LLVM as a backend 11 / 30
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
❼ ❼ ❼ Introduction Rust Fixing Default Trait Methods Other Conclusion Status The catch ❼ Not quite ready for prime time 12 / 30
❼ ❼ 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
❼ 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
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
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
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
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
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
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
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
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
❼ Introduction Rust Fixing Default Trait Methods Other Conclusion Default methods Why override? ❼ Overriding can be useful for performance 20 / 30
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
❼ Introduction Rust Fixing Default Trait Methods Other Conclusion Problems The state at the start of the summer ❼ The above examples worked... 21 / 30
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
❼ 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
Recommend
More recommend