LOS*_*OST 9 f# byref return-by-reference
我正在尝试将F#样式接口添加到具有byref返回方法的类型.这是代码:
type IPool<'P, 'T when 'T: struct> =
abstract member GetReference: ITypedPointer<'P, 'T> -> byref<'T>
let Ref<'TPool, 'P, 'T when 'TPool :> IPool<'P, 'T>> (pool: 'TPool) pointer =
pool.GetReference pointer
Run Code Online (Sandbox Code Playgroud)
令我惊讶的是,在我介绍IPool界面之前,类似的事情工作正常.在此之前,Ref本身包含一个类似的实现&pool.data.[idx],并且运行良好.
我尝试安装每晚构建的F#Tools,因为最新版本没有正式支持byref返回,并且介绍它们的PR最近已经完成:https://github.com/Microsoft/visualfsharp/pull/4888
但是,我仍然error FS3209: The address of the variable 'copyOfStruct' cannot be used at this point. A method or function may not return the address of this local value.使用Visual Studio.类型outref<T>仍然似乎不可用.我错过了什么吗?
我也试图删除pointer参数,只返回pool.GetReference只得到一个不同的错误信息.
另外:最终目标是能够做到
let aref = Ref pool ptr
let bref = Ref pool ptr
aref <- 42
assert(aref = bref)
Run Code Online (Sandbox Code Playgroud)
例如,给调用者一个内部存储器的直接引用,通常由数组支持,类似于Span<T>.我出于性能原因进行此操作,因此在每次调用Ref时分配都不行.
出于某种原因,减少泛化有助于消除错误:
let Ref<'P, 'T when 'T: struct> (pool: IPool<'P, 'T>) pointer = pool.GetReference pointer
Run Code Online (Sandbox Code Playgroud)
解决方案提供者
https://github.com/Microsoft/visualfsharp/issues/5366#issuecomment-407521220
虽然它没有解释为什么原始代码不能编译。
| 归档时间: |
|
| 查看次数: |
341 次 |
| 最近记录: |