F# is rather strict about 'protected' bits 'escaping' the class scope accidentally. When this gets in your way, the 'standard workaround' is below:

1
2
3
4
5
6
7
8
9
10
type Foo() =
    inherit SomeBase()

    // F# does not allow protected members to be captured by lambdas, this is the standard workaround
    member internal x.BaseProtectedMethod(args) =  
        base.ProtectedMethod(args)

    member x.Other() =
        // use x.BaseProtectedMethod instead of base.ProtectedMethod
        ()
By on 3/23/2009 10:10 AM ()

How would you access protected base class fields? I'm trying to create a safe handle as follows:

1
2
3
4
5
6
7
8
9
10
11
12
type DeviceInfoSetSafeHandle() = 

        inherit SafeHandleMinusOneIsInvalid(true)

        [DllImport("setupapi.dll")]

        extern bool SetupDiDestroyDeviceInfoList(IntPtr deviceInfoSet)

        member internal s.BaseHandle = base.handle

        override s.ReleaseHandle() = SetupDiDestroyDeviceInfoList(s.BaseHandle)

I've tried it a number of different ways but when I try to access base.handle I get the following compile time error:

'base' values may only be used to make direct calls to the base implementations of overridden members

Thanks!

m

By on 1/28/2010 7:16 PM ()

How would you access protected base class fields?

You don't need to use base to refer to protected methods or fields, it's only used for calling into the base method when overriding a method. Instead, just use the identifier you used to refer to the instance (s in your case).

By on 1/29/2010 6:10 PM ()

I forgot that I could just use base.DangerousGetHandle() to get the handle. Anyways, I'd still be curious how to access base class protected fields.

By on 1/28/2010 7:46 PM ()

Thank you

By on 3/23/2009 11:33 AM ()

This is probably the most annoying thing about the F# language I'd like changed. (I guess that means it's doing pretty well if I have to complain about this.)

If there's not a technical, performance, or mathematical reason for this, I'd really prefer if F# didn't try to protect me from this (if I'm understanding the rationale correctly).

By on 3/23/2009 10:27 AM ()

Ignore this post.I posted a reply but saw that my answer was already in your question. (apparently I can't delete my post either)sorry.

By on 3/23/2009 6:39 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