> On Jun 22, 2016, at 9:12 AM, Charlie Monroe wrote:
>
> Please consider the following code:
>
> let myString: String? = "Hello"
> let anyValue: Any = myString
> let obj: AnyObject? = anyValue as? AnyObject // nil, since it's
> Optional, String being struct,
>
> On Jun 22, 2016, at 5:51 PM, John McCall wrote:
>
>
>> On Jun 22, 2016, at 12:15 AM, L. Mihalkovic via swift-evolution
>> wrote:
>>
>> Func asOpt(v:Any) -> Optional {
>> If let val = v as? T {
>> Return val
>> }
>> Return nil
>> }
> On Jun 22, 2016, at 12:15 AM, L. Mihalkovic via swift-evolution
> wrote:
>
> Func asOpt(v:Any) -> Optional {
> If let val = v as? T {
> Return val
> }
> Return nil
> }
> Regards
> LM
> (From mobile)
This is just:
return v as? T
John.
>
> On Jun 22,
Nice!
Would you consider this as something that should be part of the language? IMHO
working this out takes much more time than it should and these two functions
would definitely help out a lot.
> On Jun 22, 2016, at 9:15 AM, L. Mihalkovic
> wrote:
>
> Func
Ext Optional {
Func ifUnwraps(_ h: T->()){
Switch self
Case .Some(let v):
If let t=v as? T { h(t) } // invoke if T matches
Def:
Brk
}
Regards
LM
(From mobile)
> On Jun 22, 2016, at 9:15 AM, L. Mihalkovic
> wrote:
>
> Func asOpt(v:Any) ->
Func asOpt(v:Any) -> Optional {
If let val = v as? T {
Return val
}
Return nil
}
Regards
LM
(From mobile)
> On Jun 22, 2016, at 7:11 AM, Charlie Monroe via swift-evolution
> wrote:
>
> Unfortunately, this is not as easy, because automatic bridging won't be
Unfortunately, this is not as easy, because automatic bridging won't be applied:
let myString: String? = "Hello"
let anyValue: Any = myString
myString as? AnyObject // _NSContiguousString
anyValue as? AnyObject // nil, since String is struct
let array: [String]? = ["Hello"]
let anyArray: Any =
'as?' should already do this. If you have an Any that contains an Optional
and cast 'any as? T', you'll get the value inside the Optional if there is one,
or the cast will fail if the optional is nil or the type doesn't match.
-Joe
> On Jun 20, 2016, at 11:00 PM, Charlie Monroe via