> On Oct 29, 2017, at 4:04 AM, Lukas Stabe <lu...@stabe.de> wrote:
> 
>> On 28. Oct 2017, at 23:10, Chris Lattner via swift-evolution 
>> <swift-evolution@swift.org> wrote:
>> 
>> … which is to say, exactly identical to the Python version except that new 
>> variables need to be declared with let/var.  This can be done by blessing 
>> Python.Object (which is identical to “PyObject*” at the machine level) with 
>> some special dynamic name lookup behavior:  Dot syntax turns into a call to 
>> PyObject_GetAttrString, subscripts turn into PyObject_GetItem, calls turn 
>> into PyObject_Call, etc.  ARC would be implemented with INCREF etc.
> 
> That sounds like a very interesting prospect. Do you think it would make 
> sense to make the language features that facilitate this (dynamic dispatch of 
> method calls, property accesses, subscript and ARC) available to Swift 
> classes annotated in some way, so that interop like this can be implemented 
> as a library without special treatment by the Swift compiler? This could also 
> enable more dynamic DSL like features.

I haven’t explored enough of the design space to be sure, but I’d want to make 
sure that a library version of this could be done without giving up ergonomics 
of the result.  If you were interested in being able to interop with other 
languages that are dynamically typed and reference counted, then something like 
this could be possible in principle:

protocol DynamicDispatchable { // Protocol is “magic" known by the compiler.
  func retain()
  func release()
  func memberLookup(_ : String) -> Self
  func subscript<T>(_ : T) -> Self
  func call(_ args: [Self]) -> Self
} 

module Python {
  struct Object : DynamicDispatchable {
    var state : UnsafePointer<PyObject>

    func retain() {
       INCREF(self)
   }

     func memberLookup(_ : String) -> Object {
        PyObject_GetAttrString(…)
     }
    etc
  }

module Perl5 { 
   struct Object : DynamicDispatchable {
    var state : UnsafePointer<SV>

    func retain() {
       SvREFCNT_inc(self)
   }
….



Are there other uses for such a thing?

-Chris


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

Reply via email to