{"action":"created","issue":{"url":"https://api.github.com/repos/JuliaLang/julia/issues/11902","labels_url":"https://api.github.com/repos/JuliaLang/julia/issues/11902/labels{/name}","comments_url":"https://api.github.com/repos/JuliaLang/julia/issues/11902/comments","events_url":"https://api.github.com/repos/JuliaLang/julia/issues/11902/events","html_url":"https://github.com/JuliaLang/julia/issues/11902","id":91541587,"number":11902,"title":"Julep: More support for working with immutables","user":{"login":"Keno","id":1291671,"avatar_url":"https://avatars.githubusercontent.com/u/1291671?v=3","gravatar_id":"","url":"https://api.github.com/users/Keno","html_url":"https://github.com/Keno","followers_url":"https://api.github.com/users/Keno/followers","following_url":"https://api.github.com/users/Keno/following{/other_user}","gists_url":"https://api.github.com/users/Keno/gists{/gist_id}","starred_url":"https://api.github.com/users/Keno/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Keno/subscriptions","organizations_url":"https://api.github.com/users/Keno/orgs","repos_url":"https://api.github.com/users/Keno/repos","events_url":"https://api.github.com/users/Keno/events{/privacy}","received_events_url":"https://api.github.com/users/Keno/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/JuliaLang/julia/labels/julep","name":"julep","color":"F4F0CB"}],"state":"open","locked":false,"assignee":null,"milestone":{"url":"https://api.github.com/repos/JuliaLang/julia/milestones/11","html_url":"https://github.com/JuliaLang/julia/milestones/0.4.0","labels_url":"https://api.github.com/repos/JuliaLang/julia/milestones/11/labels","id":748886,"number":11,"title":"0.4.0","description":"For tracking release-defining items for 0.4: important new features, breaking changes, and blocking bugs.\r\n\r\nAfter a brief period for deciding scope, adding issues to this milestone is not allowed. Once the items in this milestone are done, 0.4 will enter its stabilizing period and a release candidate can be made soon after.","creator":{"login":"JeffBezanson","id":744556,"avatar_url":"https://avatars.githubusercontent.com/u/744556?v=3","gravatar_id":"","url":"https://api.github.com/users/JeffBezanson","html_url":"https://github.com/JeffBezanson","followers_url":"https://api.github.com/users/JeffBezanson/followers","following_url":"https://api.github.com/users/JeffBezanson/following{/other_user}","gists_url":"https://api.github.com/users/JeffBezanson/gists{/gist_id}","starred_url":"https://api.github.com/users/JeffBezanson/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/JeffBezanson/subscriptions","organizations_url":"https://api.github.com/users/JeffBezanson/orgs","repos_url":"https://api.github.com/users/JeffBezanson/repos","events_url":"https://api.github.com/users/JeffBezanson/events{/privacy}","received_events_url":"https://api.github.com/users/JeffBezanson/received_events","type":"User","site_admin":false},"open_issues":9,"closed_issues":53,"state":"open","created_at":"2014-08-10T18:13:46Z","updated_at":"2015-06-28T02:04:03Z","due_on":"2015-06-20T18:30:00Z","closed_at":null},"comments":2,"created_at":"2015-06-28T02:04:03Z","updated_at":"2015-06-28T03:54:55Z","closed_at":null,"body":"This Julep is motivated by the current difficulties of working with immutables, particularly tuples. This issue aims to capture the result of the JuliaCon discussions between @JeffBezanson @carnaval @vtjnash @yuyichao and myself.\r\n\r\nWorking with tuples as fixed size arrays is currently quite cumbersome, particularly because they are hard to construct (since they can't be changed after constructing). To address \r\n\r\n## setindex! on a Ref\r\n\r\nRef creates a mutable container around an immutable object. This allows us to create changed immutables by putting them inside a Ref, modifying them in place and then pulling the changed value out. To support this, we can defined setindex! on Ref, giving it an index chain to modify, e.g.\r\n```\r\nref = Ref{Tuple{Tuple{Int32,Int64},Int64}}(((1,2),3))\r\nref[1,1] = 4\r\n@assert ref == ((1,4),3)\r\n```\r\nOn tuples, this should generally be lowered to an appropriate getlementptr and store (assuming the tuples are on the stack) or an appropriate insertelement. This would allow construction of long tuples as follows:\r\n```\r\nref = Ref{NTuple{1000,Int64}}()\r\nfor i = 1:1000\r\n    ref[i] = i\r\nend\r\nreturn ref[] # This is not our constructed tuple\r\n```\r\n## Complications with general immutables\r\nThis syntax should eventually also extend to generic immutables, but that need not be part of the initial implementation:\r\n```\r\nimmutable foo\r\ny::Int64\r\nend\r\nimmutable bar\r\nx::foo\r\nend\r\nref = Ref{bar}()\r\nref[:x,:y] = 4\r\nref[] # bar(foo(4))\r\n```\r\nHowever, semantically, we do not want to allow setting arbitrary fields in immutables, because those immutables may then no longer obey invariants imposed by the constructor. To properly support this use case, we propose lowering the above `ref[:x,:y] = 4` as (semantically at least)\r\n```\r\nref[] = setindex(ref[],:x,setindex(ref[:x],:y,4))\r\n```\r\nwhere `setindex` is a new generic function that creates a copy of the passed in first argument but modifying the identified field to the given new value. To support this in general, a default version of `setindex` would be created by default unless an inner constructor is specified (i.e. the same way in which default constructors are currently created). People needing to enforce invariants on their immutables would then be expected to provide a method of setindex (perhaps defining it within the type block to get access to `new`), that checks that the appropriate invariants are maintained. Additionally, `setindex` would support a field index as a second argument, as well as providing a definition that takes keyword arguments and semantically applies `setindex` several times to change all arguments.\r\n"},"comment":{"url":"https://api.github.com/repos/JuliaLang/julia/issues/comments/116190527","html_url":"https://github.com/JuliaLang/julia/issues/11902#issuecomment-116190527","issue_url":"https://api.github.com/repos/JuliaLang/julia/issues/11902","id":116190527,"user":{"login":"quinnj","id":2896623,"avatar_url":"https://avatars.githubusercontent.com/u/2896623?v=3","gravatar_id":"","url":"https://api.github.com/users/quinnj","html_url":"https://github.com/quinnj","followers_url":"https://api.github.com/users/quinnj/followers","following_url":"https://api.github.com/users/quinnj/following{/other_user}","gists_url":"https://api.github.com/users/quinnj/gists{/gist_id}","starred_url":"https://api.github.com/users/quinnj/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/quinnj/subscriptions","organizations_url":"https://api.github.com/users/quinnj/orgs","repos_url":"https://api.github.com/users/quinnj/repos","events_url":"https://api.github.com/users/quinnj/events{/privacy}","received_events_url":"https://api.github.com/users/quinnj/received_events","type":"User","site_admin":false},"created_at":"2015-06-28T03:54:55Z","updated_at":"2015-06-28T03:54:55Z","body":"There was some consensus that the more-minimal tuple implementation could be 0.4 material while the rest could be done later."}}