<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Clojure Q&amp;A - Recent questions tagged interface</title>
<link>https://ask.clojure.org/index.php/tag/interface</link>
<description></description>
<item>
<title>Could deftype allow for overriding methods?</title>
<link>https://ask.clojure.org/index.php/12113/could-deftype-allow-for-overriding-methods</link>
<description>&lt;p&gt;Currently implementing a custom &lt;code&gt;java.util.Iterator&lt;/code&gt; with the usual deftype form like&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(deftype MyIterator []
  Iterator
  (hasNext [this]
    (my-custom-code))
  (next [this]
    (my-custom-code))
)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But I'd like to override the &lt;code&gt;forEachRemaining&lt;/code&gt; default method on &lt;code&gt;Iterator&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;default void forEachRemaining(Consumer&amp;lt;? super E&amp;gt; action) {
    Objects.requireNonNull(action);
    while (hasNext())
        action.accept(next());
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;... with a more performant/specialized implementation. Perhaps I would even want to implement the by-default unimplemented &lt;code&gt;remove&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;default void remove() {
    throw new UnsupportedOperationException(&quot;remove&quot;);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But deftype does not allow for overriding methods. Is this a fundamental limitation or just something that hasn't been done (probably because Clojure predates default methods in java 8)? Anyways as a language whose philosophy is to embrace the the host, this would come in highly useful for using/integrating with java interfaces.&lt;/p&gt;
</description>
<category>Java Interop</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12113/could-deftype-allow-for-overriding-methods</guid>
<pubDate>Sat, 13 Aug 2022 16:52:31 +0000</pubDate>
</item>
<item>
<title>How to call java method with interface as parameters?</title>
<link>https://ask.clojure.org/index.php/9296/how-to-call-java-method-with-interface-as-parameters</link>
<description>&lt;p&gt;Here's my sample code.&lt;br&gt;
Thanks.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;// Java code
interface IFace {
  String message();
}
class A implements IFace {
  @Override message () { return &quot;Hello A!&quot;};
}
class B implements IFace {
  @Override message () { return &quot;Hello B!&quot;};
}
class PrintHello {
  static void print (IFace msg) {
    if (msg instanceof A) { ... } else { ... }
  }
}

; Clojure code

(def a (A.))
(. PrintHello (print a))
&lt;/code&gt;&lt;/pre&gt;
</description>
<category>Java Interop</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9296/how-to-call-java-method-with-interface-as-parameters</guid>
<pubDate>Fri, 08 May 2020 04:37:31 +0000</pubDate>
</item>
<item>
<title>Replacing core interfaces with protocols</title>
<link>https://ask.clojure.org/index.php/8323/replacing-core-interfaces-with-protocols</link>
<description>&lt;p&gt;Currently &lt;code&gt;IFn&lt;/code&gt;, &lt;code&gt;IDererf&lt;/code&gt; etc implemented as interfaces in clojure but these are protocols in clojurescript.&lt;br&gt;
Are there any plans to fix this mismatch?&lt;/p&gt;
</description>
<category>Compiler</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/8323/replacing-core-interfaces-with-protocols</guid>
<pubDate>Sun, 04 Aug 2019 16:04:40 +0000</pubDate>
</item>
</channel>
</rss>