{"action":"created","issue":{"url":"https://api.github.com/repos/nim-lang/rfcs/issues/71","repository_url":"https://api.github.com/repos/nim-lang/rfcs","labels_url":"https://api.github.com/repos/nim-lang/rfcs/issues/71/labels{/name}","comments_url":"https://api.github.com/repos/nim-lang/rfcs/issues/71/comments","events_url":"https://api.github.com/repos/nim-lang/rfcs/issues/71/events","html_url":"https://github.com/nim-lang/rfcs/issues/71","id":370221768,"node_id":"MDU6SXNzdWUzNzAyMjE3Njg=","number":71,"title":"[RFC] `reset()` on variant discriminators","user":{"login":"narimiran","id":16516130,"node_id":"MDQ6VXNlcjE2NTE2MTMw","avatar_url":"https://avatars2.githubusercontent.com/u/16516130?v=4","gravatar_id":"","url":"https://api.github.com/users/narimiran","html_url":"https://github.com/narimiran","followers_url":"https://api.github.com/users/narimiran/followers","following_url":"https://api.github.com/users/narimiran/following{/other_user}","gists_url":"https://api.github.com/users/narimiran/gists{/gist_id}","starred_url":"https://api.github.com/users/narimiran/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/narimiran/subscriptions","organizations_url":"https://api.github.com/users/narimiran/orgs","repos_url":"https://api.github.com/users/narimiran/repos","events_url":"https://api.github.com/users/narimiran/events{/privacy}","received_events_url":"https://api.github.com/users/narimiran/received_events","type":"User","site_admin":false},"labels":[{"id":1091665951,"node_id":"MDU6TGFiZWwxMDkxNjY1OTUx","url":"https://api.github.com/repos/nim-lang/rfcs/labels/RFC","name":"RFC","color":"1d1d1d","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":6,"created_at":"2018-10-15T15:36:02Z","updated_at":"2018-10-15T15:36:17Z","closed_at":null,"author_association":"COLLABORATOR","body":"<a href=\"https://github.com/awr1\"><img src=\"https://avatars0.githubusercontent.com/u/41453959?v=4\" align=\"left\" width=\"96\" height=\"96\" hspace=\"10\"></img></a> **Issue by [awr1](https://github.com/awr1)**\n_Sunday Aug 12, 2018 at 20:43 GMT_\n_Originally opened as https://github.com/nim-lang/Nim/issues/8623_\n\n----\n\nAccording to Araq's post [here](https://forum.nim-lang.org/t/3515#22005) and the manual [here](https://nim-lang.org/docs/manual.html#types-object-variants) part of the design of of `reset()` is to allow an instance of a variant object to transition from one branch to another, something you cannot do with vanilla (i.e. `=`) reassignment of the discriminator *by design*.\r\n\r\nIn other terms, say `a.kind` is a discriminator in a variant and you first assign it via `a.kind = Mario`, it follows that any subsequent reassignment - `a.kind = Luigi` - would raise `FieldError`. To transition `a.kind` from `Mario` to `Luigi`, one would first need to `reset(a)`. This cleans out the entire contents of the object, which then permits the assignment `a.kind = Luigi`.\r\n\r\nThe problem here is the \"whole object\" part. `reset()` is fine if that whole object is just a single case section, but Nim permits fields outside of case sections, as well as multiple case sections alongside each other, which a simple `reset()` cleans out entirely. We need a way to be able to reset a case section whilst preserving the remaining contents of an object.\r\n\r\nI propose allowing `reset()` on case discriminators. Currently, you cannot call `reset()` on them, and will complain of:\r\n```\r\nvariant_test.nim(17, 6) Error: type mismatch: got <Character>\r\nbut expected one of: \r\nproc reset[T](obj: var T)\r\n  for a 'var' type a variable needs to be passed, but 'foo.kind' is immutable\r\n```\r\nCalling `reset()` on a case discriminator should do two things:\r\n\r\n1. Reset the discriminator field (obviously)\r\n2. Reset the complete contents of the associated union space.\r\n\r\nTake for instance, this example:\r\n\r\n```nim\r\ntype\r\n  Character = enum\r\n    Mario, Luigi, Peach, Bowser\r\n  Thingy = object\r\n    abc: int\r\n    case kind: Character\r\n    of Mario:\r\n      def: int\r\n      ghi: float\r\n    of Peach:\r\n      jkl: pointer\r\n    else: discard\r\n```\r\nwhich is represented in C as:\r\n```c\r\nstruct Thingy {\r\n  NI abc;\r\n  Character kind;\r\n  union {\r\n    struct {\r\n      NI def;\r\n      NF ghi;\r\n    } S1;\r\n    struct {\r\n      void* jkl;\r\n    } S2;\r\n  } kindU;\r\n};\r\n```\r\nIf `foo` were a variable of type `Thingy`, then calling `reset(foo.kind)` would internally zero out `kind` and the contents of `kindU`.\r\n\r\nThoughts?\n"},"comment":{"url":"https://api.github.com/repos/nim-lang/rfcs/issues/comments/429903899","html_url":"https://github.com/nim-lang/rfcs/issues/71#issuecomment-429903899","issue_url":"https://api.github.com/repos/nim-lang/rfcs/issues/71","id":429903899,"node_id":"MDEyOklzc3VlQ29tbWVudDQyOTkwMzg5OQ==","user":{"login":"narimiran","id":16516130,"node_id":"MDQ6VXNlcjE2NTE2MTMw","avatar_url":"https://avatars2.githubusercontent.com/u/16516130?v=4","gravatar_id":"","url":"https://api.github.com/users/narimiran","html_url":"https://github.com/narimiran","followers_url":"https://api.github.com/users/narimiran/followers","following_url":"https://api.github.com/users/narimiran/following{/other_user}","gists_url":"https://api.github.com/users/narimiran/gists{/gist_id}","starred_url":"https://api.github.com/users/narimiran/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/narimiran/subscriptions","organizations_url":"https://api.github.com/users/narimiran/orgs","repos_url":"https://api.github.com/users/narimiran/repos","events_url":"https://api.github.com/users/narimiran/events{/privacy}","received_events_url":"https://api.github.com/users/narimiran/received_events","type":"User","site_admin":false},"created_at":"2018-10-15T15:36:17Z","updated_at":"2018-10-15T15:36:17Z","author_association":"COLLABORATOR","body":"<a href=\"https://github.com/yglukhov\"><img src=\"https://avatars3.githubusercontent.com/u/6962409?v=4\" align=\"left\" width=\"48\" height=\"48\" hspace=\"10\"></img></a> **Comment by [yglukhov](https://github.com/yglukhov)**\n_Friday Sep 07, 2018 at 18:26 GMT_\n\n----\n\n\"I want\" nim to automatically reset the variant object fields upon discriminant change.\r\n```nim\r\ntype Foo = object\r\n  case discr: bool\r\n  of true:\r\n    a: int\r\n  of false:\r\n    b: float\r\n\r\nvar f: Foo\r\nf.b = 5.0\r\nd.discr = true # resets implicitly\r\nassert(f.a == 0)\r\nf.a = 5\r\nf.discr = false # resets implicitly\r\nassert(f.b == 0)\r\nf.discr = true # resets implicitly\r\nassert(f.a == 0)\r\n# EDIT:\r\nf.a = 5\r\nf.discr = true # Doesn't reset because discr doesn't change. Don't know if it's good or not though...\r\nassert(f.a == 5)\r\n```\n"}}
{"actor":{"display_login":"narimiran"}}