Hello Keiko.

As far as I can get, the problem seems to be that in the definition of c3:

1
2
3
4
5
type c3 = class
    inherit c2
    new (init) = { inherit c2 (init) }
    member self.x = 1
end;;

The line "member self.x = 1" does not redefine the field x in class c2, it defines a new member x for class c3 and this member "hides" the field x in class c2.

If you try to evaluate

1
2
3
4
5
6
7
8
9
let x3 = new c3 4;;
// val x3: c3

x3.x;;
//val it : int = 1
// but...

(x3 :> c2).x;;
// val it : int = 4

So the value of field x is there as it should.

Hope it helps.

By on 11/27/2007 1:13 AM ()

Hello.

Thank you for replies.
I have been thinking for a while, but I still do not understand.

Continuing my previous post:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
type c6 = class
   inherit c5
   new () = {}
   member self.f x = x + 3
   member self.g (x:int) = self.f x
end;;

type c7 = class
  inherit c5
  new () = {}
  member self.f x = x + 3
  member self.g (x:int) = (self :> c7).f x
end;;

let x6 = new c6 ();;
let x7 = new c7 ();;

x6.g 0;;
//val it : int = 2
x7.g 0;;
//val it : int = 2

As seen even I coerce self into c7, c7'self still looks for a method f within c5.
Is there a way to make it search within c7, for instance?
Could you please explain to me?

With best regards,
keiko

By on 11/29/2007 2:13 AM ()

Hi keiko,

I reworked your definitions for c5, c6 and c7. I think it works as you expect now.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
type c5 = class
    inherit c4
    new () = {}
    abstract f: int -> int default self.f x = x + 2
    abstract g: int -> int default self.g (x:int) = self.f x
end;;

type c6 = class
    inherit c5
    new () = {}
    override self.f x = x + 3
    override self.g (x:int) = self.f x
end;;

type c7 = class
    inherit c5
    new () = {}
    override self.f x = x + 3
    override self.g (x:int) = (self :> c7).f x
end;;

let x6 = new c6 ();;

let x7 = new c7 ();;

x6.g 0;;
//val it : int = 3

x7.g 0;;
//val it : int = 3

Also, the class c7 gives a warning "FS0066: This upcast is unnecessary - the types are identical".

Hope it helps.

By on 11/29/2007 3:20 AM ()

Thank you for such a prompt reaction.

Yes, I understand that everything go as expected
if I use this overriding mechanism.
Yet I also want to understand the hiding behavior I have been observing
to know how I can exploit this behavior in my programming practice.

Best regards,
Keiko

By on 11/29/2007 4:15 AM ()

BTW,

I thought that the line "member self.x = 1" would raise an error, because "self" is not automatically defined. The fact that there as no error really surprised me.

Could somebody explain that?

Thanks.

By on 11/27/2007 1:17 AM ()

You don't need to the same reference name for all methods, nor do you need declaring what reference name(s) you will use at the beginning of the class (as in OCaml where you specify it at the "top" of the class, but never reference it afterwards). The following works :

1
2
3
4
5
 
type foo() =
  member x.doit() = print_string "subliminal message"
  member y.run() = ()
  member z.run_again() = ()

If you want to use references to inherited class, you simply have to give an alias for that class, as in :

1
2
3
4
 
type bar()=
  inherit foo () as foo
  member x.doit() = foo.doit() ; print_string " : use F# !!!"
By on 11/27/2007 6:18 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper