[swift-users] Customizing my custom type's appearance in the debugger
Kate Stone
k8stone at apple.com
Mon Jun 27 12:41:50 CDT 2016
> On Jun 26, 2016, at 10:56 PM, Jim Ingham via swift-users <swift-users at swift.org> wrote:
>
> The display in the variables view uses synthetic child providers:
>
> http://lldb.llvm.org/varformats.html <http://lldb.llvm.org/varformats.html>
>
> We could hook up the Mirrors to the synthetic child providers if there is no formatter registered, but Mirrors always run code and we try not to run code for the variables view if we can avoid it as it slows down debugging more than somewhat.
I'd add that running code when trying to display variables can have much more serious effects than just slowing debugging. It allows for arbitrary side effects, which aren't always desirable, and depends on the code execution, which isn't always possible in situations where the target process has deadlocked or crashed.
Writing custom formatters that run in LLDB"s process as described by Jim above is indeed the way to change the default display in the Variables View.
>> On Jun 26, 2016, at 5:03 PM, Tim Vermeulen via swift-users <swift-users at swift.org <mailto:swift-users at swift.org>> wrote:
>>
>> I already did that, sorry for not providing any code. Take this as an example:
>>
>> public struct Wrapper<Element> {
>>
>> private var elements: [Element]
>>
>> public init<S: Sequence where S.Iterator.Element == Element>(_ sequence: S) {
>> elements = [Element](sequence)
>> }
>>
>> }
>>
>> extension Wrapper: Collection {
>>
>> public var startIndex: Int { return elements.startIndex }
>> public var endIndex: Int { return elements.endIndex }
>>
>> public func index(after index: Int) -> Int {
>> return index + 1
>> }
>>
>> public subscript(position: Int) -> Element {
>> return elements[position]
>> }
>>
>> }
>>
>> extension Wrapper: CustomReflectable {
>>
>> public var customMirror: Mirror {
>> return Mirror(self, unlabeledChildren: self, displayStyle: .collection)
>> }
>>
>> }
>>
>> If I debug an instance of this Wrapper type, then Xcode’s Variables View will show
>>
>> ▿ wrapper
>> ▿ elements = x values
>> [0] = 0
>> [1] = …
>>
>> But the `elements` property is an implementation detail. What I would really want to see is this:
>>
>> ▿ wrapper = x values
>> [0] = 0
>> [1] = …
>>
>> But I’m not sure if this is even possible. That’s basically why I’m asking this. Hopefully it’s clearer now :)
>>
>> One last thing to note, the code `dump(wrapper)` will print
>>
>> ▿ 3 elements
>> - 1
>> - 2
>> - 3
>>
>> to the console, which is good. If I don’t implement CustomReflectable, then `dump(wrapper)` will show this:
>>
>> ▿ Wrapper<Swift.Int>
>> ▿ elements: 3 elements
>> - 1
>> - 2
>> - 3
>>
>> So my CustomReflectable conformance is definitely doing something, but I would like to see the results in the variables view as well.
>>
>>> On 27 Jun 2016, at 01:40, Dmitri Gribenko <gribozavr at gmail.com <mailto:gribozavr at gmail.com>> wrote:
>>>
>>> On Fri, Jun 24, 2016 at 3:53 PM, Tim Vermeulen via swift-users
>>> <swift-users at swift.org <mailto:swift-users at swift.org>> wrote:
>>>> I’ve implemented a linked list. Now I’d like to be able to view the elements of a linked list in the debugger just like with an array. In the debugger, an array is represented like this:
>>>>
>>>> [0] = the first element
>>>> [1] = the second element
>>>> etc
>>>>
>>>> I wonder if I can do the same for my linked list. I already implemented CustomReflectable, so the code `dump(myLinkedList)` shows this in the console:
>>>>
>>>> 3 elements
>>>> - first element
>>>> - second elements
>>>> - third element
>>>>
>>>> I thought this would also change the appearance of my linked list in the debugger, but unfortunately it’s unchanged. Is there a way to do what I’m trying to do?
>>>
>>> Try setting "displayStyle: .collection" when you call the Mirror initializer.
>>>
>>> Dmitri
>>>
>>> --
>>> main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
>>> (j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com <mailto:gribozavr at gmail.com>>*/
>>
>> _______________________________________________
>> swift-users mailing list
>> swift-users at swift.org <mailto:swift-users at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-users
>
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160627/13c58f10/attachment.html>
More information about the swift-users
mailing list