{"action":"created","issue":{"url":"https://api.github.com/repos/bryanjos/elixirscript/issues/161","repository_url":"https://api.github.com/repos/bryanjos/elixirscript","labels_url":"https://api.github.com/repos/bryanjos/elixirscript/issues/161/labels{/name}","comments_url":"https://api.github.com/repos/bryanjos/elixirscript/issues/161/comments","events_url":"https://api.github.com/repos/bryanjos/elixirscript/issues/161/events","html_url":"https://github.com/bryanjos/elixirscript/issues/161","id":138329797,"number":161,"title":"Protocols: Dynamically load protocol implementation","user":{"login":"bryanjos","id":1257573,"avatar_url":"https://avatars.githubusercontent.com/u/1257573?v=3","gravatar_id":"","url":"https://api.github.com/users/bryanjos","html_url":"https://github.com/bryanjos","followers_url":"https://api.github.com/users/bryanjos/followers","following_url":"https://api.github.com/users/bryanjos/following{/other_user}","gists_url":"https://api.github.com/users/bryanjos/gists{/gist_id}","starred_url":"https://api.github.com/users/bryanjos/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bryanjos/subscriptions","organizations_url":"https://api.github.com/users/bryanjos/orgs","repos_url":"https://api.github.com/users/bryanjos/repos","events_url":"https://api.github.com/users/bryanjos/events{/privacy}","received_events_url":"https://api.github.com/users/bryanjos/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/bryanjos/elixirscript/labels/enhancement","name":"enhancement","color":"84b6eb"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":7,"created_at":"2016-03-03T23:31:09Z","updated_at":"2016-03-09T23:19:17Z","closed_at":null,"body":"Connected to #155 and #160. Update protocol to dynamically load implementation of protocol when type is encountered. If module for protocol does not exist, an error should be thrown. Once an implemention is loaded, it should be cached.\r\n\r\nThere are 2 complications with this. One, ES2015 imports can only be made at the top level scope. System.js is probably the only option if we are keeping ES2015 modules. Two, this needs to be synchronous. System.js should be able to handle it using `System.get`. `System.import` won't work because it returns a promise.\r\n\r\nAlternatives to one and two are to switch to CommonJS modules. They are synchronous and can be called below the top scope. This would require some significant, but not complicated effort to change from ES2015 modules to CommonJS.\r\n\r\n\r\nTo give an example of what would happen:\r\n\r\n```javascript\r\n      return function(...args) {\r\n        let thing = args[0];\r\n        let fun = null;\r\n\r\n        let protocolType = this.constructor;\r\n        let ImplementationType = thing.constructor;\r\n\r\n        try{\r\n          let implementation = null;\r\n          //Check in cache first\r\n          if(this.cache.has(ImplementationType){\r\n             implementation = this.cache.get(ImplementationType);\r\n           }else{\r\n             implementation = System.get(protocolType + \".\" + ImplementationType);\r\n             // alternatively\r\n             // implementation = require(protocolType + \".\" + ImplementationType);\r\n           }\r\n\r\n           return implementation[funName].apply(this, args);\r\n        }catch(e){\r\n          throw new Error(\"No implementation found for \" + thing);\r\n        }\r\n      }\r\n```"},"comment":{"url":"https://api.github.com/repos/bryanjos/elixirscript/issues/comments/194561061","html_url":"https://github.com/bryanjos/elixirscript/issues/161#issuecomment-194561061","issue_url":"https://api.github.com/repos/bryanjos/elixirscript/issues/161","id":194561061,"user":{"login":"bryanjos","id":1257573,"avatar_url":"https://avatars.githubusercontent.com/u/1257573?v=3","gravatar_id":"","url":"https://api.github.com/users/bryanjos","html_url":"https://github.com/bryanjos","followers_url":"https://api.github.com/users/bryanjos/followers","following_url":"https://api.github.com/users/bryanjos/following{/other_user}","gists_url":"https://api.github.com/users/bryanjos/gists{/gist_id}","starred_url":"https://api.github.com/users/bryanjos/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bryanjos/subscriptions","organizations_url":"https://api.github.com/users/bryanjos/orgs","repos_url":"https://api.github.com/users/bryanjos/repos","events_url":"https://api.github.com/users/bryanjos/events{/privacy}","received_events_url":"https://api.github.com/users/bryanjos/received_events","type":"User","site_admin":false},"created_at":"2016-03-09T23:19:17Z","updated_at":"2016-03-09T23:19:17Z","body":"I should elaborate a bit. The *.defimpl.js file would be created with the protocol, but it would be populated in a stage afterwards that takes that, looks for the implementation files, and builds it up using that."}}