[swift-evolution] Python Interop with Swift 4+

Brent Royal-Gordon brent at architechies.com
Mon Nov 20 05:34:10 CST 2017


> On Nov 20, 2017, at 12:32 AM, David Waite via swift-evolution <swift-evolution at swift.org> wrote:
> 
>> On Nov 20, 2017, at 1:16 AM, David Hart via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> 
> <snip>
> 
>> Moreover, Ruby allows classes to have instance variables with the same name as methods:
>> 
>> class Foo
>>   def initialize()
>>     @bar = 5
>>   end
>> 
>>   def bar()
>>     puts “Hello"
>>   end
>> end
>> 
>> In that case, how would one implement DynamicMemberLookupProtocol for the lookup of bar, and what would the return value be? Its entirely context sensitive.
> 
> I do not believe Ruby does not give you direct external access to variables. Everything with a ‘.’ is a function call.
> 
> You would use e.g.
> 
> Foo.new.instance_variable_get("@bar”) // => 5
> 
> attr :bar exposes a variable @bar through functions bar() and bar=()   (and also optimizes storage in some implementations)

This is correct…sort of. Ruby uses symbols to refer to both methods and instance variables, but instance variable symbols always start with @, so they effectively belong to a different namespace. (Similarly, symbols starting with a capital letter are for constants; symbols starting with @@ are for class variables; I believe symbols starting with $ are for global variables.) Ruby only provides syntax to access another object's methods and (for a class) constants, so in practice there's no way to access another object's instance variables except by calling a method on it, but there's no particular reason our bridge would need to follow that rule.

Leaving aside those technicalities, it's pretty clear that `foo.bar` should access a method, not an instance variable, when `foo` is a Ruby object. That doesn't mean it's the same as Python, though, because in Ruby it will need to *call* the method immediately if we're to provide natural syntax for Ruby objects bridged to Swift.

Bottom line: Ruby cannot be bridged naturally with just an undifferentiated "access member" hook. It needs separate "access property" and "access method" hooks.

-- 
Brent Royal-Gordon
Architechies

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20171120/46e4163b/attachment.html>


More information about the swift-evolution mailing list