{"action":"closed","issue":{"url":"https://api.github.com/repos/facebook/immutable-js/issues/301","repository_url":"https://api.github.com/repos/facebook/immutable-js","labels_url":"https://api.github.com/repos/facebook/immutable-js/issues/301/labels{/name}","comments_url":"https://api.github.com/repos/facebook/immutable-js/issues/301/comments","events_url":"https://api.github.com/repos/facebook/immutable-js/issues/301/events","html_url":"https://github.com/facebook/immutable-js/issues/301","id":56082803,"number":301,"title":"Extending/creating custom Immutable types","user":{"login":"tgriesser","id":154748,"avatar_url":"https://avatars.githubusercontent.com/u/154748?v=3","gravatar_id":"","url":"https://api.github.com/users/tgriesser","html_url":"https://github.com/tgriesser","followers_url":"https://api.github.com/users/tgriesser/followers","following_url":"https://api.github.com/users/tgriesser/following{/other_user}","gists_url":"https://api.github.com/users/tgriesser/gists{/gist_id}","starred_url":"https://api.github.com/users/tgriesser/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tgriesser/subscriptions","organizations_url":"https://api.github.com/users/tgriesser/orgs","repos_url":"https://api.github.com/users/tgriesser/repos","events_url":"https://api.github.com/users/tgriesser/events{/privacy}","received_events_url":"https://api.github.com/users/tgriesser/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/facebook/immutable-js/labels/needs-decision","name":"needs-decision","color":"5319e7"}],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":32,"created_at":"2015-01-30T20:48:14Z","updated_at":"2016-05-01T18:07:24Z","closed_at":"2016-05-01T18:07:24Z","body":"I figured a separate ticket would be a better place to discuss what an API for this feature might look like, since the potential implementation approach depends on this heavily. So here are some things I've been thinking about. While the syntax:\r\n\r\n```js\r\nclass User extends Immutable.Map {\r\n}\r\n```\r\n\r\nis kind of neat/simple and initially looks idiomatic ES6, it comes with a lot of baggage. First, it's always necessary to define a custom constructor to pass along the return value from the `super` call as this isn't done implicitly in js classes. \r\n\r\n```js\r\nclass User extends Immutable.Map {\r\n   constructor(val) {\r\n      return super(val)\r\n   }\r\n}\r\n```\r\n\r\nFurther, as the constructor doesn't really act like a normal constructor there end up being a lot of additional checks necessary to ensure that another Map passed into the custom type is [coerced correctly](https://github.com/tgriesser/immutable-js/blob/10deaf40f829bd81dc7a58afe861bbe9d518cb18/src/Map.js#L34).\r\n\r\nSo I'm wondering whether there should be something more like:\r\n\r\n```js\r\nvar User = Immutable.make(Immutable.Map, {\r\n   \r\n   fullName() {\r\n     return this.get('firstName') + ' ' + this.get('lastName')\r\n   }\r\n\r\n})\r\n```\r\n\r\nwould be better suited. It would allow for having a dedicated constructor type for a custom immutable type. It might still come with a perf-hit but it would be better isolated from the core implementation.\r\n\r\nAlso, it might be worth examining at what the equivalent would have looked like in [mori](https://github.com/swannodette/mori/pull/108) when implemented with cljs protocols."}}