Re: [swift-users] overloading methods where only difference is optional vs. non-optional type

2017-10-10 Thread Howard Lovatt via swift-users
func funny() -> Int {
 return 1
}

func funny() -> String {
 return "2"
}

let i: Int = funny()

let s: String = funny()

Is fine. Since Swift knows the return type required. However, it can’t infer 
the type of:

let q = funny()

-- Howard. 

> On 11 Oct 2017, at 4:36 am, C. Keith Ray via swift-users 
>  wrote:
> 
> You need to know that the NAME of a method or function isn't just outside the 
> parenthesis.
> 
> The name of 
> 
> func IsTextEmpty(foo : String?) -> Bool? 
> 
> is
> 
> "IsTextEmpty(foo:)"
> 
> and the name of
> 
> 
> func IsTextEmpty(text : String?) -> Bool
> 
> is
> 
> "IsTextEmpty(text:)"
> 
> The argument labels are important.
> 
> 
> func IsTextEmpty(foo : String?) -> Bool? {
> return foo?.isEmpty
> }
> 
> func IsTextEmpty(text : String?) -> Bool {
> guard let text = text else {
> return true
> }
> 
> return text.isEmpty
> }
> 
> print(IsTextEmpty(foo: "1"), IsTextEmpty(text: "2"))
> // prints Optional(false) false
> 
> 
> --
> C. Keith Ray
> Senior Software Engineer / Trainer / Agile Coach
> * http://www.thirdfoundationsw.com/keith_ray_resume_2014_long.pdf
> 
> 
> 
>> On Oct 10, 2017, at 10:27 AM, C. Keith Ray  wrote:
>> 
>> nope. it's the same as this example:
>> 
>> func funny() -> Int {
>>  return 1
>> }
>> 
>> func funny() -> String {
>>  return "2"
>> }
>> 
>> print(funny()) // the compiler doesn't know which one you want.
>> 
>> // the above doesn't compile.
>> // error: forloop.playground:8:1: error: ambiguous use of 'funny()'
>> 
>> 
>> You have to have some difference visible in the caller:
>> 
>> 
>> func funny(useInt: Bool) -> Int {
>>  return 1
>> }
>> 
>> func funny(useString: Bool) -> String {
>>  return "2"
>> }
>> 
>> print(funny(useInt: true), funny(useString: true))
>> // prints "1 2\n"
>> 
>> 
>> --
>> C. Keith Ray
>> Senior Software Engineer / Trainer / Agile Coach
>> * http://www.thirdfoundationsw.com/keith_ray_resume_2014_long.pdf
>> * https://leanpub.com/wepntk <- buy my book?
>> 
>> 
>>> On Oct 10, 2017, at 9:06 AM, Phil Kirby via swift-users 
>>>  wrote:
>>> 
>>> 2 down vote favorite
>>> Original StackOverflow post:
>>> 
>>> https://stackoverflow.com/questions/46620311/overloading-methods-where-only-difference-is-optional-vs-non-optional-type
>>> 
>>> I was under the impression that swift can have overloaded methods that 
>>> differ only in the type of object that the methods return. I would think 
>>> that I could have two funcs with the same signature yet they differ in 
>>> return type.
>>> 
>>> import Foundation
>>> 
>>> // ambiguous use of 'IsTextEmpty(text:)'
>>> func IsTextEmpty(text : String?) -> Bool? {
>>>   return text?.isEmpty
>>> }
>>> 
>>> func IsTextEmpty(text : String?) -> Bool {
>>>guard let text = text else {
>>>  return true
>>>}
>>> 
>>>return text.isEmpty
>>> }
>>> 
>>> let text: String? = nil
>>> 
>>> if let empty = IsTextEmpty(text:"text") {
>>>print("Not Empty")
>>> }
>>> 
>>> if IsTextEmpty(text: text) {
>>>print("Empty")
>>> }
>>> Here, both functions have the same input parameters but one func returns an 
>>> optional Bool? and the other returns a Bool. In this case I get an error:
>>> 
>>> ambiguous use of 'IsTextEmpty(text:)'
>>> If I change the name of one of the input parameters I no longer get the 
>>> ambiguous error:
>>> 
>>> // Works
>>> func IsTextEmpty(foo : String?) -> Bool? {
>>>   return foo?.isEmpty
>>> }
>>> 
>>> func IsTextEmpty(text : String?) -> Bool {
>>>guard let text = text else {
>>>  return true
>>>}
>>> 
>>>return text.isEmpty
>>> }
>>> 
>>> let text: String? = nil
>>> 
>>> if let empty = IsTextEmpty(foo:"text") {
>>>print("Not Empty")
>>> }
>>> 
>>> if IsTextEmpty(text: text) {
>>>print("Empty")
>>> }
>>> Shouldn't the compiler detect that they are two distinct methods even 
>>> though their return types are different, since an optional Bool? is a 
>>> different type from a non-optional Bool?
>>> 
>>> 
>>> ___
>>> swift-users mailing list
>>> swift-users@swift.org
>>> https://lists.swift.org/mailman/listinfo/swift-users
>> 
> 
> ___
> swift-users mailing list
> swift-users@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] overloading methods where only difference is optional vs. non-optional type

2017-10-10 Thread C. Keith Ray via swift-users
You need to know that the NAME of a method or function isn't just outside the 
parenthesis.

The name of 

func IsTextEmpty(foo : String?) -> Bool? 

is

"IsTextEmpty(foo:)"

and the name of


func IsTextEmpty(text : String?) -> Bool

is

"IsTextEmpty(text:)"

The argument labels are important.


func IsTextEmpty(foo : String?) -> Bool? {
return foo?.isEmpty
}

func IsTextEmpty(text : String?) -> Bool {
guard let text = text else {
return true
}

return text.isEmpty
}

print(IsTextEmpty(foo: "1"), IsTextEmpty(text: "2"))
// prints Optional(false) false


--
C. Keith Ray
Senior Software Engineer / Trainer / Agile Coach
* http://www.thirdfoundationsw.com/keith_ray_resume_2014_long.pdf



> On Oct 10, 2017, at 10:27 AM, C. Keith Ray  wrote:
> 
> nope. it's the same as this example:
> 
> func funny() -> Int {
>  return 1
> }
> 
> func funny() -> String {
>  return "2"
> }
> 
> print(funny()) // the compiler doesn't know which one you want.
> 
> // the above doesn't compile.
> // error: forloop.playground:8:1: error: ambiguous use of 'funny()'
> 
> 
> You have to have some difference visible in the caller:
> 
> 
> func funny(useInt: Bool) -> Int {
>  return 1
> }
> 
> func funny(useString: Bool) -> String {
>  return "2"
> }
> 
> print(funny(useInt: true), funny(useString: true))
> // prints "1 2\n"
> 
> 
> --
> C. Keith Ray
> Senior Software Engineer / Trainer / Agile Coach
> * http://www.thirdfoundationsw.com/keith_ray_resume_2014_long.pdf 
> 
> * https://leanpub.com/wepntk  <- buy my book?
> 
> 
>> On Oct 10, 2017, at 9:06 AM, Phil Kirby via swift-users 
>> > wrote:
>> 
>>  <>2 down vote <> favorite 
>> 
>>  
>> Original StackOverflow post:
>> 
>> https://stackoverflow.com/questions/46620311/overloading-methods-where-only-difference-is-optional-vs-non-optional-type
>>  
>> 
>> I was under the impression that swift can have overloaded methods that 
>> differ only in the type of object that the methods return. I would think 
>> that I could have two funcs with the same signature yet they differ in 
>> return type.
>> 
>> import Foundation
>> 
>> // ambiguous use of 'IsTextEmpty(text:)'
>> func IsTextEmpty(text : String?) -> Bool? {
>>   return text?.isEmpty
>> }
>> 
>> func IsTextEmpty(text : String?) -> Bool {
>>guard let text = text else {
>>  return true
>>}
>> 
>>return text.isEmpty
>> }
>> 
>> let text: String? = nil
>> 
>> if let empty = IsTextEmpty(text:"text") {
>>print("Not Empty")
>> }
>> 
>> if IsTextEmpty(text: text) {
>>print("Empty")
>> }
>> Here, both functions have the same input parameters but one func returns an 
>> optional Bool? and the other returns a Bool. In this case I get an error:
>> 
>> ambiguous use of 'IsTextEmpty(text:)'
>> If I change the name of one of the input parameters I no longer get the 
>> ambiguous error:
>> 
>> // Works
>> func IsTextEmpty(foo : String?) -> Bool? {
>>   return foo?.isEmpty
>> }
>> 
>> func IsTextEmpty(text : String?) -> Bool {
>>guard let text = text else {
>>  return true
>>}
>> 
>>return text.isEmpty
>> }
>> 
>> let text: String? = nil
>> 
>> if let empty = IsTextEmpty(foo:"text") {
>>print("Not Empty")
>> }
>> 
>> if IsTextEmpty(text: text) {
>>print("Empty")
>> }
>> Shouldn't the compiler detect that they are two distinct methods even though 
>> their return types are different, since an optional Bool? is a different 
>> type from a non-optional Bool?
>> 
>> 
>> ___
>> swift-users mailing list
>> swift-users@swift.org 
>> https://lists.swift.org/mailman/listinfo/swift-users
> 

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


Re: [swift-users] overloading methods where only difference is optional vs. non-optional type

2017-10-10 Thread C. Keith Ray via swift-users
nope. it's the same as this example:

func funny() -> Int {
 return 1
}

func funny() -> String {
 return "2"
}

print(funny()) // the compiler doesn't know which one you want.

// the above doesn't compile.
// error: forloop.playground:8:1: error: ambiguous use of 'funny()'


You have to have some difference visible in the caller:


func funny(useInt: Bool) -> Int {
 return 1
}

func funny(useString: Bool) -> String {
 return "2"
}

print(funny(useInt: true), funny(useString: true))
// prints "1 2\n"


--
C. Keith Ray
Senior Software Engineer / Trainer / Agile Coach
* http://www.thirdfoundationsw.com/keith_ray_resume_2014_long.pdf
* https://leanpub.com/wepntk  <- buy my book?


> On Oct 10, 2017, at 9:06 AM, Phil Kirby via swift-users 
>  wrote:
> 
>  <>2 down vote <> favorite 
> 
>  
> Original StackOverflow post:
> 
> https://stackoverflow.com/questions/46620311/overloading-methods-where-only-difference-is-optional-vs-non-optional-type
>  
> 
> I was under the impression that swift can have overloaded methods that differ 
> only in the type of object that the methods return. I would think that I 
> could have two funcs with the same signature yet they differ in return type.
> 
> import Foundation
> 
> // ambiguous use of 'IsTextEmpty(text:)'
> func IsTextEmpty(text : String?) -> Bool? {
>   return text?.isEmpty
> }
> 
> func IsTextEmpty(text : String?) -> Bool {
>guard let text = text else {
>  return true
>}
> 
>return text.isEmpty
> }
> 
> let text: String? = nil
> 
> if let empty = IsTextEmpty(text:"text") {
>print("Not Empty")
> }
> 
> if IsTextEmpty(text: text) {
>print("Empty")
> }
> Here, both functions have the same input parameters but one func returns an 
> optional Bool? and the other returns a Bool. In this case I get an error:
> 
> ambiguous use of 'IsTextEmpty(text:)'
> If I change the name of one of the input parameters I no longer get the 
> ambiguous error:
> 
> // Works
> func IsTextEmpty(foo : String?) -> Bool? {
>   return foo?.isEmpty
> }
> 
> func IsTextEmpty(text : String?) -> Bool {
>guard let text = text else {
>  return true
>}
> 
>return text.isEmpty
> }
> 
> let text: String? = nil
> 
> if let empty = IsTextEmpty(foo:"text") {
>print("Not Empty")
> }
> 
> if IsTextEmpty(text: text) {
>print("Empty")
> }
> Shouldn't the compiler detect that they are two distinct methods even though 
> their return types are different, since an optional Bool? is a different type 
> from a non-optional Bool?
> 
> 
> ___
> swift-users mailing list
> swift-users@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

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


[swift-users] overloading methods where only difference is optional vs. non-optional type

2017-10-10 Thread Phil Kirby via swift-users
2 down vote favorite
Original StackOverflow post:
https://stackoverflow.com/questions/46620311/overloading-methods-where-only-difference-is-optional-vs-non-optional-type
I was under the impression that swift can have overloaded methods that differ 
only in the type of object that the methods return. I would think that I could 
have two funcs with the same signature yet they differ in return type.
import Foundation

// ambiguous use of 'IsTextEmpty(text:)'
func IsTextEmpty(text : String?) -> Bool? {
return text?.isEmpty
}

func IsTextEmpty(text : String?) -> Bool {
  guard let text = text else {
return true
  }

  return text.isEmpty
}

let text: String? = nil

if let empty = IsTextEmpty(text:"text") {
  print("Not Empty")
}

if IsTextEmpty(text: text) {
  print("Empty")
}
Here, both functions have the same input parameters but one func returns an 
optional Bool? and the other returns a Bool. In this case I get an error:
ambiguous use of 'IsTextEmpty(text:)'
If I change the name of one of the input parameters I no longer get the 
ambiguous error:
// Works
func IsTextEmpty(foo : String?) -> Bool? {
return foo?.isEmpty
}

func IsTextEmpty(text : String?) -> Bool {
  guard let text = text else {
return true
  }

  return text.isEmpty
}

let text: String? = nil

if let empty = IsTextEmpty(foo:"text") {
  print("Not Empty")
}

if IsTextEmpty(text: text) {
  print("Empty")
}
Shouldn't the compiler detect that they are two distinct methods even though 
their return types are different, since an optional Bool? is a different type 
from a non-optional Bool?

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


Re: [swift-users] Why does the withUnsafeMutableBufferPointer closure take an inout parameter?

2017-10-10 Thread Martin R via swift-users
Thank you Howard for you response! However, I have a follow-up question:

Why are UnsafeMutableBufferPointer methods which modify not the buffer pointer 
itself, but only the pointed-to memory, mutating at all?

UnsafeMutable(Buffer)Pointer already have non-mutating subscript setters, which 
means that I can modify the pointed-to memory even if the buffer pointer itself 
is a constant. For example, this compiles and runs without problems:

func foo(bufptr: UnsafeMutableBufferPointer) {
let tmp = bufptr[0]
bufptr[0] = bufptr[1]
bufptr[1] = tmp
}

var a = [1, 2, 3, 4, 5]
a.withUnsafeMutableBufferPointer { foo(bufptr: $0) }


Doing the same with swapAt() does not compile:

func bar(bufptr: UnsafeMutableBufferPointer) {
bufptr.swapAt(0, 1)
// error: cannot use mutating member on immutable value: 'bufptr' is a 
'let' constant
}

which means that I have to use a variable copy:

func bar(bufptr: UnsafeMutableBufferPointer) {
var bufptr = bufptr
bufptr.swapAt(0, 1)
}

So my "feeling" is that methods (like swapAt) which modify the pointed-to 
memory of an UnsafeMutableBufferPointer should be non-mutating. 

That would then also allow (coming back to my original question) that 
withUnsafeMutableBufferPointer() passes a _constant_ buffer pointer to the 
closure, and _might_ make the check at

   
https://github.com/apple/swift/blob/master/stdlib/public/core/Arrays.swift.gyb#L1750
   
obsolete (which verifies that the closure did not modify the pointer or length).

I am probably overlooking something, so please let me know where I am wrong!

Regards, Martin

> On 9. Oct 2017, at 01:15, Howard Lovatt  wrote:
> 
> If it isn't an `inout` then it is a `let` not a `var` and hence you can't 
> call mutating methods on it. There is no 'invar' in Swift, best you can do is 
> `inout`.
> 
>   -- Howard.
> 
> On 9 October 2017 at 06:14, Martin R via swift-users  
> wrote:
> I wonder why the closure in the Array method
> 
> mutating func withUnsafeMutableBufferPointer(_ body: (inout 
> UnsafeMutableBufferPointer) throws -> R) rethrows -> R
> 
> takes an _inout_ parameter. The closure is called with a pointer to the 
> contiguous array storage, and I fail to imagine an example where it makes 
> sense to assign a new value to the parameter.
> 
> Any insights are welcome!
> 
> Regards, Martin
> ___
> swift-users mailing list
> swift-users@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
> 

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