Skip to content

Commit c4b6b2f

Browse files
authored
Merge pull request #1 from wallymathieu/patch-1
Adding a section on static members
2 parents 2f83d49 + e63fae8 commit c4b6b2f

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

docs/fsharp/style-guide/component-design-guidelines.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,24 @@ let u = v * 10.0
245245

246246
This guidance corresponds to general .NET guidance for these types. However, it can be additionally important in F# coding as this will allow these types to be used in conjunction with F# functions and methods with member constraints, such as List.sumBy.
247247

248+
#### Consider using `[<CompiledName>]` to provide a .NET-friendly name for other .NET language consumers
249+
250+
Sometimes you may wish name something in one style for F# consumers (such as a static member in lower case), but have a different style for the name when it is compiled into an assembly. You can use the `[<CompiledName>]` attribute to provide a different style for non F# code consuming the assembly.
251+
252+
```fsharp
253+
type Vector(x:float, y:float) =
254+
255+
member v.X = x
256+
member v.Y = y
257+
258+
[<CompiledName("Create")>]
259+
static member create x y = Vector (x, y)
260+
261+
let v = Vector.create 5.0 3.0
262+
```
263+
264+
By using `[<CompiledName>]`, you can use .NET naming conventions for non F# consumers of the assembly.
265+
248266
#### Use method overloading for member functions, if doing so provides a simpler API
249267

250268
Method overloading is a powerful tool for simplifying an API that may need to do multiple related things.
@@ -852,4 +870,4 @@ The fixes we have made to prepare this type for use as part of a vanilla .NET li
852870

853871
* We used the .NET delegate type `System.Func` instead of an F# function type.
854872

855-
This makes it far nicer to consume in C# code.
873+
This makes it far nicer to consume in C# code.

0 commit comments

Comments
 (0)