[swift-corelibs-dev] [swift-dev] Mirror behavior

Enrico Granata egranata at apple.com
Fri Sep 23 12:54:22 CDT 2016

> On Sep 22, 2016, at 10:08 PM, Will Stanton via swift-dev <swift-dev at swift.org> wrote:
> 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:
> http://stackoverflow.com/questions/34601802/how-can-i-check-if-a-property-has-been-set-using-swift-reflection
> 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 at swift.org
> https://lists.swift.org/mailman/listinfo/swift-dev

- Enrico
📩 egranata@.com ☎️ 27683

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-corelibs-dev/attachments/20160923/faffbc61/attachment.html>

More information about the swift-corelibs-dev mailing list