Re: [swift-users] The case of broken polymorphism or "Cannot convert value of type to expected argument type"?

2017-02-16 Thread Slava Pestov via swift-users
Hi Isaac,

This is not about associated types. Rather, the issue is that a ‘Thing’ is a 
‘Something’, but you are casting it to 
‘Something’. The two types are not related; in general, if A 
is a subtype of B, then G is not a subtype of G.

https://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science)

Slava

> On Feb 16, 2017, at 9:05 AM, Isaac Rivera via swift-users 
>  wrote:
> 
> Hello, list!
> 
> I am trying to find my way around Swift’s protocol limitations. It appears 
> that in general, any protocol with declared associatedtype will break 
> polymorphism?
> 
> Take the case below which does not compile. All "Thing” instances are 
> "Something” but they can’t be passed around or coerced 
> as so.
> 
> How is it that I can legally write the code:
> 
> class Thing: Something { }
> 
> and instantiate it, but it is not the very thing it implements? 
> 
> All Thing instances conform to the public interfaces of 
> Something so why can’t they be recognized as such and 
> coerced as such?
> 
> What is the work-around of this break in Polymorphism?
> 
> import UIKit
> 
> protocol Anything: class, NSObjectProtocol {
>   
>   associatedtype ViewControllerType: UIViewController
>   
>   var viewController: ViewControllerType { get }
>   
>   init(viewController: ViewControllerType)
>   
>   func addAnything(anything: Something) -> Bool
> }
> 
> class Something: NSObject, Anything {
>   
>   typealias ViewControllerType = VC
>   
>   private(set) var viewController: ViewControllerType
>   
>   required init(viewController: ViewControllerType) { self.viewController 
> = viewController }
>   
>   final private var things = [String: Something]()
>   
>   final internal func addAnything(anything: Something) 
> -> Bool {
>   // implementation details...
>   return true
>   }
> }
> 
> class Thing: Something { }
> 
> let firstThing = Thing(viewController: UINavigationController())
> let secondThing = Thing(viewController: UINavigationController())
> 
> firstThing.addAnything(anything: secondThing)
> 
> // Playground execution failed: error: MyPlayground.playground:48:34: error: 
> cannot convert value of type 'Thing' to expected argument type 
> 'Something'
> 
> firstThing.addAnything(anything: secondThing as Something)
> 
> // Playground execution failed: error: MyPlayground.playground:48:34: error: 
> cannot convert value of type 'Thing' to type 'Something' in 
> coercion
> 
> 
> 
> 
> ___
> 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] ambiguous minus operator

2017-02-16 Thread Zhao Xin via swift-users
I don't know if you are using an IDE, but in Xcode. I can just cmd+mouse
left click to see the code headers.

Zhaoxin

On Fri, Feb 17, 2017 at 2:22 AM, Jon Shier via swift-users <
swift-users@swift.org> wrote:

> I’ve had good luck with being able to click the candidates in
> cases like this so Xcode will navigate to those bits of code. However, in
> this case I think the issue is that the candidates weren’t from user code
> but from the standard library, which Xcode has all sorts of issues
> navigating within, seemingly at random.
> As an aside, it would be really cool if, instead of seeing the
> standard library as a compiled module, Xcode could navigate to the relevant
> source code (perhaps optionally). Otherwise I’m stuck either looking at
> documentation (Apple’s docs or swiftdocs) and if that doesn’t help, trying
> to navigate to the relevant code in GitHub. This is especially true for
> things like finding out what the default values defined for functions are,
> as the documentation just shows default, which is useless.
>
>
> Jon
>
> > On Feb 16, 2017, at 1:16 PM, Jordan Rose via swift-users <
> swift-users@swift.org> wrote:
> >
> >> As usual the candidates are unknown.
> >
> > Just on this particular note, this is a terrible presentation issue and
> totally something we Apple people need to fix in Xcode, but you can usually
> see what the candidates were in the build log.
> >
> > Jordan
> >
> >
> >> On Feb 16, 2017, at 07:56, J.E. Schotsman via swift-users <
> swift-users@swift.org> wrote:
> >>
> >> Hello,
> >>
> >> I am trying to define an operator that subtracts dispatch times:
> >>
> >> #import Dispatch
> >>
> >> func -( time1: DispatchTime, time2: DispatchTime ) ->
> DispatchTimeInterval
> >>  {
> >>  return DispatchTimeInterval.nanoseconds( time2.uptimeNanoseconds
> - time1.uptimeNanoseconds )
> >>  }
> >>
> >> Compiler says: Ambiguous use of operator ‘-'
> >> Found this candidate
> >> Found this candidate
> >>
> >> As usual the candidates are unknown.
> >>
> >> What am I doing wrong?
> >>
> >> Jan E.
> >> ___
> >> 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
>
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


[swift-users] Problems installing Swift on Ubuntu

2017-02-16 Thread Mohit Athwani via swift-users
I am a noob too and I got Swift to compile on Ubuntu 16.04 LTS using the
following command from the swift/utils directory:

./build-script --preset=buildbot_linux_1604 install_destdir=/tmp/install
installable_package=/tmp/swift.tar.gz

And then running swift with:
/tmp/install/bin/swift my_code.swift

Hope this helps!

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


Re: [swift-users] I have a problems to install Swift on Ubuntu 16.04

2017-02-16 Thread Mishal Shah via swift-users
Hi Francesco, 

Which toolchain are you using? Is it the development or swift 3.1 snapshot and 
which build did you download swift.org?

Thanks, 
Mishal Shah
> On Feb 15, 2017, at 10:42 AM, Francesco via swift-users 
>  wrote:
> 
> I followed all the procedures indicated on the Swift site to install Swift on 
> Ubuntu 4.16, but when I enter the terminal command "swift" gives me this 
> error: can not execute binary file: executable format can invalid, help me 
> solve the problem, thank you. ;-)
> ___
> 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] ambiguous minus operator

2017-02-16 Thread Jon Shier via swift-users
I’ve had good luck with being able to click the candidates in cases 
like this so Xcode will navigate to those bits of code. However, in this case I 
think the issue is that the candidates weren’t from user code but from the 
standard library, which Xcode has all sorts of issues navigating within, 
seemingly at random.
As an aside, it would be really cool if, instead of seeing the standard 
library as a compiled module, Xcode could navigate to the relevant source code 
(perhaps optionally). Otherwise I’m stuck either looking at documentation 
(Apple’s docs or swiftdocs) and if that doesn’t help, trying to navigate to the 
relevant code in GitHub. This is especially true for things like finding out 
what the default values defined for functions are, as the documentation just 
shows default, which is useless.


Jon

> On Feb 16, 2017, at 1:16 PM, Jordan Rose via swift-users 
>  wrote:
> 
>> As usual the candidates are unknown.
> 
> Just on this particular note, this is a terrible presentation issue and 
> totally something we Apple people need to fix in Xcode, but you can usually 
> see what the candidates were in the build log.
> 
> Jordan
> 
> 
>> On Feb 16, 2017, at 07:56, J.E. Schotsman via swift-users 
>>  wrote:
>> 
>> Hello,
>> 
>> I am trying to define an operator that subtracts dispatch times:
>> 
>> #import Dispatch
>> 
>> func -( time1: DispatchTime, time2: DispatchTime ) -> DispatchTimeInterval
>>  {
>>  return DispatchTimeInterval.nanoseconds( time2.uptimeNanoseconds - 
>> time1.uptimeNanoseconds )
>>  }
>> 
>> Compiler says: Ambiguous use of operator ‘-'
>> Found this candidate
>> Found this candidate
>> 
>> As usual the candidates are unknown.
>> 
>> What am I doing wrong?
>> 
>> Jan E.
>> ___
>> 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] ambiguous minus operator

2017-02-16 Thread Jordan Rose via swift-users
> As usual the candidates are unknown.

Just on this particular note, this is a terrible presentation issue and totally 
something we Apple people need to fix in Xcode, but you can usually see what 
the candidates were in the build log.

Jordan


> On Feb 16, 2017, at 07:56, J.E. Schotsman via swift-users 
>  wrote:
> 
> Hello,
> 
> I am trying to define an operator that subtracts dispatch times:
> 
> #import Dispatch
> 
> func -( time1: DispatchTime, time2: DispatchTime ) -> DispatchTimeInterval
>   {
>   return DispatchTimeInterval.nanoseconds( time2.uptimeNanoseconds - 
> time1.uptimeNanoseconds )
>   }
> 
> Compiler says: Ambiguous use of operator ‘-'
> Found this candidate
> Found this candidate
> 
> As usual the candidates are unknown.
> 
> What am I doing wrong?
> 
> Jan E.
> ___
> 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] How to return value in void * C pointer ?

2017-02-16 Thread Quinn "The Eskimo!" via swift-users

On 16 Feb 2017, at 15:59, Biala via swift-users  wrote:

> I have to implement a callback method that is called by the API. As a 
> variable I get a class with void * member. I have to allocate some object in 
> memory and assign this void* member to point to it.

I don’t really understand this question, alas.  Can you perhaps post some 
snippets of code that show the specific problem you’re trying to solve?

Share and Enjoy
--
Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware


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


Re: [swift-users] ambiguous minus operator

2017-02-16 Thread Saagar Jha via swift-users
DispatchTimeInterval.nanoseconds expects an Int, while uptimeNanoseconds is an 
UInt64 (an thus so is their difference). You can “fix” this by adding an 
explicit cast:

func -(time1: DispatchTime, time2: DispatchTime) -> DispatchTimeInterval {
return DispatchTimeInterval.nanoseconds(Int(time2.uptimeNanoseconds - 
time1.uptimeNanoseconds))
}

However, you will need to perform checking for when time2 < time1 and then the 
difference doesn’t fit in an Int–these will cause crashes.

Saagar Jha

> On Feb 16, 2017, at 7:56 AM, J.E. Schotsman via swift-users 
>  wrote:
> 
> Hello,
> 
> I am trying to define an operator that subtracts dispatch times:
> 
> #import Dispatch
> 
> func -( time1: DispatchTime, time2: DispatchTime ) -> DispatchTimeInterval
>   {
>   return DispatchTimeInterval.nanoseconds( time2.uptimeNanoseconds - 
> time1.uptimeNanoseconds )
>   }
> 
> Compiler says: Ambiguous use of operator ‘-'
> Found this candidate
> Found this candidate
> 
> As usual the candidates are unknown.
> 
> What am I doing wrong?
> 
> Jan E.
> ___
> 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] How to return value in void * C pointer ?

2017-02-16 Thread Biala via swift-users
Hello,
Using the Apple core audio I come to this problem: I have to implement a 
callback method that is called by the API. As a variable I get a class with 
void * member. I have to allocate some object in memory and assign this void* 
member to point to it.How can I do this in SWIFT?

Regards,Alex___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


[swift-users] Searching thru many threads; Linux dev

2017-02-16 Thread Félix Fischer via swift-users
Hi!

Sorry for my noobness. I have two questions that may or may not be very apt.

1) How do I search through the threads of the mailing list? I'm looking for
answers to Q2
2) What kind of environment can I set up for programming for Swift on
Linux? I have a slow, old pc, so I can't quite afford the resources that
Atom uses.
I'm mainly looking for an editor that supports Swift Linting.

Thanks for your time,
Félix
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


[swift-users] I have a problems to install Swift on Ubuntu 16.04

2017-02-16 Thread Francesco via swift-users
I followed all the procedures indicated on the Swift site to install 
Swift on Ubuntu 4.16, but when I enter the terminal command "swift" 
gives me this error: can not execute binary file: executable format can 
invalid, help me solve the problem, thank you. ;-)
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


[swift-users] The case of broken polymorphism or "Cannot convert value of type to expected argument type"?

2017-02-16 Thread Isaac Rivera via swift-users
Hello, list!

I am trying to find my way around Swift’s protocol limitations. It appears that 
in general, any protocol with declared associatedtype will break polymorphism?

Take the case below which does not compile. All "Thing” instances are 
"Something” but they can’t be passed around or coerced as 
so.

How is it that I can legally write the code:

class Thing: Something { }

and instantiate it, but it is not the very thing it implements? 

All Thing instances conform to the public interfaces of 
Something so why can’t they be recognized as such and coerced 
as such?

What is the work-around of this break in Polymorphism?

import UIKit

protocol Anything: class, NSObjectProtocol {

associatedtype ViewControllerType: UIViewController

var viewController: ViewControllerType { get }

init(viewController: ViewControllerType)

func addAnything(anything: Something) -> Bool
}

class Something: NSObject, Anything {

typealias ViewControllerType = VC

private(set) var viewController: ViewControllerType

required init(viewController: ViewControllerType) { self.viewController 
= viewController }

final private var things = [String: Something]()

final internal func addAnything(anything: Something) 
-> Bool {
// implementation details...
return true
}
}

class Thing: Something { }

let firstThing = Thing(viewController: UINavigationController())
let secondThing = Thing(viewController: UINavigationController())

firstThing.addAnything(anything: secondThing)

// Playground execution failed: error: MyPlayground.playground:48:34: error: 
cannot convert value of type 'Thing' to expected argument type 
'Something'

firstThing.addAnything(anything: secondThing as Something)

// Playground execution failed: error: MyPlayground.playground:48:34: error: 
cannot convert value of type 'Thing' to type 'Something' in 
coercion




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


Re: [swift-users] ambiguous minus operator

2017-02-16 Thread Rien via swift-users
I don’t know about the candidates

But the uptimeNanoseconds is a UInt64 and as such you should probably use 
substractWithOverflow.

var a: UInt64 = 12
var b: UInt64 = 25

let result = UInt64.subtractWithOverflow(a, b)

if result.overflow {
print("Overflow")
} else {
let answer = result.0
}

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: http://github.com/Balancingrock
Project: http://swiftfire.nl





> On 16 Feb 2017, at 16:56, J.E. Schotsman via swift-users 
>  wrote:
> 
> Hello,
> 
> I am trying to define an operator that subtracts dispatch times:
> 
> #import Dispatch
> 
> func -( time1: DispatchTime, time2: DispatchTime ) -> DispatchTimeInterval
>   {
>   return DispatchTimeInterval.nanoseconds( time2.uptimeNanoseconds - 
> time1.uptimeNanoseconds )
>   }
> 
> Compiler says: Ambiguous use of operator ‘-'
> Found this candidate
> Found this candidate
> 
> As usual the candidates are unknown.
> 
> What am I doing wrong?
> 
> Jan E.
> ___
> 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] ambiguous minus operator

2017-02-16 Thread J.E. Schotsman via swift-users
Hello,

I am trying to define an operator that subtracts dispatch times:

#import Dispatch

func -( time1: DispatchTime, time2: DispatchTime ) -> DispatchTimeInterval
{
return DispatchTimeInterval.nanoseconds( time2.uptimeNanoseconds - 
time1.uptimeNanoseconds )
}

Compiler says: Ambiguous use of operator ‘-'
Found this candidate
Found this candidate

As usual the candidates are unknown.

What am I doing wrong?

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


Re: [swift-users] UnsafePointer to UInt64

2017-02-16 Thread Rien via swift-users
Excellent!

I had not seen the bitPattern initializer before…

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: http://github.com/Balancingrock
Project: http://swiftfire.nl





> On 16 Feb 2017, at 14:47, Martin R  wrote:
> 
> // Int to pointer:
> let ptr = UnsafePointer(bitPattern: 123)!
> print(ptr) // 0x007b
> 
> // Pointer to Int:
> let int = Int(bitPattern: ptr)
> print(int) // 123
> 
> Int has the same size as a pointer on both 32-bit and 64-bit platforms.
> 
> Regards, Martin
> 
>> On 16 Feb 2017, at 14:27, Rien via swift-users  wrote:
>> 
>> What would be the easiest (and fastest) way to convert a pointer to an 
>> (unsigned)integer?
>> 
>> Ptr -> String -> Int
>> 
>> seems a bit cumbersome to me :-)
>> 
>> Regards,
>> Rien
>> 
>> Site: http://balancingrock.nl
>> Blog: http://swiftrien.blogspot.com
>> Github: http://github.com/Balancingrock
>> Project: http://swiftfire.nl
>> 
>> 
>> 
>> 
>> 
>> ___
>> 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] UnsafePointer to UInt64

2017-02-16 Thread Martin R via swift-users
// Int to pointer:
let ptr = UnsafePointer(bitPattern: 123)!
print(ptr) // 0x007b

// Pointer to Int:
let int = Int(bitPattern: ptr)
print(int) // 123

Int has the same size as a pointer on both 32-bit and 64-bit platforms.

Regards, Martin

> On 16 Feb 2017, at 14:27, Rien via swift-users  wrote:
> 
> What would be the easiest (and fastest) way to convert a pointer to an 
> (unsigned)integer?
> 
> Ptr -> String -> Int
> 
> seems a bit cumbersome to me :-)
> 
> Regards,
> Rien
> 
> Site: http://balancingrock.nl
> Blog: http://swiftrien.blogspot.com
> Github: http://github.com/Balancingrock
> Project: http://swiftfire.nl
> 
> 
> 
> 
> 
> ___
> 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] UnsafePointer to UInt64

2017-02-16 Thread Rien via swift-users
What would be the easiest (and fastest) way to convert a pointer to an 
(unsigned)integer?

Ptr -> String -> Int

seems a bit cumbersome to me :-)

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: http://github.com/Balancingrock
Project: http://swiftfire.nl





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