[swift-evolution] [Review] SE-0096: Converting dynamicType from a property to an operator

Brent Royal-Gordon brent at architechies.com
Wed May 25 07:09:57 CDT 2016


> 	* What is your evaluation of the proposal?

I think it's a good idea. While the type is in some sense a "property" of the instance, it has always been a bit strange to have it there:

* `dynamicType` acts sort of like an extension of `Any`, but you're not actually allowed to extend `Any`.
* Using property syntax forced the designers to avoid the most obvious name, `type`, because users might want that name for their own purposes.
* Similar "universal" properties, like getting the size of an instance or reflecting it, are exposed as free functions. A fair number of the standard library's free functions could reasonably be exposed as members of `Any` or `AnyObject`, yet `dynamicType` was singled out for this treatment.

I am not convinced, however, by this proposal's argument against renaming to `typeof(_:)`. In a very brief survey of languages with a `typeof` operation (or some extremely similar keyword, like `TypeOf` or `type-of`), I found:

LANGUAGES WITH ONLY STATIC TYPES

* C: Textual substitution (more or less) of static type. (C++ and Objective-C inherit this from C.)

LANGUAGES WITH ONLY DYNAMIC TYPES

* Javascript: Dynamic type (string)
* R: Dynamic type (string)
* ActionScript: Dynamic type (string)
* Common Lisp: Dynamic type

LANGUAGES WITH BOTH

* D: Textual substitution (more or less) of static type, like C
* Crystal: Static type
* C#: N/A; used only with type names, in a similar role to `self` in Swift 2's `SomeType.self`

* Go: Dynamic type
* Visual Basic: Dynamic type
* GNU Pascal: Dynamic type

My conclusion is that, for the most part, languages use `typeof` in one of two ways:

	1) It is part of their type grammar, where it represents a static type.

	2) It is part of their expression grammar, where it represents a dynamic type.

(In Crystal, `typeof` is part of both grammars, and always represents a static type. C# uses it in a weird, idiosyncratic way.)

If Swift were to support category 1, it would almost certainly be considered "compiler magic" and thus be spelled `#typeof`. So I don't think there's much ambiguity in what `typeof(_:)` would mean in Swift.

Given that using it to mean "dynamic type" is highly precedented in other languages, and given that it is much shorter than `dynamicType` and matches an existing pattern in the standard library, I believe we should use the name `typeof` instead of `dynamicType`.

> 	* Is the problem being addressed significant enough to warrant a change to Swift?

Yes. Let's get rid of this compromised weirdness.

> 	* Does this proposal fit well with the feel and direction of Swift?

Yes. We've been clearing out these kinds of cobwebs a lot lately.

> 	* If you have used other languages or libraries with a similar feature, how do you feel that this proposal compares to those?

Most languages where I've used this kind of thing use `class` to return the dynamic type, but this isn't a sensible option in Swift, which has richly-typed instances which aren't classes.

> 	* How much effort did you put into your review? A glance, a quick reading, or an in-depth study?

I wouldn't quite call this review "in-depth", but I put maybe an hour of work into it.

-- 
Brent Royal-Gordon
Architechies



More information about the swift-evolution mailing list