Mirror behavior

Hello,

I had a question about the intended behavior/functionality of mirrors in Swift/Swift Foundation types.

In general, should a Mirror reference all variables in a type?
I’m not sure what scope mirrors should/will have, but it does seem like some of iterating through children has been done:

Should mirrors only reference public+ variables? Or private ones too? Is it meant to be up to the author?

With respect to Foundation, I was starting to silence some warnings with ‘as Any’ (https://bugs.swift.org/browse/SR-2737\), but I noticed this code in URLComponents.swift at https://github.com/apple/swift-corelibs-foundation/blob/master/Foundation/URLComponents.swift#L313:

if let s = self.scheme { c.append((label: "scheme", value: s)) }
if let u = self.user { c.append((label: "user", value: u)) }
etc...

Should public properties be mirrored even if their values are nil?
c.append((label: "scheme", value: s as Any))
c.append((label: "user", value: u as Any))
etc...

What rules should there be to determine what gets mirrored?

Regards,
Will Stanton

Hello,

I had a question about the intended behavior/functionality of mirrors in Swift/Swift Foundation types.

In general, should a Mirror reference all variables in a type?
I’m not sure what scope mirrors should/will have, but it does seem like some of iterating through children has been done:
How can I check if a property has been set using Swift reflection? - Stack Overflow

Should mirrors only reference public+ variables? Or private ones too? Is it meant to be up to the author?

There exists a default behavior, but also an override mechanism (CustomReflectable) in case the author of the type knows better

For instance, if you have

struct Point { var x: Double; var y: Double }

you probably do just want to see "x" and "y" reflected - and the default will do just that

But in more complex scenarios, e.g. Array<T>, you probably want to see the logical contents of the type (the array elements), not the underlying implementation details

I don't think metadata currently vends visibility, so it's unlikely we do anything to hide non-public variables in reflection.
I think other languages take a similar stance, so maybe it's just the right thing to do, but I also can't recall any explicit discussion about this

As for the Foundation-specific bits, I'll let somebody with more Foundation expertise help you out

With respect to Foundation, I was starting to silence some warnings with ‘as Any’ (https://bugs.swift.org/browse/SR-2737\), but I noticed this code in URLComponents.swift at https://github.com/apple/swift-corelibs-foundation/blob/master/Foundation/URLComponents.swift#L313:

if let s = self.scheme { c.append((label: "scheme", value: s)) }
if let u = self.user { c.append((label: "user", value: u)) }
etc...

Should public properties be mirrored even if their values are nil?
c.append((label: "scheme", value: s as Any))
c.append((label: "user", value: u as Any))
etc...

What rules should there be to determine what gets mirrored?

Regards,
Will Stanton

_______________________________________________
swift-dev mailing list
swift-dev@swift.org
https://lists.swift.org/mailman/listinfo/swift-dev

Thanks,
- Enrico
:envelope_with_arrow: egranata@.com :phone: 27683

···

On Sep 22, 2016, at 10:08 PM, Will Stanton via swift-dev <swift-dev@swift.org> wrote:

Hello Enrico,

Thank you for the reply!

If the goal of `Mirror` is reflection, wouldn’t the full structure of a type need to be exposed by its `Mirror`? And even if the goal is a more limited form of inspection, wouldn’t it still be necessary to make the default implementation, which exposes all variables, accessible?
(Is there a way to do that – to ignore `customMirror` and get a type’s ‘full’ mirror? If not, I think the exposure of CustomReflectable outside the standard library is a bit strange.)

Regards,
Will Stanton

···

On Sep 23, 2016, at 1:54 PM, Enrico Granata <egranata@apple.com> wrote:

There exists a default behavior, but also an override mechanism (CustomReflectable) in case the author of the type knows better

For instance, if you have

struct Point { var x: Double; var y: Double }

you probably do just want to see "x" and "y" reflected - and the default will do just that

But in more complex scenarios, e.g. Array<T>, you probably want to see the logical contents of the type (the array elements), not the underlying implementation details

I don't think metadata currently vends visibility, so it's unlikely we do anything to hide non-public variables in reflection.
I think other languages take a similar stance, so maybe it's just the right thing to do, but I also can't recall any explicit discussion about this

As for the Foundation-specific bits, I'll let somebody with more Foundation expertise help you out

With respect to Foundation, I was starting to silence some warnings with ‘as Any’ (https://bugs.swift.org/browse/SR-2737\), but I noticed this code in URLComponents.swift at https://github.com/apple/swift-corelibs-foundation/blob/master/Foundation/URLComponents.swift#L313:

if let s = self.scheme { c.append((label: "scheme", value: s)) }
if let u = self.user { c.append((label: "user", value: u)) }
etc...

Should public properties be mirrored even if their values are nil?
c.append((label: "scheme", value: s as Any))
c.append((label: "user", value: u as Any))
etc...

What rules should there be to determine what gets mirrored?

Regards,
Will Stanton