[swift-evolution] Proposal: Introduce User-defined "Dynamic Member Lookup" Types

Letanyan Arumugam letanyan.a at gmail.com
Tue Dec 12 11:37:23 CST 2017



> On 12 Dec 2017, at 19:27, C. Keith Ray via swift-evolution <swift-evolution at swift.org> wrote:
> 
> in https://gist.github.com/lattner/b016e1cf86c43732c8d82f90e5ae5438 <https://gist.github.com/lattner/b016e1cf86c43732c8d82f90e5ae5438>
> 
> there is this statement:
> 
> "For this reason, the compiler only permits conformance of this protocol on the original type definition, not extensions"
> 
> and this example:
> 
> extension JSON : DynamicMemberLookupProtocol {
> 
> 
> Contradictory?
> 
I think this is just an oversight after it was changed that this protocol could not conform on extensions.

> 
> Also...
> 
> Calling Java from C is done like the following, how would one do it in Swift using this proposal?
> 
> ( Example from https://www.ibm.com/developerworks/java/tutorials/j-jni/j-jni.html <https://www.ibm.com/developerworks/java/tutorials/j-jni/j-jni.html> )
> 
> Java code:
> 
>   public class Sample2
>   {
>     public static int intMethod(int n) {
>         return n*n;
>     }
>  
>     public static boolean booleanMethod(boolean bool) {
>          return !bool;
>     }
>   }
> 
> C Code:
> 
> #include <jni.h>
> 
> #ifdef _WIN32
> #define PATH_SEPARATOR ';'
> #else
> #define PATH_SEPARATOR ':'
> #endif
> int main()
> {
>   JavaVMOption options[1];
>   JNIEnv *env;
>   JavaVM *jvm;
>   JavaVMInitArgs vm_args;
>   long status;
>   jclass cls;
>   jmethodID mid;
>   jint square;
>   jboolean not;
> 
>   options[0].optionString = "-Djava.class.path=.";
>   memset(&vm_args, 0, sizeof(vm_args));
>   vm_args.version = JNI_VERSION_1_2;
>   vm_args.nOptions = 1;
>   vm_args.options = options;
>   status = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
> 
>   if (status != JNI_ERR)
>   {
>     cls = (*env)->FindClass(env, "Sample2");
>     
>     if (cls !=0)
>     { 
>       mid = (*env)->GetStaticMethodID(env, cls, "intMethod", "(I)I");
>       
>       if (mid !=0)
>       { 
>         square = (*env)->CallStaticIntMethod(env, cls, mid, 5);
>         printf("Result of intMethod: %d\n", square);
>       }
>       
>       mid = (*env)->GetStaticMethodID(env, cls, "booleanMethod", "(Z)Z");
>       
>       if (mid !=0)
>       { 
>          not = (*env)->CallStaticBooleanMethod(env, cls, mid, 1);
>          printf("Result of booleanMethod: %d\n", not);
>       }
>    }
>    (*jvm)->DestroyJavaVM(jvm);
>    return 0;
>   }
>   else
>     return -1;
> }
> 

I think this should be up to whoever implements the Java wrapper. This proposal doesn’t specifically address the implementation of specific language interop’s. It just gives the writer a way to give their implementation a nice syntactic view in the language.

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


More information about the swift-evolution mailing list