Re: [swift-evolution] [Pitch] Detecting and working with Optionals from Any

2016-06-22 Thread John McCall via swift-evolution
> 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, >

Re: [swift-evolution] [Pitch] Detecting and working with Optionals from Any

2016-06-22 Thread L. Mihalkovic via swift-evolution
> 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 >> }

Re: [swift-evolution] [Pitch] Detecting and working with Optionals from Any

2016-06-22 Thread John McCall via swift-evolution
> 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,

Re: [swift-evolution] [Pitch] Detecting and working with Optionals from Any

2016-06-22 Thread Charlie Monroe via swift-evolution
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

Re: [swift-evolution] [Pitch] Detecting and working with Optionals from Any

2016-06-22 Thread L. Mihalkovic via swift-evolution
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) ->

Re: [swift-evolution] [Pitch] Detecting and working with Optionals from Any

2016-06-22 Thread L. Mihalkovic via swift-evolution
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

Re: [swift-evolution] [Pitch] Detecting and working with Optionals from Any

2016-06-21 Thread Charlie Monroe via swift-evolution
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 =

Re: [swift-evolution] [Pitch] Detecting and working with Optionals from Any

2016-06-21 Thread Joe Groff via swift-evolution
'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

[swift-evolution] [Pitch] Detecting and working with Optionals from Any

2016-06-21 Thread Charlie Monroe via swift-evolution
I've recently written a CoreData editor on iOS which automatically generates UI based on the model which is described using classes such as PrimitiveProperty, etc. Since it automatically sets the value on the entity, it needs to convert the value to AnyObject in order to pass it to