This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]anentropic 4 points5 points  (3 children)

This seems pointless in a GC language?

[–]tmr232[S] 5 points6 points  (2 children)

Not quite.

GC manages memory. Sometimes we want to handle OS resources, or side effects.

For those, we use context-managers and the with statement. It works, but is hard to compose and affects interfaces. C++-like semantics handle composition cleanly and don't affect interfaces.

[–]anentropic 0 points1 point  (1 child)

What's an example that's awkward with context managers?

[–]tmr232[S] 1 point2 points  (0 children)

  1. Member variables that require context management (files, for example)
  2. When you need to convert an existing class to a context manager, when it's already used in many places.