functional programming - From the F# interactive, why would it not accept this? -
I am using F # CTP 1.9.7.8 and running samples based on Thomas Petris's article on page 12
Type MyCell (n: int) = mutual data = n + 1 print "MyCell (create% d)" N member x.get () = Data and set with data (v) = Data & lt; - v member x Print () = printf "data% d" n override x.ToString () = sprintf "(data% d)" data static member FromInt (n) = MyCell (n)
# Type in interactive:
- I got an error message as shown in Figure 1 below.
- Why a
=
next tomember x.print ()
,x.ToString ()
butmember X.Data not in
Li> - Where did
x
come? And why this happens when typeMyCell
is being defined, how can you reference 'object' in such a way, likex.print ()
, < Code> X.ToString () andx.Data ? Type myCell (n: int) = - Temporary data = n + 1 type myCell (n: int) = ----- ^^^^^^^ stdin ( 6,6): Error FS0547: One type of definition is required for one or more members or other announcements. If you intend to define an empty square, structure or interface, use 'type ... = class end', 'interface end' or 'straight end'. -
Figure 1.
Thanks, Best regards, Tom.
As pblassucci said, you have to indent the contents of your class.
print
andtoasting
are methods, butdata is a property, so the
data For
and=
set
methods.- Instead of using an identifier such as
this
to refer to that class, whose members are being defined, F # is the identifier on the Member Member Member basis. Lets choose. Thex
is used in many instances, but the choice is arbitrary.
Comments
Post a Comment