{"action":"created","issue":{"url":"https://api.github.com/repos/wch/R6/issues/78","labels_url":"https://api.github.com/repos/wch/R6/issues/78/labels{/name}","comments_url":"https://api.github.com/repos/wch/R6/issues/78/comments","events_url":"https://api.github.com/repos/wch/R6/issues/78/events","html_url":"https://github.com/wch/R6/issues/78","id":118119541,"number":78,"title":"setters with pass by reference semantics","user":{"login":"nverno","id":4791025,"avatar_url":"https://avatars.githubusercontent.com/u/4791025?v=3","gravatar_id":"","url":"https://api.github.com/users/nverno","html_url":"https://github.com/nverno","followers_url":"https://api.github.com/users/nverno/followers","following_url":"https://api.github.com/users/nverno/following{/other_user}","gists_url":"https://api.github.com/users/nverno/gists{/gist_id}","starred_url":"https://api.github.com/users/nverno/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nverno/subscriptions","organizations_url":"https://api.github.com/users/nverno/orgs","repos_url":"https://api.github.com/users/nverno/repos","events_url":"https://api.github.com/users/nverno/events{/privacy}","received_events_url":"https://api.github.com/users/nverno/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-11-20T20:24:53Z","updated_at":"2015-11-21T00:09:41Z","closed_at":null,"body":"Would it make sense to have setter functions change fields by reference?  Something like the following example,\r\n```\r\n## Make a setter function to modify a field without copying\r\nlibrary(inline)\r\nsetter <- cfunction(signature(field=\"list\", index=\"integer\",\r\n                              value=\"character\"),\r\n                    body='SET_VECTOR_ELT(field, INTEGER(index)[0]-1, value); return R_NilValue;')\r\n\r\nlibrary(R6)\r\nExample <- R6Class(\r\n  \"Example\",\r\n  public = list(\r\n    example = NA,\r\n    initialize = function(length) {\r\n      if (!missing(length)) self$example <- vector(\"list\", length)\r\n    },\r\n    set_example = function(index, value) {\r\n      setter(self$example, as.integer(index), as.character(value))\r\n      invisible()\r\n    }\r\n  )\r\n)\r\n\r\nexample <- Example$new(length=10)\r\ntracemem(example$example)\r\nexample$set_example(1, \"B\")\r\nexample$example[[1]]\r\n```\r\n"},"comment":{"url":"https://api.github.com/repos/wch/R6/issues/comments/158563960","html_url":"https://github.com/wch/R6/issues/78#issuecomment-158563960","issue_url":"https://api.github.com/repos/wch/R6/issues/78","id":158563960,"user":{"login":"wch","id":86978,"avatar_url":"https://avatars.githubusercontent.com/u/86978?v=3","gravatar_id":"","url":"https://api.github.com/users/wch","html_url":"https://github.com/wch","followers_url":"https://api.github.com/users/wch/followers","following_url":"https://api.github.com/users/wch/following{/other_user}","gists_url":"https://api.github.com/users/wch/gists{/gist_id}","starred_url":"https://api.github.com/users/wch/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wch/subscriptions","organizations_url":"https://api.github.com/users/wch/orgs","repos_url":"https://api.github.com/users/wch/repos","events_url":"https://api.github.com/users/wch/events{/privacy}","received_events_url":"https://api.github.com/users/wch/received_events","type":"User","site_admin":false},"created_at":"2015-11-21T00:09:41Z","updated_at":"2015-11-21T00:09:41Z","body":"I don't think so - I believe that with R's reference counting, if there's only one reference to a list, then when you set an element in the list, it modifies the list directly, without making a copy.\r\n\r\nAlso, one danger of using the code you have there is if someone makes a copy of the list with `x <- example$example`, then modifying one will modify the other, which would be confusing."}}