{"action":"closed","issue":{"url":"https://api.github.com/repos/chapel-lang/chapel/issues/8667","repository_url":"https://api.github.com/repos/chapel-lang/chapel","labels_url":"https://api.github.com/repos/chapel-lang/chapel/issues/8667/labels{/name}","comments_url":"https://api.github.com/repos/chapel-lang/chapel/issues/8667/comments","events_url":"https://api.github.com/repos/chapel-lang/chapel/issues/8667/events","html_url":"https://github.com/chapel-lang/chapel/issues/8667","id":301527131,"number":8667,"title":"owned & borrowed syntax proposal: default to raw pointers","user":{"login":"mppf","id":3653132,"avatar_url":"https://avatars3.githubusercontent.com/u/3653132?v=4","gravatar_id":"","url":"https://api.github.com/users/mppf","html_url":"https://github.com/mppf","followers_url":"https://api.github.com/users/mppf/followers","following_url":"https://api.github.com/users/mppf/following{/other_user}","gists_url":"https://api.github.com/users/mppf/gists{/gist_id}","starred_url":"https://api.github.com/users/mppf/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mppf/subscriptions","organizations_url":"https://api.github.com/users/mppf/orgs","repos_url":"https://api.github.com/users/mppf/repos","events_url":"https://api.github.com/users/mppf/events{/privacy}","received_events_url":"https://api.github.com/users/mppf/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":7,"created_at":"2018-03-01T19:20:43Z","updated_at":"2018-05-11T15:15:36Z","closed_at":"2018-05-11T15:15:36Z","author_association":"CONTRIBUTOR","body":"Previous: #8654. Next: #8797.\r\n\r\nGeneral idea: add syntactical convenience for Owned but it's still a type that one has to opt-in to. `:MyClass` refers to a user-managed/raw class (i.e. one that needs to be `delete`d). `new MyClass()` returns a user-managed/raw class type. A borrow from an owned MyClass returns a `borrowed MyClass` type.\r\n\r\n\r\n### Locales Primer Example\r\n``` chapel\r\nclass Node {\r\n  var data: real;\r\n  var next: owned Node;\r\n}\r\nvar head = new owned Node(0);\r\nvar current = head.borrow(); // borrow needs to be explicit here\r\n                             // otherwise we move from head into current\r\n                             // and leave head empty.\r\nfor i in 1..numLocales-1 do on Locales[i] {\r\n  current.next = new owned Node(i);\r\n  current      = current.next.borrow();\r\n}\r\n```\r\n\r\n### Generic Collection Example\r\n``` chapel\r\nrecord Collection {\r\n  var element;\r\n}\r\nproc Collection.addElement(x) {\r\n  element = x;\r\n}\r\nproc test() {\r\n  var c: Collection(owned MyClass);\r\n  c.addElement(new owned MyClass(1));\r\n  var d: Collection(int);\r\n  d.addElement(2);\r\n}\r\n```\r\n\r\n### Generic Function Example\r\n``` chapel\r\nproc process(data) {\r\n  var sum = 0;\r\n  for i in 1..n {\r\n    sum += data.value; // double-deref here, for const ref data:owned MyClass\r\n  }\r\n}\r\nproc test() {\r\n  var data = new owned MyClass(1);\r\n  process(data);\r\n}\r\n```\r\n\r\n### Borrow into Argument Record\r\n``` chapel\r\nrecord Argument {\r\n  var data;\r\n}\r\nproc process(arg:Argument) {\r\n  ... use arg.data ...\r\n}\r\nproc test() {\r\n  var data = new owned MyClass(1);\r\n  var arg:Argument(borrow MyClass) = new Argument(data.borrow());\r\n  process(arg); \r\n}\r\n```\r\n\r\n### Function returning newly allocated object\r\n``` chapel\r\nproc returnNewMyClass() : owned MyClass {\r\n  return new MyClass(1);\r\n}\r\nproc wrapReturnNewMyClass() {\r\n  return returnNewMyClass(); // return type is owned MyClass \r\n}\r\n```\r\n\r\n### Function returning a borrow\r\n``` chapel\r\nproc returnBorrowGlobal() : borrowed MyClass {\r\n  return globalMyClass.borrow();\r\n}\r\nproc wrapReturnBorrowGlobal() {\r\n  return returnBorrowGlobal(); // returns type borrowed MyClass\r\n}\r\n```\r\n\r\n### Does specifying type change new?\r\n``` chapel\r\nvar myC = new MyClass(1);\r\n// myC is a \"raw\" MyClass\r\nvar myC:MyClass = new MyClass(1);\r\n// myC is a \"raw\" MyClass\r\nvar myC = new owned MyClass(1);\r\n// myC is an owned MyClass\r\nvar myC: owned MyClass = new owned MyClass(1);\r\n// myC is an owned MyClass\r\nvar myC:MyClass = new owned MyClass(1);\r\n// type error: no implicit conversion from Owned to Raw pointer\r\n```\r\n"}}
{"actor":{"display_login":"mppf"}}