◆ Boolean? とか演算子オーバーライドとかゆるく比較されるとか

ネットで見かけた書き込みで if (value == true) という謎なコードがあった というのがありました

Java などの強い型付け言語だと 型が異なる変数は比較自体できないので true と比較できるのは Boolean 型だからわざわざ true と比較する必要はありません

ですが 意味のある場合もあります


例えば C# だと Boolean? 型という Nullable な型があります
true/false/null のいずれかですが true の場合のみというときは value == true としないとエラーになります
Boolean と Boolean? は別の型なので if 文の中に Boolean? 型はおけません
Boolean? b = true;
if (b == true) Console.WriteLine("This is shown.");
if (b) Console.WriteLine("Exception");
// Cannot implicitly convert type 'bool?' to 'bool'.

また C++ などの == 演算子をオーバーライドできる言語だと true と比較するのは Boolean 型以外の型の場合もあり定義した独自の比較処理が行われます

JavaScript などの弱い型付け言語だと == はキャストして型を合わせて比較という演算子になります
JavaScript では if の中に Boolean 型以外を入れると Boolean 関数に入れた結果で判定され true と == 比較したものとは別の結果です
if (1 == true) console.log("This is shown.")
if (2 == true) console.log("This is not shown.")
if ([] == true) console.log("This is not shown.")
if ({} == true) console.log("This is not shown.")
if ("0" == true) console.log("This is not shown.")

↑は true と比較せず if 文の中に直接書けば全部表示されます


という感じで if (value == true) という書き方は言語によってはちゃんと意味があるんです