Wednesday, March 27, 2024

Noncopyable Generics Walkthrough

Ben Cohen:

Non-copyable generics aren’t for every-day code – but we’ve put a lot of care into making them stay out of your way until you need them, and then keeping them usable once you do. They will allow libraries to unlock more performance and safety for end users.

[…]

To help tie all these pieces together, I wrote up some code that uses all these proposals in order to build a basic singly-linked list type.

[…]

This is a struct that opts out of the default Copyable conformance via : ~Copyable. This allows it to have a deinit, like a class. This type uses no reference counting to know when to destroy the box. The type cannot be copied, so when it goes out of scope, the deinit is called by the compiler.

[…]

The generic placeholder Wrapped, which can stand in for the type of anything you want to put in the box, is also marked ~Copyable. This means that the Box type cannot make any copies of its wrapped type. […] What this ~Copyable annotation means is just that the Box type doesn’t know if the type it holds is copyable, which means it can safely hold both copyable and non-copyable types.

[…]

Sequence, and therefore for…in, does not yet support non-copyable types. Sequence could be made to support it today by marking the protocol up as ~Copyable and having makeIterator() be consuming. However this is probably not desirable. Mostly, you want iteration to be a borrowing operation. Accomplishing this needs more language features.

Previously:

1 Comment RSS · Twitter · Mastodon


And the complexity increases yet again , we’re almost surpassing C++.

Yet another feature I will never need when building iOS apps. I’m glad the language people are having fun building these things, while the most basic things are forever broken. All I need for making software is a working debugger, a non crashing and FAST compiler, less bugs in everything (regexes are broken, Decimal is broken, etc…) and simplicity.

The relative elegance of Objective-C is far away. Seem like the architecture astronauts have won and are more concerned about technical achievements than actually making usable software.

Leave a Comment