How to check whether an F# function/method has been initialized

1732 단어
Sometimes, we may run into this kind of situation that  we want to check if the given method/function has been initialized. We all know this is fairly easy in C#, since we can use delegate to invoke the function , then verify if the value of delegate is null. But in F# , delegate is rarely needed because F# can treat a function as a value, without the need for any wrapper. So , here is an easy way to solve this problem.
  Here is what we do in C# side:
public class Class1
    {
        public delegate void CancelHandler(Object sender);
        public CancelHandler onCancel;
 
        public void Verify()
        {
            if (onCancel == null)
            {
                //do something;
            } 
        } 
    }
		

  Usually, the null keyword is converted to opetion type in F#, but in this situation, we should take the judgement as this:
type public Class1() =
    [>]
    val mutable public onCancel : System.Object -> unit
 
    member public this.Verify() = 
        if box(this.onCancel) = null then
            ()//do something

 
  Enjoying:)
 
다음으로 전송:https://www.cnblogs.com/FsharpZack/archive/2012/11/07/2758787.html

좋은 웹페이지 즐겨찾기