[Pre-Pitch] SwiftAwake()

As I think about what I wanted this for (serialization), I wonder if it would be enough (or a good first-pass.. or something else entirely) to just have a magical protocol or attribute that basically makes name mangling for that type publicly available. In other words, all it provides is a mechanism to get a String that can then any time later be turned back into the very same Type (and only that type). This would eliminate any need to worry about things like iterating all possible types, defining an ordering, worrying about conditional conformance, etc, etc.

So this might not be the best way to go about it, but imagine like:

@identifiableType class Foo {}
@identifiableType struct Bar {}
struct Baz {}

let fooTypeString = String(identifierFor: Foo.self)
let barTypeString = String(identifierFor: Bar.self)
let bazTypeString = String(identifierFor: Baz.self) // error

// some free function, maybe, named something like:
func type(for identifier: String) -> Any.Type?

// so then you can do this:
let fooType = type(for: fooTypeString) // == Foo.self
let bazType = type(for: bazTypeString) // == nil

And the idea there would be that if you passed the type(for:) function any string that doesn't exactly conform to the mangled name of a known @identifiableType, it would return nil even if the mangling is exactly correct for an existing type. Likewise, the String(identifierFor:) initializer would, perhaps ideally, not even compile if you attempt to pass a type isn't @identifiableType. (Alternatively it could be a failable initializer and potentially return nil maybe.)

This is obviously quite a lot different from iterable types or SwiftAwake()... :stuck_out_tongue:

edit:

I still feel like I want this, but while I was writing this up, I got a reply in another thread that does a pretty convincing job of explaining why this might not be a good idea at all: Why is it possible to get a class from a String but not a struct or enum? - #13 by lukasa

1 Like