OliverVS
OliverVS

Reputation: 53

Making a function which changes the x,y values in a method

I am very new to object oriented programming. I have to make a method which moves my drone closer to it's destination however I can't seem to make it work. I have added my code, I have tried to access the first and second value in my tuples position and destination. But this code gives me errors saying that position and destination is not mutable. What am I doing wrong?

module Drones 
type Drone(x1:int, y1:int, x2:int, y2:int, spd:int) =
    let mutable position = (x1,y1)
    let mutable destination = (x2,y2)
    let mutable speed = spd
    member x.fly (position,destination) =
        if fst destination - fst position > 0 then position <- (x1+1,y1)
        else if snd destination - snd position > 0 then position <- (x1,y1+1) ```

Upvotes: 0

Views: 104

Answers (1)

Scott Hutchinson
Scott Hutchinson

Reputation: 1721

This should work:

module Drones 

type Drone(x1:int, y1:int, x2:int, y2:int, spd:int) =
    let mutable position = (x1,y1)
    let mutable destination = (x2,y2)
    let mutable speed = spd
    member x.fly pos dest =
        if fst dest - fst pos > 0 then position <- (x1+1,y1)
        else if snd dest - snd pos > 0 then position <- (x1,y1+1)

Or maybe better to remove the parameters from the fly method:

type Drone(x1:int, y1:int, x2:int, y2:int, spd:int) =
    let mutable position = (x1,y1)
    let mutable destination = (x2,y2)
    let mutable speed = spd
    member x.fly () =
        if fst destination - fst position > 0 then position <- (x1+1,y1)
        else if snd destination - snd position > 0 then position <- (x1,y1+1)

Upvotes: 1

Related Questions