A kernel of another newtype idea

[Warning: writing at 2AM.]

Sometimes when I'm thinking about one enhancement to Swift, thoughts of another one come up. This isn't fully formed, but I want to record it here.

I don't know if I said it here or not, but since a newtype on a struct is still a product type, and one on an enum is still a sum type, instead of making a newtype nominal type in parallel to those, make it a modifier on a "base" type:

struct MyNewStruct: newtype MyOldStruct, myNewProtocol {

enum MyNewEnum: newtype (public fileprivate(set)) MyOldEnum, myRehashedProtocol {

The original type is available to the new one with the RawRepresentable interface, but it's not necessarily public by default. (I hadn't considered before that a developer might want to hide the fact they're using a strong type-alias.) If you don't specify a access level, it defaults to the most restrictive of the new type's publicity, the old type's publicity, and internal. (So a public strong type-alias of another public type is still uses internal access to the RawRepresentable interface by default.) You can't make the RawRepresentable interface more public than either the old or new types.

I've been thinking about reject and permit statements to specify which states of the old the that the new type uses, but not in too much detail. By default, all states are permitted, i.e. a strong type-alias. We can also go "screw it" and use the RawRepresentable.init? to filter values, at the cost of less code elision.

As with previous version of the newtype idea, we need a way to selectively copy the old type's interface over as needed. But: we do not need it for Beta 1, since we can always manually write our trampolines. It probably needs to be done by Beta 2 or 3, but my point is that our initial implementation doesn't need to have it.

(Although I implied going struct to struct and enum to enum, we can go crossways! Certain values and value families of a struct can form the cases of its sum-type new-type. A specific enum case can form the base of its product-type new-type.)

1 Like
Terms of Service

Privacy Policy

Cookie Policy