hubFS: THE place for F#

. . . are you on The Hub?
Welcome to hubFS: THE place for F# Sign in | Join | Help
in Search

Inheriting .NET classes

Last post 05-26-2008, 22:41 by ekot. 2 replies.
Sort Posts: Previous Next
  •  05-26-2008, 1:36 6011

    Inheriting .NET classes

    Could someone tell me why this code gives me compilation warnings

    open System
    open System.Diagnostics

    type LogTraceListener =
    class
    inherit TraceListener
    new() as x = {}
    override TraceListener.Write(s: String) =
    Console.Write(s)
    override LogTraceListener.WriteLine(s: string) =
    Console.WriteLine(s)
    end

    let ltl = new LogTraceListener()
    let i = Trace.Listeners.Add(ltl)

    let () = Trace.Write("test");


    test.fs(9,27): warning FS0070: The member 'Write' doesn't correspond to a unique abstract slot based on name and argument count alone. Multiple implemented interfaces have a member with this name and argument count. Additional type annotations may be required to indicate the relevant override. This warning can be disabled using #nowarn "70" or --no-warn 70

    In 1.9.3.14 it is
    test.fs(9,27): warning: The abstract member implemented by this member couldn't be uniquely determined. Consider implementing in
    terfaces 'TraceListener' and 'TraceListener' explicitly.

    Also in 1.9.3.14 I actually get 'test' written to console when running it and in 1.9.4.17 a get nothing.
    How to fix it?
  •  05-26-2008, 16:49 6013 in reply to 6011

    Re: Inheriting .NET classes

    Hi,
    There is one change in the latest versio (1.9.4.17) - it reflects .NET "Conditional" attribute, so calls to Trace.Write are ignored unless you define symbol "TRACE" (by adding "--define TRACE" in the arguments). After addig this it worked for me. It reports the warning FS0070 and prints 'test' which is what I'd expect.

    The warning should IMHO just inform you that the overriding may be tricky. F# doesn't directly support overloading and TraceListener has Write(s:string) and Write(o:object), so when overriding the method you need to be more cautious to make sure that you override the on which you wanted (e.g. by specifying type annotations as you did). If you wanted to override both of them, you'd have to use OverloadID attribute, which is a trick for overloads in F#:

    type LogTraceListener =
      class
        inherit TraceListener
        new() as x = {}
        [<OverloadID("WriteStr")>]
        override x.Write(s: String) =
          Console.Write(s)
        [<OverloadID("WriteObj")>]
        override x.Write(s: obj) =
          Console.Write(s)
        override x.WriteLine(s: string) =
          Console.WriteLine(s)
      end

    Tomas Petricek, C# MVP
    http://tomasp.net/blog
  •  05-26-2008, 22:41 6017 in reply to 6013

    Re: Inheriting .NET classes

    Thanks!
View as RSS news feed in XML
Powered by Community Server, by Telligent Systems