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


      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() =
        [<DefaultValue>]
        val mutable public onCancel : System.Object -> unit
     
        member public this.Verify() = 
            if box(this.onCancel) = null then
                ()//do something

      Enjoying:)

  • 相关阅读:
    [汇编与C语言关系]1.函数调用
    深入理解计算机系统
    设计模式
    深度探索C++对象模型
    More Effective C++
    MySQL必知必会
    数据结构与算法分析
    Java编程思想(后)
    Java编程思想(前十章)
    Java 入门
  • 原文地址:https://www.cnblogs.com/FsharpZack/p/2758787.html
Copyright © 2020-2023  润新知