Skip to content

Commit

Permalink
feat:Statically Resolved Type Parameters (#22)
Browse files Browse the repository at this point in the history
* feat:added Statically Resolved Type Parameters

* feat:contents as a list for better display in HTML

---------

Co-authored-by: ⚙︎ Greg <[email protected]>
  • Loading branch information
SpiralOSS and ⚙︎ Greg authored Oct 23, 2023
1 parent 1840d11 commit a163d18
Showing 1 changed file with 38 additions and 14 deletions.
52 changes: 38 additions & 14 deletions docs/fsharp-cheatsheet.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
This cheatsheet aims to succinctly cover the most important aspects of [F# 6.0](http://research.microsoft.com/en-us/um/cambridge/projects/fsharp/manual/spec.html).
This cheatsheet aims to succinctly cover the most important aspects of [F# 7.0](http://research.microsoft.com/en-us/um/cambridge/projects/fsharp/manual/spec.html).

The Microsoft [F# Documentation](https://learn.microsoft.com/en-us/dotnet/fsharp/) is complete and authoritative and has received a lot of love in recent years; it's well worth the time investment to read. Only after you've got the lowdown here of course ;)

If you have any comments, corrections, or suggested additions, please open an issue or send a pull request to [https://github.com/fsprojects/fsharp-cheatsheet](https://github.com/fsprojects/fsharp-cheatsheet). Questions are best addressed via the [F# slack](https://fsharp.org/guides/slack) or the [F# discord](https://discord.me/fsharp).

Contents
--------
[Comments](#Comments)
[Strings](#Strings)
[Basic Types and Literals](#BasicTypesAndLiterals)
[Functions](#Functions)
[Pattern Matching](#PatternMatching)
[Collections](#Collections)
[Tuples and Records](#TuplesAndRecords)
[Discriminated Unions](#DiscriminatedUnions)
[Exceptions](#Exceptions)
[Classes and Inheritance](#ClassesAndInheritance)
[Interfaces and Object Expressions](#InterfacesAndObjectExpressions)
[Active Patterns](#ActivePatterns)
[Compiler Directives](#CompilerDirectives)
- [Comments](#Comments)
- [Strings](#Strings)
- [Basic Types and Literals](#BasicTypesAndLiterals)
- [Functions](#Functions)
- [Pattern Matching](#PatternMatching)
- [Collections](#Collections)
- [Tuples and Records](#TuplesAndRecords)
- [Discriminated Unions](#DiscriminatedUnions)
- [Statically Resolved Type Parameters](#StaticallyResolvedTypeParameters)
- [Exceptions](#Exceptions)
- [Classes and Inheritance](#ClassesAndInheritance)
- [Interfaces and Object Expressions](#InterfacesAndObjectExpressions)
- [Active Patterns](#ActivePatterns)
- [Compiler Directives](#CompilerDirectives)

<a name="Comments"></a>Comments
--------
Expand Down Expand Up @@ -338,6 +339,29 @@ Single-case discriminated unions are often used to create type-safe abstractions
// Use pattern matching to deconstruct single-case DU
let (Order id) = orderId

<a name="#StaticallyResolvedTypeParameters"></a>Statically Resolved Type Parameters
--------------------
A *statically resolved type parameter* is a type parameter that is replaced with an actual type at compile time instead of at run time. They are primarily useful in conjunction with member constraints.

let inline add x y = x + y
let integerAdd = add 1 2
let floatAdd = add 1.0f 2.0f // without `inline` on `add` function, this would cause a type error

####

type RequestA = { Id: string; StringValue: string }
type RequestB = { Id: string; IntValue: int }

let requestA : RequestA = { Id = "A"; StringValue = "Value" }
let requestB : RequestB = { Id = "B"; IntValue = 42 }

let inline getIdOfRequest<'t when 't : (member Id: string)> (x: 't) = x.Id

let idA = getIdOfRequest requestA // "A"
let idB = getIdOfRequest requestB // "B"

See [Statically Resolved Type Parameters (MS Learn)](https://learn.microsoft.com/en-us/dotnet/fsharp/language-reference/generics/statically-resolved-type-parameters) and [Constraints (MS Learn)](https://learn.microsoft.com/en-us/dotnet/fsharp/language-reference/generics/constraints) for more examples.

<a name="Exceptions"></a>Exceptions
----------

Expand Down

0 comments on commit a163d18

Please sign in to comment.