90 people like it.
Like the snippet!
Read only ref
F# implementation of RO_ref from the "Effective ML" talk.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
|
module PRef =
type RO = RO
type RW = RW
type t<'a, 'b>(r:'a ref) =
member internal x.Ref = r
let pref r = t<_, RW>(ref r)
let pref_ref r = t<_, RW>(r)
let inline (!!) (r:t<_, _>) = !r.Ref
let get r = !!r
let inline (=<) (r:t<_, RW>) v = r.Ref := v
let set r v = r =< v
let ro (r:t<_, _>) = t<_, RO>(r.Ref)
//FSI Example
open PRef
let x = pref 1
x =< 3
!!x // 3
let z = ro x
z =< 2 // fails, z is RO
|
Multiple items
union case RO.RO: RO
--------------------
type RO = | RO
Full name: Script.PRef.RO
Multiple items
union case RW.RW: RW
--------------------
type RW = | RW
Full name: Script.PRef.RW
type t<'a,'b> =
new : r:'a ref -> t<'a,'b>
member internal Ref : 'a ref
Full name: Script.PRef.t<_,_>
val r : 'a ref
Multiple items
val ref : value:'T -> 'T ref
Full name: Microsoft.FSharp.Core.Operators.ref
--------------------
type 'T ref = Ref<'T>
Full name: Microsoft.FSharp.Core.ref<_>
val x : t<'a,'b>
member internal t.Ref : 'a ref
Full name: Script.PRef.t`2.Ref
val pref : r:'a -> t<'a,RW>
Full name: Script.PRef.pref
val r : 'a
new : r:'a ref -> t<'a,'b>
val pref_ref : r:'a ref -> t<'a,RW>
Full name: Script.PRef.pref_ref
val r : t<'a,'b>
property t.Ref: 'a ref
val get : r:t<'a,'b> -> 'a
Full name: Script.PRef.get
val r : t<'a,RW>
val v : 'a
val set : r:t<'a,RW> -> v:'a -> unit
Full name: Script.PRef.set
val ro : r:t<'a,'b> -> t<'a,RO>
Full name: Script.PRef.ro
module PRef
from Script
val x : t<int,RW>
Full name: Script.x
val z : t<int,RO>
Full name: Script.z
More information