たとえば、PHP やその他の緩い型言語では、次のようなコードを作成できます。
/* PSEUDOCODE based on PHP */
$selection = $this->getSelection($id);
/* Below code will not work in a strict language */
if (is_array($selection))
show_template($selection);//forwards *array* results to the view script
else
print $selection; //prints the *string* notice message
/* Returns either array or string */
/* return param doubles as a business-payload or error message */
function getSelection($id)
{
$result = $db->getResultsById($id);
if ($result) return $result;//array
else return "No results";//string
}
PHP や同様の言語でこれを行うことができるので、そうするべきでしょうか?
別の方法として、たとえば C++ の場合と同様に、コードを再設計するか、データ構造を作成してその型を維持する場所にコーディングすることを強制することができます。しかし、私はそれをする必要がありますか?