Useless instructions: Difference between revisions

(→‎{{header|Raku}}: Add a Raku example)
Line 172:
</lang>
 
=={{header|Nim}}==
In Nim, for an integer “x”, <code>inc x, 3</code> and <code>x += 3</code> are equivalent. But this is true only for integers. If “x” is a float, the only valid statement is <code>x += 3</code>. And if “x” is a bool or an enum, we can write <code>inc x</code> but not, of course, <code>x += 1</code>.
 
So, this is not really a good example of useless instruction.
 
There is a better example for a string or a sequence “s”, where <code>s.add val</code> and <code>s &= val</code> are equivalent.
 
Also, when defining a type with variants, when a branch doesn’t contain any field, we can use the keyword <code>discard</code> (as in a case statement) or the value <code>nil</code>.
 
<lang Nim>type T = object
case i: 0..2
of 0: value: int
of 1: discard
of 2: nil</lang>
 
But, as <code>discard</code> is used as empty statement and <code>nil</code> is used as null value for references and pointers, we cannot say that they are useless even if they are synonyms in one context.
 
=={{header|Raku}}==
Anonymous user