f# - MIX seqbuilder [CustomOperation] attribute method AND vanilla yield IN a single seq expression -
this code runs fine except if uncomment last line in custom seq expression :
type t (i: int) = member x.i = override x.tostring() = sprintf "t %a " x.i type tbuilder() = member x.yield (()) = seq.empty [<customoperation("test")>] member x.test1 (source : seq<_>, i: int) : seq<t> = printfn "calling test1 i= %d" |> ignore seq { yield! source yield t(i) } let t = tbuilder() let mytest = t { test 42 test 43 // yield t(44) // if uncommented, not compile }
if yield t(44)
line uncommented, compiler error :
error control construct may used if computation expression builder defines 'for' method.
my question : there way mix
- my [customoperation] test (from method test1) yields t objects
with
- a vanilla yield, example
yield t(44)
or other seq related syntax
inside unique seq expression without defining 'for' method ?
reference : dsl in action f# (chapter 7) anh-dung phan on github. thanks.
short answer: no. if change operators preserve variable bindings (via maintainsvariablespace=true
or maintainsvariablespaceusingbind=true
arguments [<customoperator>]
attribute constructor) won't need for
you'll need bind
instead.
what expect computation expression you've written mean? if @ how f# spec specifies translation computation expressions, of form
bldr { op1 x op2 y yield z }
will turn
bldr.for(bldr.op2(bldr.op1(bldr.yield(), x), y), fun () -> b.yield(z))
so need for
method , yield
method needs different; @ least needs able take arguments of arbitrary types (e.g. in above example needs work on argument of type unit
, on argument of whatever type value z
has).
Comments
Post a Comment