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

Will Stanton willstanton1 at yahoo.com
Sun Sep 25 00:22:43 CDT 2016


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 at 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



More information about the swift-corelibs-dev mailing list