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:)

  • 相关阅读:
    fork子进程
    多输入使用多线程
    多输入select
    多输入之轮询
    开启telnet
    slickedit编译调试linux应用程序
    电子书框架
    通用Makefile
    STDIN_FILENO和stdin
    libiconv交叉编译提示arm-none-linux-gnueabi-gcc
  • 原文地址:https://www.cnblogs.com/FsharpZack/p/2758787.html
Copyright © 2011-2022 走看看