[swift-users] Static type when expecting dynamic type

2017-02-01 Thread Howard Lovatt via swift-users
Hi All,

Anyone know what is going on here:

//: Closure picks up static type not dynamic

class MutableReference {
init() {
guard type(of: self) != MutableReference.self else {
fatalError("MutableReference is an abstract class; create a
derrivative of MutableReference")
}
}
var value: T {
get {
fatalError("Calculated property value getter should be
overridden")
}
set {
fatalError("Calculated property value setter should be
overridden")
}
}
}

class DefaultMutableReference: MutableReference {
private var _value: T
override var value: T {
get {
return _value
}
set {
_value = newValue
}
}
init(_ value: T) {
_value = value
}
}

let e: (MutableReference<[Int]>, Int) -> Void = { $0.value.append($1) }
let dmr = DefaultMutableReference([2])
dmr.value // [2]
e(dmr, 2) // fatal error: Calculated property value getter should be
overridden
dmr.value // Expect [2, 2]

If I change `e` to:
let e: (DefaultMutableReference<[Int]>, Int) -> Void = {
$0.value.append($1) }

It works fine.

IE the closure is using the static type of its first argument and not
dynamically dispatching.

Am I doing something wrong? Is there a way round where I can still use the
base class for `e`?


Thanks for any help in advance,

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


Re: [swift-users] Initializer accessing self

2017-02-01 Thread Ray Fix via swift-users

In the working version, I think the timer is first set to nil and then changed 
to the scheduled timer.  In the non-working case time can only be set exactly 
once and self access is not legal until after it is set.

Ray


> On Feb 1, 2017, at 8:06 PM, Brandon Knope via swift-users 
>  wrote:
> 
> class Test {
> let timer: Timer!
> 
> init() {
> timer = Timer.scheduledTimer(timeInterval: 20, target: self, 
> selector: #selector(test(_:)), userInfo: nil, repeats: true)
> }
> 
> @objc func test(_ timer: Timer) {
> 
> }
> }

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


[swift-users] Initializer accessing self

2017-02-01 Thread Brandon Knope via swift-users
I am at a loss:

Why does this not work:
class Test {
let timer: Timer!

init() {
timer = Timer.scheduledTimer(timeInterval: 20, target: self, selector: 
#selector(test(_:)), userInfo: nil, repeats: true)
}

@objc func test(_ timer: Timer) {

}
}

error: constant 'self.timer' used before being initialized
timer = Timer.scheduledTimer(timeInterval: 20, target: self, selector: 
#selector(test(_:)), userInfo: nil, repeats: true)

But this does:

class TestTwo {
var timer: Timer!

init() {
timer = Timer.scheduledTimer(timeInterval: 20, target: self, selector: 
#selector(test(_:)), userInfo: nil, repeats: true)
}

@objc func test(_ timer: Timer) {

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


Re: [swift-users] How to debug Segmentation Fault

2017-02-01 Thread Jim Ingham via swift-users
There is a Linux port of lldb, the swift enabled version of which is included 
in the toolchains that are built for Swift on Linux.  gdb will do fine for the 
C side of the world, but it knows nothing about Swift.  So if you need to see 
the swift side of the world to understand your crash, you'll need to use that 
lldb.  You use it in pretty much the same way as gdb, and there are tutorials 
for its use at:

http://lldb.llvm.org

Jim

> On Feb 1, 2017, at 5:26 PM, Jens Alfke via swift-users 
>  wrote:
> 
> 
>> On Feb 1, 2017, at 5:25 PM, I wrote:
>> 
>> gdb is no longer used on Mac (we have lldb instead), so further discussion 
>> of it would be off-topic here :)
> 
> Oops, never mind that … I didn’t notice this was swift-users and thought it 
> was one of the Apple mailing lists. *facepalm*
> 
> —Jens
> ___
> 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 debug Segmentation Fault

2017-02-01 Thread Jens Alfke via swift-users

> On Feb 1, 2017, at 5:00 PM, Maxim Veksler via swift-users 
>  wrote:
> 
> My method, though effective is probably barbaric. It's been years since I've 
> touched code that can actually seg fault, and I'm rusty on how you approach 
> debugging such cases, I'm wondering both Mac and Linux, even though my case 
> is Linux only.

You run the program in a debugger, probably gdb if this is Linux. Just run "gdb 
/path/to/binary”. Then at the “(gdb)” prompt enter “run”. (You can type 
command-line arguments after “run" and they’ll be passed to your program.) If 
the process segfaults or otherwise crashes, gdb takes over and prints another 
prompt. Now you can start debugging. The usual first thing I do is enter “bt” 
which will dump the stack. That itself should produce enough output to submit a 
bug report. There are lots of docs and tutorials for gdb online if you want to 
learn more.

gdb is no longer used on Mac (we have lldb instead), so further discussion of 
it would be off-topic here :)

Another possibility, instead of gdb, is getting the OS to produce a crash 
report when the process terminates. On Mac this is enabled by default, but 
maybe you need to toggle some setting on Linux to get similar behavior.

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


Re: [swift-users] How to debug Segmentation Fault

2017-02-01 Thread Jens Alfke via swift-users

> On Feb 1, 2017, at 5:25 PM, I wrote:
> 
> gdb is no longer used on Mac (we have lldb instead), so further discussion of 
> it would be off-topic here :)

Oops, never mind that … I didn’t notice this was swift-users and thought it was 
one of the Apple mailing lists. *facepalm*

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


[swift-users] How to debug Segmentation Fault

2017-02-01 Thread Maxim Veksler via swift-users
I had some code work great on Mac and seg fault on Linux, bug demo[1] and
bug report[2]. It took me some time to narrow it down using print()'s,
essentially running a binary search to identify the point at which program
election halts, leading to a seg fault.

My method, though effective is probably barbaric. It's been years since
I've touched code that can actually seg fault, and I'm rusty on how you
approach debugging such cases, I'm wondering both Mac and Linux, even
though my case is Linux only.

I'd appreciate any links and discussions of how to tackle this issue, as it
seems that the days of segmentation faulting code are big time coming back!
So let's get into shape.

Assuming the discussion turns into a valuable resource I'll work to
summarize it into some form of a blog post for the benefit of the community.

[1]
https://github.com/maximveksler/thank-you/tree/master/swift/DateFormatter
[2]  https://bugs.swift.org/browse/SR-3828
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users