What is the reason for needing an explict downcast?

By on 6/27/2006 8:19 PM ()

F# requires types to match exactly or you get a type error. You can generally get round this by placing # before your type constraint. For example the following won't compile:

1
2
3
4
5
6
7
8
9
10
open System.Windows.Forms

let show_form (form : Form) =
    form.Show()

// PrintPreviewDialog is defined in the BCL and is 
// derived directly the Form class
let my_form = new PrintPreviewDialog() 

do show_form my_form

Where as the following will:

1
2
3
4
5
6
7
let show_form_revised (form : #Form) =
    form.Show()

// ThreadExceptionDialog  is define in the BCL and is 
// directly derived type of the Form class
let another_form = new ThreadExceptionDialog(new Exception()) 
do show_form_revised another_form  
By on 6/27/2006 10:43 PM ()

You need an explicit downcast in the constructor, also a constructor was missing from testobj and "member x.get_Data() = x.data" was missing the "x". The following will compile:

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
type ITestInterface = interface
    abstract member count : int with get
end

type testobj = class
new() = {}
    interface ITestInterface
    with
        member x.get_count() = 0
    end
end

type ITestHolder = interface
    abstract member Data : ITestInterface with get
end


type holder_obj = class
    val data : ITestInterface
    // Constructor
    new() = { data = (new testobj()  :> ITestInterface)
                }
    interface ITestHolder
    with
        member x.get_Data() = x.data
    end
end
By on 6/23/2006 12:14 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