I was reading through my most recent book addiction purchase (Programming F#) and I came across something I thought was interesting.
The (=) operator, basically in any other language assigns something to another thing, but in F# it’s actually a comparison operator. In C# (==) is used for comparison and (=) is used for assignment. If you forget the second (=), you can create a fun bug, for example:
bool canDoSomething = false;
if (canDoSomething = true)
{
DoSomething();
}
In this example the if statement is assigning true to the canDoSomething variable, which evaluates to true if the assignment is successful.
The if statement should really be:
if (canDoSomething == true)
In F# you “assign” using the let statement:
let canDoSomething = true
Moral of the story is, if you program in F#, it’s easier to avoid this bug. However, watch your step when going from F# to another language.
For a little bit more information, check out this list of F# arithmetic operators:
http://msdn.microsoft.com/en-us/library/dd469493%28VS.100%29.aspx
0 Responses
Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.