Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I satisfy the compiler to eliminate warning 0052?

I have a desire to satisfy compiler warning level 5. So I have 32 warnings in one file FS0052 The value has been copied to ensure the original is not mutated by this operation

I've followed the only SO post that seems to be related to this warning, but since my type is being type provider generated by Microsoft I can't just go mark the field mutable to quiet the warning. plus making something mutable that actually shouldn't ever be mutated seems like a hack not a fix.

examples:

  1. Nullable .GetValueOrDefault()
  2. Nullable .ToString()
  3. Guid .toString()
  4. struct method calls of any sort I believe

What is the recommended way to deal with this warning from a proper functional perspective?

like image 244
Maslow Avatar asked Apr 21 '15 13:04

Maslow


1 Answers

Not sure if you're still interested.

It seems to me that the compiler emits the warning when it is unsure whether the method call is going to destroy the state of original instance (this should mostly come from any library outside F#).

Explicit copy the value into a variable is, in my case, often mitigate the warning. For example:

open System

// generate the warning due to "ToString()"
let DirSeparator = Path.DirectorySeparatorChar.ToString()  

// no warning
let ExplicitCopy = let x = Path.DirectorySeparatorChar in x.ToString()  

let Alternative = sprintf "%c" Path.DirectorySeparatorChar
like image 96
Ruxo Avatar answered Nov 05 '22 10:11

Ruxo