How to hide methods in F #?

I am currently implementing the Spec framework in F # and I want to hide the Equals, GetHashCode methods, etc. in my type should, so the API will not clutter them.

I know that in C # this is done by implementing a class with this interface:

using System;
using System.ComponentModel;

public interface IFluentInterface
{
    [EditorBrowsable(EditorBrowsableState.Never)]
    bool Equals(object other);

    [EditorBrowsable(EditorBrowsableState.Never)]
    string ToString();

    [EditorBrowsable(EditorBrowsableState.Never)]
    int GetHashCode();

    [EditorBrowsable(EditorBrowsableState.Never)]
    Type GetType();
}

I tried to do the same in F #:

type IFluentInterface = interface

    [<EditorBrowsable(EditorBrowsableState.Never)>]
    abstract Equals : (obj) -> bool

    [<EditorBrowsable(EditorBrowsableState.Never)>]
    abstract ToString: unit -> string

    [<EditorBrowsable(EditorBrowsableState.Never)>]
    abstract GetHashCode: unit -> int

    [<EditorBrowsable(EditorBrowsableState.Never)>]
    abstract GetType : unit -> Type 
end

Implemented in my type:

        interface IFluentInterface with
        member x.Equals(other) = x.Equals(other)
        member x.ToString()    = x.ToString() 
        member x.GetHashCode() = x.GetHashCode()
        member x.GetType()     = x.GetType() 

but without success.

I also tried to override the methods in my type and add the attribute this way, but that didn't help either.

So the question remains, how can I clear my API?

Edit:

Thanks to the help (see below), I was able to solve my problem.

This way, .Equalsand .GetHashCodecan be hidden using [<NoEquality>] [<NoComparison>], but it will also change the semantics.

Hiding with EditorBrowsable attributes does not work.

API - - .

, FSharpSpec.

, , .

, .

...

+5
3

, , , . F #, - .NET. , "kvb", - :

type MyNum(n:int) =
  member x.Add(m) = MyNum(n+m)
  member x.Mul(m) = MyNum(n*m)

let n = new MyNum(1)
n.Add(2).Mul(10) // 'ToString' shows in the IntelliSense

:

type Num = Num of int
module MyNum =
  let create n = Num n
  let add m (Num n) = Num (m + n)
  let mul m (Num n) = Num (m * n)

MyNum.create 1 |> MyNum.add 2 |> MyNum.mul 10

MyNum., F # IntelliSense , , .

+4

http://cs.hubfs.net/forums/thread/13317.aspx

F # Equals GetHashCode ( intellisense), NoEquality NoComparison, . intellisense Obsolete CompilerMessage IsHidden = true. System.Object GetType ToString F # intellisense.

[<NoEquality; NoComparison>]
type Foo() =
    member x.Bar() = ()
    member x.Qux() = ()
    [<System.Obsolete>]
    member x.HideMe() = ()
    [<CompilerMessage("A warning message",99999,IsError=false,IsHidden=true)>]
    member x.WarnMe() = ()

let foo = new Foo()
foo.  // see intellisense here
+5

, F # . .Equals .GetHashCode , [<NoEquality>] , .

You can also mention that the F # interface rarely uses smooth interfaces, since combinators and pipelining are much more idiomatic. For example, imagine that we want to create a way to create arithmetic expressions. Instead

let x = Expressions.MakeExpr(1).Add(2).Mul(3).Add(4)

I think most F # users prefer to write

open Expressions
let x = 
  1
  |> makeExpr
  |> add 2
  |> mul 3
  |> add 4

There is no need to hide elements in this style, because expressions are passed to combinators, not calls to expression constructor methods.

+3
source

All Articles