2020-05-11 06:09:48 +03:00
|
|
|
;; Test Interfaces
|
|
|
|
|
|
|
|
(load "Test.carp")
|
|
|
|
(use Test)
|
|
|
|
|
|
|
|
(definterface foo (Fn [a] Int))
|
|
|
|
|
|
|
|
;; A module implements an interface using implements.
|
|
|
|
;; Implementations don't need to share names with interfaces.
|
|
|
|
(defmodule A
|
|
|
|
(defn bar [x] x)
|
|
|
|
(implements foo A.bar))
|
|
|
|
|
|
|
|
;; Implementations may be declared before definitions
|
|
|
|
;; like `doc`, the name is relative to the module environment
|
|
|
|
(defmodule B
|
|
|
|
(implements foo baz)
|
|
|
|
(defn baz [y] (if y 5 0)))
|
|
|
|
|
|
|
|
;; Interfaces may be implemented retroactively
|
|
|
|
;; global functions can also implement interfaces.
|
2020-06-02 06:39:19 +03:00
|
|
|
(sig gojira (Fn [&String] String))
|
2020-05-11 06:09:48 +03:00
|
|
|
(defn gojira [s] @s)
|
|
|
|
(implements monster gojira)
|
|
|
|
|
|
|
|
(definterface monster (Fn [a] String))
|
|
|
|
|
|
|
|
;; An interface name can be used as a default implementation
|
|
|
|
(defn monster [scary?] (if scary? @"RAWR" @"meow"))
|
|
|
|
(implements monster monster)
|
|
|
|
|
2020-12-24 00:24:52 +03:00
|
|
|
;; If multiple implementations of the same concrete type are provided,
|
|
|
|
;; one overwrites the other.
|
|
|
|
(defn laugh-monster [times] (String.repeat times "LOL"))
|
|
|
|
(implements monster laugh-monster)
|
|
|
|
(defn pikachu [times] (String.repeat times "pika"))
|
|
|
|
(implements monster pikachu)
|
|
|
|
|
2020-05-11 06:09:48 +03:00
|
|
|
(deftest test
|
|
|
|
(assert-equal test
|
|
|
|
&2
|
|
|
|
&(foo 2) ;; A.foo
|
|
|
|
"Implements works as expected.")
|
|
|
|
(assert-equal test
|
|
|
|
&5
|
|
|
|
&(foo true) ;; B.foo
|
|
|
|
"Implementations can be declared before definitions.")
|
|
|
|
(assert-equal test
|
|
|
|
"SKRYEEE"
|
|
|
|
&(monster "SKRYEEE")
|
|
|
|
"Interfaces can be implemented retroactively.")
|
|
|
|
(assert-equal test
|
|
|
|
"meow"
|
|
|
|
&(monster false)
|
|
|
|
"Implementations may be global, and an implementation with the same name may
|
|
|
|
be used as a default.")
|
2020-12-24 00:24:52 +03:00
|
|
|
(assert-equal test
|
|
|
|
"pikapikapika"
|
|
|
|
&(monster 3)
|
|
|
|
"Implementations may be overwritten, when multiple implementations of the same type
|
|
|
|
are provided.")
|
2020-05-11 06:09:48 +03:00
|
|
|
)
|