{"action":"created","issue":{"url":"https://api.github.com/repos/google/flatbuffers/issues/333","labels_url":"https://api.github.com/repos/google/flatbuffers/issues/333/labels{/name}","comments_url":"https://api.github.com/repos/google/flatbuffers/issues/333/comments","events_url":"https://api.github.com/repos/google/flatbuffers/issues/333/events","html_url":"https://github.com/google/flatbuffers/issues/333","id":115628111,"number":333,"title":"Return null for missing optional field?","user":{"login":"zz85","id":314997,"avatar_url":"https://avatars.githubusercontent.com/u/314997?v=3","gravatar_id":"","url":"https://api.github.com/users/zz85","html_url":"https://github.com/zz85","followers_url":"https://api.github.com/users/zz85/followers","following_url":"https://api.github.com/users/zz85/following{/other_user}","gists_url":"https://api.github.com/users/zz85/gists{/gist_id}","starred_url":"https://api.github.com/users/zz85/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/zz85/subscriptions","organizations_url":"https://api.github.com/users/zz85/orgs","repos_url":"https://api.github.com/users/zz85/repos","events_url":"https://api.github.com/users/zz85/events{/privacy}","received_events_url":"https://api.github.com/users/zz85/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":4,"created_at":"2015-11-07T02:56:15Z","updated_at":"2015-11-11T17:35:05Z","closed_at":null,"body":"I'm trying out the javascript generated flatbuffer code when I observe that optional string fields return null if the field doesn't exist while missing field for uint returns 0. I would think that it's useful to know when a field is null(missing) instead of returning 0 (which is a valid number).\r\n\r\nIs this the intended behaviour? A quick check on the generated python and java classes shows that they return null for missing string fields but 0 on missing integers too.\r\n\r\nTo give a reduced use case, here's the idl. \r\n\r\n```c\r\ntable File {\r\n        size: uint;\r\n}\r\n\r\nroot_type File;\r\n```\r\n\r\nand the generated JS code.\r\n\r\n```js\r\n/**\r\n * @returns {number}\r\n */\r\nFile.prototype.size = function() {\r\n  var offset = this.bb.__offset(this.bb_pos, 6);\r\n  return offset ? this.bb.readUint32(this.bb_pos + offset) : 0;\r\n};\r\n```\r\n\r\nimho, i would prefer it to be the following (simply replace 0 with null when no offset is found)\r\n```js\r\nFile.prototype.size = function() {\r\n  var offset = this.bb.__offset(this.bb_pos, 6);\r\n  return offset ? this.bb.readUint32(this.bb_pos + offset) : null;\r\n};\r\n```\r\n\r\ncc @evanw (nice work on the js bindings btw!)\r\n\r\n_update_: I observe a similar behaviour with arrays too. a missing field for `[children]` would return `null` in `.children()` but 0 in `.childLength()`. \r\n"},"comment":{"url":"https://api.github.com/repos/google/flatbuffers/issues/comments/155856289","html_url":"https://github.com/google/flatbuffers/issues/333#issuecomment-155856289","issue_url":"https://api.github.com/repos/google/flatbuffers/issues/333","id":155856289,"user":{"login":"gwvo","id":6145400,"avatar_url":"https://avatars.githubusercontent.com/u/6145400?v=3","gravatar_id":"","url":"https://api.github.com/users/gwvo","html_url":"https://github.com/gwvo","followers_url":"https://api.github.com/users/gwvo/followers","following_url":"https://api.github.com/users/gwvo/following{/other_user}","gists_url":"https://api.github.com/users/gwvo/gists{/gist_id}","starred_url":"https://api.github.com/users/gwvo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gwvo/subscriptions","organizations_url":"https://api.github.com/users/gwvo/orgs","repos_url":"https://api.github.com/users/gwvo/repos","events_url":"https://api.github.com/users/gwvo/events{/privacy}","received_events_url":"https://api.github.com/users/gwvo/received_events","type":"User","site_admin":false},"created_at":"2015-11-11T17:35:05Z","updated_at":"2015-11-11T17:35:05Z","body":"The easiest way to get a null default for an integer field is to wrap it in a struct:\r\n~~~\r\nstruct myint { x:int; }\r\ntable mytable { scalar:myint; }\r\n~~~\r\nthis will get you null if scalar isn't present. It also doesn't take up any more space on the wire than a regular int.\r\n"}}