为什么第一类函数的类型包含byrefs?

时间:2016-06-30 17:19:06

标签: f#

例如,以下内容是不允许的,我不确定原因:

> let f () = 
    let f2 (a : byref<int>) =
        ()
    let mutable a = 0
    f2 &a;;

我的猜测是,byref可能是对堆栈变量的可变引用,如果f2决定将其存储在某个地方,它可能会超出范围。还是别的什么?

2 个答案:

答案 0 :(得分:9)

.NET类型系统不允许将byref类型用作泛型类型参数(例如,您无法创建<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" app:theme="@style/CustomActionBar"/> )。由于(第一类)F#函数实际上是 using System using System.Net using Microsoft.Office.Interop.Excel; //LAUNCH AND TEMPLATE EXCEL Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); Workbook wb = xlApp.Workbooks.Add(); Worksheet ws = (Worksheet)wb.Worksheets[1] ws.Cells[1, 1] = "Report Date"; ws.Cells[1, 2] = "Catagories"; ws.Cells[1, 3] = "Page Name"; ws.Cells[1, 4] = "Last Revision Date"; ws.Cells[1, 5] = "Beginning Total"; ws.Cells[1, 6] = "Ending Total"; ws.Cells[1, 7] = "Monthly Total"; ws.Cells[1, 8] = "YTD"; //LAUNCH AND TEMPLATE EXCEL wb.SaveCopyAs("C:/Users/pta72645/Desktop/TestFile5_2.xlsx"); //save Excel, **ERROR OCCURS HERE** 类型的实例,这意味着F#函数也不能在其域或范围内使用byref。

答案 1 :(得分:0)

错误似乎源于您将f2声明为嵌套函数的事实。如果从f下面提取它,它会编译:

let f2 (a : int byref) = ()

let f () =
    let mutable a = 1
    f2 &a