logoalt Hacker News

travisalast Wednesday at 2:55 AM2 repliesview on HN

The linked documentation in the first paragraph is confusing. It sets things in `context`, then calls `supertramp()`, which doesn't even use `context.user_index` or `my_custom_allocator()` specifically, which is not defined in the example? Are we to assume that `new()` somehow makes use of `context.allocator` and `context.user_index`? It goes on to explain that new(x) is also `new(x, context.allocator)`, but never explains the relevance of `context.user_index` No wonder people "misunderstand"


Replies

gingerBilllast Wednesday at 9:07 AM

The code example is more to show the scoped copy-on-write behaviour of `context` more than how it is used in practice. I agree it might be a bit confusing and thus needs to be made clearer that is what the code example is meant to show. However, I don't there is any code example that could show "correct usage" of `context` without it defeating the point of it. As I say in the article, it makes more sense to leave it "confusing" because then people will ignore it.

The Overview is just that, an "Overview" and it's not meant be either a spec, a heavily detailed tutorial, nor an explanation of why a language construct is designed the way it is and/or why it exists. The latter of which would probably be as long as the Overview for each construct that exists if that.

brabellast Wednesday at 6:53 AM

The implicit idea is that new will always use a context allocator to create the thing. Not its own allocator that you cannot control. But yes that does not seem obvious to the guy trying to learn the language.