<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<div name="messageBodySection" style="font-size: 14px; font-family: -apple-system, BlinkMacSystemFont, sans-serif;">Hello everyone. This is my first contribution to Swift Evolution. In summary: <span style="font-family: inherit; font-size: 13px; white-space: pre-wrap;">It would be nice if the type casting operator accepted expressions on its right side.</span>
<div><span style="font-family: inherit; font-size: 13px; white-space: pre-wrap;"><br /></span></div>
<div>
<pre class="preContent" style="outline: none; font-family: inherit; font-size: 13px; margin-top: 0px; margin-bottom: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word;">
So, basically, the type casting operator only accepts types and can't process expressions on its right side.
Having a class A, and a subclass of A, B:
class A {}
class B: A {}
And a function that returns the passed type:
func type<T: A>(t: T.Type) -> T.Type {
return t
}
The following scenario works:
let b = B()
let a = (b as A)
However the following scenario doesn't:
let b = B()
let a = (b as type(A))
</pre>
<pre class="preContent" style="outline: none; font-family: inherit; font-size: 13px; margin-top: 0px; margin-bottom: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word;">
<br />
</pre>
<pre class="preContent" style="outline: none; font-family: inherit; font-size: 13px; margin-top: 0px; margin-bottom: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word;">
Having the “as” accept expressions on its right side would be really useful and could enable some truly great generic algorithm implementations.
</pre>
<pre class="preContent" style="outline: none; font-family: inherit; font-size: 13px; margin-top: 0px; margin-bottom: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word;">
<br />
</pre>
<pre class="preContent" style="outline: none; font-family: inherit; font-size: 13px; margin-top: 0px; margin-bottom: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word;">
Looking forward to what you guys think about this.
</pre></div>
</div>
<div name="messageSignatureSection" style="font-size: 14px; font-family: -apple-system, BlinkMacSystemFont, sans-serif;"><br />
— Oscar Swanros | @swanros
<div><br /></div>
</div>
</body>
</html>