Are there any static duck-typed languages?

前端 未结 15 1027
我寻月下人不归
我寻月下人不归 2020-12-14 10:12

Can I specify interfaces when I declare a member?

After thinking about this question for a while, it occurred to me that a static-duck-typed language might actually

15条回答
  •  感情败类
    2020-12-14 10:23

    F# supports static duck typing, though with a catch: you have to use member constraints. Details are available in this blog entry.

    Example from the cited blog:

    let inline speak (a: ^a) =
        let x = (^a : (member speak: unit -> string) (a))
        printfn "It said: %s" x
        let y = (^a : (member talk: unit -> string) (a))
        printfn "Then it said %s" y
    
    type duck() =
        member x.speak() = "quack"
        member x.talk() = "quackity quack"
    type dog() =
        member x.speak() = "woof"
        member x.talk() = "arrrr"
    
    let x = new duck()
    let y = new dog()
    speak x
    speak y
    

提交回复
热议问题