{"action":"created","issue":{"url":"https://api.github.com/repos/chapel-lang/chapel/issues/10615","repository_url":"https://api.github.com/repos/chapel-lang/chapel","labels_url":"https://api.github.com/repos/chapel-lang/chapel/issues/10615/labels{/name}","comments_url":"https://api.github.com/repos/chapel-lang/chapel/issues/10615/comments","events_url":"https://api.github.com/repos/chapel-lang/chapel/issues/10615/events","html_url":"https://github.com/chapel-lang/chapel/issues/10615","id":346385321,"node_id":"MDU6SXNzdWUzNDYzODUzMjE=","number":10615,"title":"Proposal for enabling assignment-based initialization: init=","user":{"login":"benharsh","id":1883119,"node_id":"MDQ6VXNlcjE4ODMxMTk=","avatar_url":"https://avatars0.githubusercontent.com/u/1883119?v=4","gravatar_id":"","url":"https://api.github.com/users/benharsh","html_url":"https://github.com/benharsh","followers_url":"https://api.github.com/users/benharsh/followers","following_url":"https://api.github.com/users/benharsh/following{/other_user}","gists_url":"https://api.github.com/users/benharsh/gists{/gist_id}","starred_url":"https://api.github.com/users/benharsh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/benharsh/subscriptions","organizations_url":"https://api.github.com/users/benharsh/orgs","repos_url":"https://api.github.com/users/benharsh/repos","events_url":"https://api.github.com/users/benharsh/events{/privacy}","received_events_url":"https://api.github.com/users/benharsh/received_events","type":"User","site_admin":false},"labels":[{"id":499565193,"node_id":"MDU6TGFiZWw0OTk1NjUxOTM=","url":"https://api.github.com/repos/chapel-lang/chapel/labels/area:%20Language","name":"area: Language","color":"d4c5f9","default":false},{"id":112762321,"node_id":"MDU6TGFiZWwxMTI3NjIzMjE=","url":"https://api.github.com/repos/chapel-lang/chapel/labels/type:%20Design","name":"type: Design","color":"e99695","default":false}],"state":"open","locked":false,"assignee":{"login":"benharsh","id":1883119,"node_id":"MDQ6VXNlcjE4ODMxMTk=","avatar_url":"https://avatars0.githubusercontent.com/u/1883119?v=4","gravatar_id":"","url":"https://api.github.com/users/benharsh","html_url":"https://github.com/benharsh","followers_url":"https://api.github.com/users/benharsh/followers","following_url":"https://api.github.com/users/benharsh/following{/other_user}","gists_url":"https://api.github.com/users/benharsh/gists{/gist_id}","starred_url":"https://api.github.com/users/benharsh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/benharsh/subscriptions","organizations_url":"https://api.github.com/users/benharsh/orgs","repos_url":"https://api.github.com/users/benharsh/repos","events_url":"https://api.github.com/users/benharsh/events{/privacy}","received_events_url":"https://api.github.com/users/benharsh/received_events","type":"User","site_admin":false},"assignees":[{"login":"benharsh","id":1883119,"node_id":"MDQ6VXNlcjE4ODMxMTk=","avatar_url":"https://avatars0.githubusercontent.com/u/1883119?v=4","gravatar_id":"","url":"https://api.github.com/users/benharsh","html_url":"https://github.com/benharsh","followers_url":"https://api.github.com/users/benharsh/followers","following_url":"https://api.github.com/users/benharsh/following{/other_user}","gists_url":"https://api.github.com/users/benharsh/gists{/gist_id}","starred_url":"https://api.github.com/users/benharsh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/benharsh/subscriptions","organizations_url":"https://api.github.com/users/benharsh/orgs","repos_url":"https://api.github.com/users/benharsh/repos","events_url":"https://api.github.com/users/benharsh/events{/privacy}","received_events_url":"https://api.github.com/users/benharsh/received_events","type":"User","site_admin":false}],"milestone":null,"comments":22,"created_at":"2018-07-31T22:44:00Z","updated_at":"2018-10-10T05:14:37Z","closed_at":null,"author_association":"MEMBER","body":"See #8242 for original discussion/inspiration for this proposal.\r\n\r\nWe want authors of types to permit initialization from certain types in a normal variable declaration, like:\r\n\r\n```chpl\r\nvar x : MyIntWrapper = 30;\r\n```\r\n\r\nBut we don't want to simply invoke a regular initializer in this case (e.g. new MyIntWrapper(30)). For example, a \"MyVector\" type's initializer could accept an integer to represent length, but probably doesn't want to allow initialization from a statement like:\r\n\r\n```chpl\r\nvar x : MyVector = 20;\r\n```\r\n\r\nIn this proposal a normal user initializer (\"proc init(...)\") would only be invoked with a ``new`` expression.\r\n\r\nThis proposal also would introduce a new language initialization construct: ``init=``. ``init=`` would allow for initialization with or without a ``new`` expression:\r\n```chpl\r\nrecord R {\r\n  var x : int;\r\n  proc init=(x : int, y = 1) {\r\n    this.x = x * y;\r\n  }\r\n}\r\nvar x = new R(10); // init=\r\nvar y : R = 10; // init=\r\nvar z : R = new R(10, 2); // init=\r\n```\r\n\r\nFurthermore, regular ``init`` methods would no longer be considered for copy-initialization:\r\n```chpl\r\nrecord Q {\r\n  var x : int;\r\n  proc init(x:int) {\r\n    this.x = x;\r\n  }\r\n  proc init(r:Q) {\r\n    writeln(\"init(Q)\");\r\n    this.x = r.x;\r\n  }\r\n  proc init=(r:Q) {\r\n    writeln(\"init=(Q)\");\r\n    this.x = r.x;\r\n  }\r\n}\r\n\r\nvar A = new Q(10);\r\nvar B = A; // prints \"init=(Q)\"\r\n```\r\n\r\nThis means that the compiler would no longer emit a warning for completely-generic ``init`` methods. If the user did not write a ``init=`` method, the compiler would generate one for them (like current copy-initializers).\r\n\r\nThe compiler will will consider ``init`` to be 'more specific' than ``init=`` for the following statements:\r\n\r\n```chpl\r\n// using record 'Q' from previous example\r\nvar A : Q; // declaration without initialization expression\r\nnew Q(10); // new-expr\r\nnew Q(someOtherQ); // NB: possibly confusing, may seem like a copy-constructor call\r\nthis.init(...); // sibling initializer call\r\n```\r\n\r\nWe think this proposal would help to eliminate special-case code for wrapper types like 'bigint', and could also enable elegant initialization of atomic variables:\r\n```chpl\r\nvar x : atomic int = 5; // x.write(5);\r\n```\r\n\r\n### Open Questions\r\n\r\n1) Should we allow a completely generic formal for ``init=`` ?"},"comment":{"url":"https://api.github.com/repos/chapel-lang/chapel/issues/comments/428440520","html_url":"https://github.com/chapel-lang/chapel/issues/10615#issuecomment-428440520","issue_url":"https://api.github.com/repos/chapel-lang/chapel/issues/10615","id":428440520,"node_id":"MDEyOklzc3VlQ29tbWVudDQyODQ0MDUyMA==","user":{"login":"bradcray","id":7536222,"node_id":"MDQ6VXNlcjc1MzYyMjI=","avatar_url":"https://avatars0.githubusercontent.com/u/7536222?v=4","gravatar_id":"","url":"https://api.github.com/users/bradcray","html_url":"https://github.com/bradcray","followers_url":"https://api.github.com/users/bradcray/followers","following_url":"https://api.github.com/users/bradcray/following{/other_user}","gists_url":"https://api.github.com/users/bradcray/gists{/gist_id}","starred_url":"https://api.github.com/users/bradcray/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bradcray/subscriptions","organizations_url":"https://api.github.com/users/bradcray/orgs","repos_url":"https://api.github.com/users/bradcray/repos","events_url":"https://api.github.com/users/bradcray/events{/privacy}","received_events_url":"https://api.github.com/users/bradcray/received_events","type":"User","site_admin":false},"created_at":"2018-10-10T05:14:37Z","updated_at":"2018-10-10T05:14:37Z","author_association":"MEMBER","body":"> Specifically, I think of such coercions as being most useful for initializations and argument passing for copy-in contexts:\r\n> \r\n> ```chapel\r\n> record MyInt { ... }\r\n> proc +(x: MyInt, y: MyInt): MyInt { ... }\r\n> \r\n> var i: MyInt = 42;  // would like to have \"42\" become a `MyInt`\r\n> writeln(i + 2);        // would like to have \"2\" become a `MyInt` in order to call +(MyInt, MyInt)\r\n> ```\r\n> \r\n> With `init=`, we'd no longer need user-defined coercions for the first case.  What I was realizing is that maybe it would suffice for the second case as well, particularly given recent discussions [like this one](https://github.com/chapel-lang/chapel/issues/10215#issuecomment-406713133) relating the `in` intent to variable initialization.\r\n\r\nIn today's deep-dive, it was pointed out that there was a flaw in my reasoning for the `i + 2` case above, which is the following:  Since the default argument intent for records is `const ref`, the `+()` operator I wrote above on the `MyInt` type wouldn't cause the `init=` overload to fire by my \"`in` intents are essentially equivalent to variable initialization\" argument.  If I were to define `proc +(in x: MyInt, y: MyInt) { ... }` then my argument would be more likely to fire, but then I'd end up invoking copy initializers for all arguments, which is probably not what I'd like.\r\n\r\nIn spite of that, I continue to think that treating `in` and initialization as symmetrically as possible is desirable and continue to think that `init=` is probably a better road to user-defined coercions than what we'd been thinking about before.\r\n\r\nBTW @mppf [this was the comment](https://github.com/chapel-lang/chapel/issues/10615#issuecomment-417106253) I was referring to today, and am happy to remember that you did in fact see it even though you didn't remember that today.  :)"}}
{"actor":{"display_login":"bradcray"}}