Expression Versus Statement

前端 未结 21 1945
别那么骄傲
别那么骄傲 2020-11-22 11:54

I\'m asking with regards to c#, but I assume its the same in most other languages.

Does anyone have a good definition of expressions and statements

21条回答
  •  独厮守ぢ
    2020-11-22 12:13

    To improve on and validate my prior answer, definitions of programming language terms should be explained from computer science type theory when applicable.

    An expression has a type other than the Bottom type, i.e. it has a value. A statement has the Unit or Bottom type.

    From this it follows that a statement can only have any effect in a program when it creates a side-effect, because it either can not return a value or it only returns the value of the Unit type which is either nonassignable (in some languages such a C's void) or (such as in Scala) can be stored for a delayed evaluation of the statement.

    Obviously a @pragma or a /*comment*/ have no type and thus are differentiated from statements. Thus the only type of statement that would have no side-effects would be a non-operation. Non-operation is only useful as a placeholder for future side-effects. Any other action due to a statement would be a side-effect. Again a compiler hint, e.g. @pragma, is not a statement because it has no type.

提交回复
热议问题