简体   繁体   中英

F# A value or constructor is not defined, even though using get and set

I'm a first year CS student, with nor prior programming knowledge. We have just finished our time with functional programming, and have now gone to object-oriented programming. I'm currently working on an assignment, in which I have to imitate a race between animals. These animals have been given some attributes and methods, which defines their weight, maximum speed etc. One of the requirements of the code is that it has to generate a random variable, which determines its weight, for each instance it is called. So far I have come to this point in my code:

let rnd = System.Random()

type Animal (name:string, animalWeight:float, maxSpeed:float) = class

  let mutable foodInTakePercentage = float(rnd.Next(0,101))

  member val animalMaxSpeed : float = maxSpeed with get, set
  member val animalWeight = animalWeight with get, set
  member val neccesaryFoodIntake = 0.0 with get, set
  member val Name = name
  new (name, maxSpeed) =
    let minWeight = 70.0
    let maxWeight = 300.0
    let Weight = minWeight + rnd.NextDouble() * (maxWeight-minWeight)
    Animal (name, Weight, maxSpeed)
  member this.FoodInTakePercentage = foodInTakePercentage/100.0
  member this.CurrentSpeed =
    this.FoodInTakePercentage*maxSpeed
  abstract FoodIntake : float 
  default this.FoodIntake = 0.5 
  member this.NeccesaryFoodIntake = 
    neccesaryFoodIntake <- animalWeight * FoodIntake

end

type Carnivore (name:string, animalWeight:float, maxSpeed:float) = class
  inherit Animal (name, animalWeight, maxSpeed)
  override this.FoodIntake = 0.08
end

type Herbivore (name:string, animalWeight:float, maxSpeed:float) = class
  inherit Animal (name, animalWeight, maxSpeed)
  override this.FoodIntake = 0.4
end

The problem is that when this is compiled, I recieve the error message:

10g.fsx(22,5): error FS0039: The value or constructor 'neccesaryFoodIntake' is not defined

I have tried everything (of my very limited knowledge) in order to try to define it as a variable, but nothing seems to work. Does anyone have an idea?

necessaryFoodIntake is a class member, not a free-standing value. To reference class members, you need to specify an object of that class, eg x.necessaryFoodIntake .

In your code, it feels like you're trying to reference the member on the " current " object, which is denoted this , so that's what you need to specify as the object:

member this.NeccesaryFoodIntake = 
    this.neccesaryFoodIntake <- animalWeight * FoodIntake

That said, it's not entirely clear what you're trying to achieve with the NecessaryFoodIntake member. The way you defined it, it's a property with a getter, and that getter actually modifies another member ( necessaryFoodIntake ) and doesn't return anything (ie returns unit ). Usually property getters are supposed to return a value and not modify state.

If you indeed wanted to define a member that modifies some internal state and doesn't return anything, you should (1) make it a method, not property, and (2) name it something more appropriate, eg

member this.CalculateNeccesaryFoodIntake() = 
    this.neccesaryFoodIntake <- animalWeight * FoodIntake

On the other hand, if you wanted to define a property with a single getter that returns the value of another member, you should not have it modify anything:

member this.NeccesaryFoodIntake = this.neccesaryFoodIntake

But that would be kinda useless, because now you have two members which are almost identical, except one of them is read-only. If you wanted to make necessaryFoodIntake inaccessible from the outside and then provide NecessaryFoodIntake as a public interface for it, you should have made the former private :

member val private neccesaryFoodIntake = 0.0 with get, set

In summary, while I can help you with your specific syntax error, the rest of the code seems off as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM