Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The code examples in this answer assume the following declaration:</p> <pre><code>var implementation: MyInterface; </code></pre> <h3>Providing an implementation of a callable interface</h3> <p>As a follow-up to <a href="https://stackoverflow.com/a/12770145/238753">the accepted answer</a>, as suggested by some of its commentors, a function that matches the interface's call signature implicitly implements the interface. So you can use any matching function as an implementation.</p> <p>For example:</p> <pre><code>implementation = () =&gt; "Hello"; </code></pre> <p>You don't need to explicitly specify that the function implements the interface. However, if you want to be explicit, you can use a cast:</p> <pre><code>implementation = &lt;MyInterface&gt;() =&gt; "Hello"; </code></pre> <h3>Providing a reusable implementation</h3> <p>If you want to produce a reusable implementation of the interface like you normally would with a Java or C# interface, just store the function somewhere accessible to its consumers.</p> <p>For example:</p> <pre><code>function Greet() { return "Hello"; } implementation = Greet; </code></pre> <h3>Providing a parameterised implementation</h3> <p>You may want to be able to parameterise the implementation in the same way that you might parameterise a class. Here's one way to do this:</p> <pre><code>function MakeGreeter(greeting: string) { return () =&gt; greeting; } implementation = MakeGreeter("Hello"); </code></pre> <p>If you want the result to be typed as the interface, just explicitly set the return type or cast the value being returned.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload