Live data from Hacker News

Type-Level Programming in Rust

willcrichton.net

1–10 of 29 posts

Re: Type-Level Programming in Rust

#2
A few months later, Will released Tyrade, an impressive macro-based DSL for type-level programming in Rust: https://github.com/willcrichton/tyrade

The README uses some of the same motivating examples as the blog post.

Type-level programming absolutely has its place. Rust's `typenum` and `generic_array` crates enabled projects like `nalgebra` long before const generics. I recently used type-level programming to prototype an extension to Rust's type system — that's now an RFC (#2981)!

Re: Type-Level Programming in Rust

#3
I'm immediately turned off by the entirely gratuitous use of unsafe here. I clicked through to the linked Reddit discussion and I'm not convinced the author has really thought it through. Writing, "pedagogically, that's irrelevant to the post, so I didn't highlight it" misunderstands the pedagogy here.

However, there's some really neat stuff later, so it's wise to grit your teeth and get past it.

In case anyone else had the same reaction, here's an entirely-safe version of the first snippet that you can pretend replaced the original:

    // Each state is a unique type
    struct Receiving;
    struct Sending;

    // The state machine is parameterized by the state
    struct Channel {
      chan: ...,
      _state: PhantomData
    }

    // Methods for the state are uniquely associated with only the state
    impl Channel {
      // recv consumes ownership, ensuring old state is invalidated
      fn recv(mut self) -> (Channel, String) {
        let Channel { chan, .. } = self;
        let msg = chan.recv();
        // The state type changes after executing a transition
        let next = Channel { chan, _state: PhantomData };
        (next, msg)
      }
    }

    impl Channel {
      fn send(mut self, msg: String) -> Channel {
        let Channel { chan, .. } = self;
        chan.send(msg);
        Channel { chan, _state: PhantomData }
      }
    }

    #[test]
    fn channel_test() {
      let c: Channel = Channel::new();
      let c: Channel = c.send("hi");
      let (c, msg) = c.recv();
      // and so on
    }

Re: Type-Level Programming in Rust

#4
post #3

I'm immediately turned off by the entirely gratuitous use of unsafe here. I clicked through to the linked Reddit discussion and I'm not convinced the author has really thought it through. Writing, "pedagogically, that's irrelevant to the post, so I didn't highlight it" misunderstands the pedagogy here. However, there's some really neat stuff later, so it's wise to grit your teeth and get past it. In case anyone else…

Thank you for going to the trouble of posting the code. I kind of agree with your point here, especially because the safe version is hardly more arduous.

Re: Type-Level Programming in Rust

#7
Just please don't overdo type-level programming, especially not if it involves trait bounds and wildcard impl.

Finding the right balance between using type-level programming to ensure invariants and not using it is a important skill.

Re: Type-Level Programming in Rust

#8
post #3

I'm immediately turned off by the entirely gratuitous use of unsafe here. I clicked through to the linked Reddit discussion and I'm not convinced the author has really thought it through. Writing, "pedagogically, that's irrelevant to the post, so I didn't highlight it" misunderstands the pedagogy here. However, there's some really neat stuff later, so it's wise to grit your teeth and get past it. In case anyone else…

Im not disagreeing with you but fyi will crichton definitely knows what he is talking about. He teaches Programming Language courses at Stanford and his brother is the all-time top contributor to Rust. So, pedagogy + rust is sort of his thing.

In general, this series of posts has been more about types than about rust so I don't blame him for ignoring unsafe. It isnt relevant (its also not a best practice!)

Re: Type-Level Programming in Rust

#10
post #8
post #3

I'm immediately turned off by the entirely gratuitous use of unsafe here. I clicked through to the linked Reddit discussion and I'm not convinced the author has really thought it through. Writing, "pedagogically, that's irrelevant to the post, so I didn't highlight it" misunderstands the pedagogy here. However, there's some really neat stuff later, so it's wise to grit your teeth and get past it. In case anyone else…

Im not disagreeing with you but fyi will crichton definitely knows what he is talking about. He teaches Programming Language courses at Stanford and his brother is the all-time top contributor to Rust. So, pedagogy + rust is sort of his thing. In general, this series of posts has been more about types than about rust so I don't blame him for ignoring unsafe. It isnt relevant (its also not a best practice!)

> his brother is the all-time top contributor to Rust.

Say no more! Any brother of such renowned talent is sure to know what they're talking about.

Post reply on HN