1
votes

I am using the F# CTP 1.9.7.8 and running the samples based on Tomas Petricek's article, on page 12

type MyCell(n:int) =
   let mutable data = n + 1
   do printf "Creating MyCell(%d)" n

   member x.Data
     with get() = data
     and  set(v) = data <- v

   member x.Print() =
     printf "Data %d" n

   override x.ToString() = 
     sprintf "(Data %d)" data

   static member FromInt(n) = 
     MyCell(n)

Four questions comes into mind as I typed this into the F# Interactive:

  1. Why do I get an error message as shown below in Figure 1.
  2. Why is there an = beside the member x.Print(), x.ToString() but not in member x.Data?
  3. Where did the x come from? and why is it there when the type MyCell is being defined so how can you reference an 'object' in that way, such as for x.Print(), x.ToString() and x.Data?
> type MyCell(n:int) =
- let mutable data = n + 1

  type MyCell(n:int) =
  -----^^^^^^^

stdin(6,6): error FS0547: A type definition requires one or more members or othe
r declarations. If you intend to define an empty class, struct or interface, the
n use 'type ... = class end', 'interface end' or 'struct end'.
-

Figure 1.

Thanks, Best regards, Tom.

3

3 Answers

4
votes
  1. As pblassucci said, you need to indent your class's contents.
  2. Print and ToString are methods, but Data is a property, so for Data the = comes before the definitions of the get and set methods.
  3. Instead of always using an identifier like this to refer to the class whose members are being defined, F# lets you choose an identifier on a member-by-member basis. x is used in many examples, but the choice is arbitrary.
4
votes

It looks like to:

> type MyCell(n:int) =
- let mutable data = n + 1

is not respecting the indentation. F# is whitespace sensitive by default, so you must keep any indentation. Try instead:

> type MyCell(n:int) =
-     let mutable data = n + 1
-     // etc.

(You can make F# non-whitespace sensitive by adding #light "off" at the top of the file, then you need to use extra keywords, as per danben's answer).

3
votes

even simpler, just indent your class body...

> type MyCell(n:int) =
-     let mutable data = n + 1
...