{"action":"created","issue":{"url":"https://api.github.com/repos/facebook/hhvm/issues/5156","labels_url":"https://api.github.com/repos/facebook/hhvm/issues/5156/labels{/name}","comments_url":"https://api.github.com/repos/facebook/hhvm/issues/5156/comments","events_url":"https://api.github.com/repos/facebook/hhvm/issues/5156/events","html_url":"https://github.com/facebook/hhvm/issues/5156","id":67567272,"number":5156,"title":"How to go from untyped data to typed array?","user":{"login":"tslettebo","id":7471024,"avatar_url":"https://avatars.githubusercontent.com/u/7471024?v=3","gravatar_id":"","url":"https://api.github.com/users/tslettebo","html_url":"https://github.com/tslettebo","followers_url":"https://api.github.com/users/tslettebo/followers","following_url":"https://api.github.com/users/tslettebo/following{/other_user}","gists_url":"https://api.github.com/users/tslettebo/gists{/gist_id}","starred_url":"https://api.github.com/users/tslettebo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tslettebo/subscriptions","organizations_url":"https://api.github.com/users/tslettebo/orgs","repos_url":"https://api.github.com/users/tslettebo/repos","events_url":"https://api.github.com/users/tslettebo/events{/privacy}","received_events_url":"https://api.github.com/users/tslettebo/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/facebook/hhvm/labels/hack","name":"hack","color":"fe8700"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":8,"created_at":"2015-04-10T10:48:44Z","updated_at":"2015-08-18T22:15:35Z","closed_at":null,"body":"We're currently converting our systems to Hack, and going one step at a time, we're going from arrays to typed arrays (rather than collections).\r\n\r\nWe've run into a problem when a database query returns a result of vaue \"mixed\", how to make the type checker understand that it has a specific format.\r\n\r\nThe problem is illustrated with the following simplified version of our database query function:\r\n```\r\n<?hh // strict\r\n\r\ntype RawRecordSet = array<array<arraykey,string>>;\r\n\r\nfunction fetch_recordset(string $sql): RawRecordSet\r\n{\r\n  $db_connection=mysqli_connect(DB_HOST,DB_USERNAME,DB_PASSWORD,DB_DATABASE);\r\n\r\n  $result=mysqli_query($db_connection,$sql);\r\n\r\n  $result_array=mysqli_fetch_all($result,MYSQLI_ASSOC);\r\n\r\n  return $result_array;\r\n}\r\n```\r\n\r\n// The following is found in builtins_mysqli.hhi:\r\n```\r\nfunction mysqli_fetch_all(mysqli_result $result, int $resulttype = MYSQLI_NUM): mixed;\r\n```\r\nThe above code gives these error messages:\r\ntest.php:13:10,22: Invalid return type (Typing[4110])\r\n  test.php:5:40,51: This is an array (used like a vector)\r\n  builtins_mysqli.hhi:189:81,85: It is incompatible with a mixed value\r\n\r\nI'd appreciate any suggestions about how we may go from mixed to array<array<arraykey,string>> like this.\r\n\r\nIf it's not possible using typed arrays, is it possible using collections, and if so, how?\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/facebook/hhvm/issues/comments/132372118","html_url":"https://github.com/facebook/hhvm/issues/5156#issuecomment-132372118","issue_url":"https://api.github.com/repos/facebook/hhvm/issues/5156","id":132372118,"user":{"login":"tslettebo","id":7471024,"avatar_url":"https://avatars.githubusercontent.com/u/7471024?v=3","gravatar_id":"","url":"https://api.github.com/users/tslettebo","html_url":"https://github.com/tslettebo","followers_url":"https://api.github.com/users/tslettebo/followers","following_url":"https://api.github.com/users/tslettebo/following{/other_user}","gists_url":"https://api.github.com/users/tslettebo/gists{/gist_id}","starred_url":"https://api.github.com/users/tslettebo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tslettebo/subscriptions","organizations_url":"https://api.github.com/users/tslettebo/orgs","repos_url":"https://api.github.com/users/tslettebo/repos","events_url":"https://api.github.com/users/tslettebo/events{/privacy}","received_events_url":"https://api.github.com/users/tslettebo/received_events","type":"User","site_admin":false},"created_at":"2015-08-18T22:15:35Z","updated_at":"2015-08-18T22:15:35Z","body":"@steelbrain There's now a way to do this in the [nightly build](5918):\r\n```\r\nfunction assert_object<T>(classname<T> $class_name,mixed $value): T\r\n{\r\n  invariant($value instanceof $class_name,\"\");\r\n\r\n  return $value;\r\n}\r\n\r\n$user = assert_object(\"User\",f());\r\n```\r\n"}}