samedi 21 novembre 2020

Why return type of either output of an IF Statement has to be the same?

For example:

IF (COND) then EXPR1 else EXPR2

Following are the types.

  • IF:T1
  • COND:T2
  • EXPR1:T3
  • EXPR2:T4

Following are the constraints.

  • T1 = T3 = T4
  • T2 = bool

My question is why T3 and T4 has to be the same? Cannot I express an algorithm like below? If not, please explain why.

*IF (COND) {return EXPR1 -- string}
ELSE {return EXPR2 -- int}*

Aucun commentaire:

Enregistrer un commentaire