zoukankan      html  css  js  c++  java
  • 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语言文法
    实验一
    词法分析
    py中文词频统计
    py字符串练习
    py画了个国旗
    熟悉常用的Linux操作
    大数据概述
    实验三、 递归下降分析程序实验
    简易c语言LL(1)文法
  • 原文地址:https://www.cnblogs.com/FsharpZack/p/2758787.html
Copyright © 2011-2022 走看看