Sven Heinz
Sven Heinz

Reputation: 51

f# Switching between different versions of parameters/properties?

I am trying to define a valuetype that tracks changes in its constituents (by the constituents informing values that use it, and use of some custom assignment operators) and then based on a modus(defined with the custon operator) updates differently.

I am relatively new to f# and have two problems:

1) use1: if the value is changed same=false and so I want to return the reference to the function and change same to true, however I seem to use the wrong syntax for match.

2)I can not shake the feeling my type is more complicated than it has to be.

I am trying to define a valuetype that tracks changes in its constituents (by the constituents informing values that use it, and use of some custom assignment operators) and then based on a modus(defined with the custon operator) updates differently.

type valtype(x) as this = 
     let reference= ref x
     let mutable value = x     
     member val same= true with get, set
     member val uses=[] with get, set    
     member this.chang1=  value<- !reference
     member this.chang2=  this.same<- false
     member val changed= this.chang1          
     member this.use1= fun a->a
     member val used=this.use1

//here the error appears
     member this.use2= if this.same then this.same<-true value<- !reference else fun a->a
    //    match this.same with
    //    |false-> this.same<-true value<- !reference
    //    |true-> fun a->a

     member this.Value
        with get()= this.used value
        and set(c)=value<-c

wanted behaviour: pseudocode:

func(x)=x+3 var1 §=func(x), var2 $=func(x), var3 &= func(x)

func(x)=x var1(intern x), var2(intern x+2), var3(intern x+2)

Ausgabe func(1): var1-> 1 ,var2(change:intern x)-> 1, var3-> 4

var3.orderChange var3(change:intern x)

Ausgabe func(1): var3->1

Upvotes: 1

Views: 156

Answers (2)

Sven Heinz
Sven Heinz

Reputation: 51

Well I did a lot wrong, here the current hopefully correct version of this project.

type Valtype(n:unit ->'T)= 
     let mutable reference= n 
     let mutable value = n()
     let uses= ResizeArray<Valtype>[]
     let get1()=
        value
     //let get2()=
     //   value<-reference()
     //   value
     let mutable get0=fun()->get1()
     let get3()=
        value<-reference()
        get0<-fun()->get1()
        value
     let set1(r:unit ->'T)=
        reference<-r
        for u in uses do
            u.Changed
        value<-r()
     //let set2(r)=
     //   reference<-fun()->r
     let mutable set0=fun(r)->set1(r)
     let mutable modi =0;
     member x.Modi
        with get()= modi
        and set(i)= modi<-i%3
     member x.Subscribe(f)=
        uses.Add(f)
     member x.Unsubscribe(f)=
        uses.Remove(f)
     member x.Value
        with get()=get0()
     member x.Set(r:unit ->'T)=set0(r:unit ->'T)
     member x.Changed=
        match modi with
        |0-> get0<-fun()->get3()`enter code here`
        |1-> value<-reference()
        |_-> ()
     member x.Force= 
        value<-reference()

Upvotes: 0

rmunn
rmunn

Reputation: 36688

While I can't answer your main question since I don't understand what you're trying to do, I can tell you that you have a syntax error in your use2 method. This is what you wrote:

member this.use2= if this.same then this.same<-true value<- !reference else fun a->a

And this the correct way to write that:

member this.use2=
    if this.same then
        this.same<-true
        value<- !reference
    else fun a->a

Notice how each assignment expression is on its own line. Jamming two assignment expressions onto a single line is a syntax error: the F# compiler would read this.same<-true value<- !reference as "Call the function true with the parameter value, then assign that result to this.same" — and then the second <- operator would produce a syntax error since you can't assign two values to one variable in a single line.

Also, given how you wrote that match expression below, I feel sure that what you wanted to write in your use2 method was if not this.same then ....

Can't help you with the rest of your question, but fixing the syntax of use2 will at least get you partway towards a solution.

Upvotes: 1

Related Questions