Formation F#
  • Intro
  • Bases
    • Le F♯, c'est quoi ?
    • Syntaxe
    • Premiers concepts
    • 🍔 Quiz
  • Fonctions
    • Signature
    • Fonctions
    • Fonctions standard
    • Opérateurs
    • Fonctions : compléments
    • 🍔 Quiz
    • 📜 Récap’
  • Types composites
    • Généralités
    • Tuples
    • Records
    • Unions
    • Enums
    • Records anonymes
    • Types valeur
    • 🍔 Quiz
  • Types : Compléments
    • Type unit
    • Génériques
    • Types flexibles
    • Unités de mesure
    • Conversion
    • Exceptions F#
  • Pattern matching
    • Patterns
    • Match expression
    • 🚀 Active Patterns
    • 📜 Récap’
  • Collections
    • Vue d'ensemble
    • Types
    • Fonctions génériques
    • Fonctions spécifiques
    • 🍔 Quiz
    • 📜 Récap’
  • Programmation asynchrone
    • Workflow asynchrone
    • Interop avec la TPL .NET
    • 📜 Récap’
  • Types monadiques
    • Type Option
    • Type Result
    • Smart constructor
    • 🚀 Computation expression (CE)
    • 🚀 CE - Fondements théoriques
    • 📜 Récap’
  • Module & namespace
    • Vue d'ensemble
    • Namespace
    • Module
    • 🍔 Quiz
    • 📜 Récap’
  • Orienté-objet
    • Introduction
    • Membres
    • Extensions de type
    • Classe, structure
    • Interface
    • Expression objet
    • Recommandations
  • 🦚 Aller plus loin
Propulsé par GitBook
Sur cette page
  • Syntaxe
  • Implémentation
  • Dans un type
  • Dans une expression objet
  • Implémentation par défaut
  • Une interface F♯ est explicite
  • Implémenter une interface générique
  • Héritage

Cet article vous a-t-il été utile ?

Modifier sur GitHub
  1. Orienté-objet

Interface

Syntaxe

Idem classe abstraite avec :

  • Que des membres abstraits, définis par leur signature

  • Sans l'attribut [<AbstractClass>]

  • Avec l'attribut [<Interface>] de manière optionnelle

type [accessibility-modifier] interface-name =
    abstract memberN : [ argument-typesN -> ] return-typeN
  • Le nom d'une interface commence par I pour suivre la convention .NET

  • Les arguments des méthodes peuvent être nommés :

[<Interface>]
type IPrintable =
    abstract member Print : format:string -> unit

On peut aussi utiliser la syntaxe verbeuse avec un bloc interface ... end, → Non idiomatique sauf dans le cas d'une interface sans membre a.k.a marker interface.

type IMarker = interface end

Implémentation

2 manières d'implémenter une interface :

  1. Dans un type (comme en C♯)

  2. Dans une expression objet 📍

Dans un type

type IPrintable =
    abstract member Print : unit -> unit

type Range = { Min: int; Max: int } with
    interface IPrintable with
        member this.Print() = printfn $"[{this.Min}..{this.Max}]"

Dans une expression objet

type IConsole =
    abstract ReadLine : unit -> string
    abstract WriteLine : string -> unit

let console =
    { new IConsole with
        member this.ReadLine () = Console.ReadLine ()
        member this.WriteLine line = printfn "%s" line }

Implémentation par défaut

F♯ 5.0 supporte les interfaces définissant des méthodes avec implémentations par défaut écrites en C♯ 8+ mais ne permet pas de les définir.

Une interface F♯ est explicite

Implémentation d'une interface en F♯ ≡ Implémentation explicite d'une interface en C♯

→ Les méthodes de l'interface ne sont consommables que par upcasting :

type IPrintable =
    abstract member Print : unit -> unit

type Range = { Min: int; Max: int } with
    interface IPrintable with
        member this.Print() = printfn $"[{this.Min}..{this.Max}]"

let range = { Min = 1; Max = 5 }
(range :> IPrintable).Print()  // Opérateur `:>` de upcast 📍
// [1..5]

Implémenter une interface générique

type IValue<'T> =
    abstract member Get : unit -> 'T

type BiValue() =
    interface IValue<int> with
        member _.Get() = 1
    interface IValue<string> with
        member _.Get() = "hello"

let o = BiValue()
let i = (o :> IValue<int>).Get() // 1
let s = (o :> IValue<string>).Get() // "hello"

Héritage

Défini avec mot clé inherit

type Base(x: int) =
    do
        printf "Base: "
        for i in 1..x do printf "%d " i
        printfn ""

type Child(y: int) =
    inherit Base(y * 2)
    do
        printf "Child: "
        for i in 1..y do printf "%d " i
        printfn ""

let child = Child(1)

// Base: 1 2 3 4
// Child: 1
PrécédentClasse, structureSuivantExpression objet

Dernière mise à jour il y a 3 ans

Cet article vous a-t-il été utile ?

Piège : mot clé interface en F♯ ≠ mot clé interface en C♯, Java, TS ≡ mot clé implements Java, TS

Mot clé default : supporté que dans les classes, pas dans les interfaces !

⚠️
⚠️