Re: [swift-users] Detect if a generic type is numeric

2017-10-01 Thread Glenn L. Austin via swift-users

> On Oct 1, 2017, at 8:56 AM, Dave Reed via swift-users  
> wrote:
> 
> 
>> On Sep 21, 2017, at 3:58 PM, V T via swift-users  
>> wrote:
>> 
>> Hi there!
>> 
>> Is there a best way to check if a given type conforms to numeric protocol 
>> (Integer or FP) at runtime?
>> 
>> func checkNumeric(_ value: T) {
>>  /* return true if vaiue is Integer or FP */
>>  /* this will not compile: */
>>  if value is Numeric {
>>  
>>  }
>> }
>> 
>> Best regards!
>> 
>> VT
>> 
> 
> I think the way to do it is to try casting as the type, but you can't use 
> "as? Numeric" as you get:
> 
> error: protocol 'Numeric' can only be used as a generic constraint because it 
> has Self or associated type requirements
> 
> but you could check for each specific numeric type such as:
> 
> func checkNumeric(_ value: T) {
>if (value as? Int != nil) || (value as? Float != nil) {
>print("numeric")
>} else {
>print("not numeric")
>}
> }
> 
> checkNumeric(3)
> checkNumeric(3.0)
> checkNumeric("3")

You can also use the 'is' operator, as in 'value is Int || value is Float || 
value is Double'

-- 
Glenn L. Austin, Computer Wizard, AustinSoft.com

___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] Bls: Bls: module (de)-initialization

2016-12-22 Thread Glenn L. Austin via swift-users
> On Dec 21, 2016, at 7:29 PM, Mr Bee via swift-users  
> wrote:
> 
> Sorry for not being clear. My english isn't very good either.
> 
> What I meant was static module linking (loading and unloading) for Swift 
> modules. Also with initialization and deinitialization for Swift modules.
> 
> It's alright then. I just want to make sure. I'm a Pascal/Delphi programmer 
> and sometimes I rely on such features in my applications. As a new and 
> considered as modern programming language, I thought Swift would provide more 
> advance features than Pascal/Delphi does.
> 
> Thank you.
>  
> 
> –Mr Bee

Many of us also depend upon dynamic linking, but the runtime for Swift (and 
Objective-C) is far more complex than just loading new code.

When loading code, there are things that "just happen" -- such as class 
extensions -- that need to be unlinked when the dynamic library is unloaded. 
For one thing if you had objects created while the class extension was 
available, those objects would have to be "re-written" to not use the class 
extension any more. If the extension allocated additional memory, that memory 
would be orphaned.

Dynamic languages are far more complex than static languages like Pascal and 
Delphi...

-- 
Glenn L. Austin, Computer Wizard and Race Car Driver <><


___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users