Hacker News new | ask | show | jobs
by IshKebab 911 days ago
> Is this on Cap'n Proto? Honestly, I don't know.

It absolutely is. It's a fairly basic principle that APIs should be difficult to misuse, and that fact that you made the same mistake 2/3 times shows that it is very easy to misuse. In other words it is a badly designed API. At the very least it should be called ListView.

I am not a big fan of CapnProto. It has some neat features but it's very complicated, full of footguns like this, and the API is extremely unergonomic.

2 comments

Yeah I agree, the ListView naming is more appropriate.

I haven't used non-owning types like string_view or span too much because I haven't needed that level of performance or memory optimization yet, and so those just seem like footguns as compared to just a reference without those needs. I do like to use a technique in classes that use non-owning references that would work for those too to prevent this particular problem.

For that, there are two methods with the same name, but different access - an lvalue version and an rvalue version. Then, you delete the rvalue method like this:

  class Response {
    auto getListView() & -> ListView {
      return ListView(m_List);
    }
    void getListView() && = delete;
  }
Then you get a compile error like in Rust when you try to call getListView() from a temporary object, but if you call the method from an lvalue it still works at least as long as the object is in scope.
> In other words it is a badly designed API.

I don't agree. The API is what it is because it is specifically a zero copy API for performance. If you don't care about performance, why are you using C++ (stupid) and a zero-copy API (doubly stupid)?

I absolutely do NOT expect a zero copy API to own things. If I drop the underlying reference that is really an alias, how on earth is that the fault of the zero copy API?

The combination of aliasing and lifetimes are C++ footguns--full stop. This is aptly demonstrated by how quickly Rust kills this cold.

If you use sharp knives, sometimes you cut your fingers. People like you would claim the knife is the problem.

I think you misunderstood. It's not a bad API because it uses non-owning references. It's a bad API because it doesn't make that clear.

> If you use sharp knives, sometimes you cut your fingers. People like you would claim the knife is the problem.

This is more like a cutting yourself on a razor sharp butter knife. If it's a sharp knife it should look like a sharp knife.