Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Higher Order Polymorphic Function
    primarykey
    data
    text
    <p>I am very familiar with functional langauges such as Scheme, and Haskell. I'm trying to solve a problem in Java and struggling, possibly because I'm still in a functional mindset.</p> <p>I want to write:</p> <pre><code>public void doQueryAndStoreData(String query, &lt;? extends Collection&gt; storeIn) { /* make a jdbc query, get ResultSet */ ResultSet rset = ...; ProcessResultSet proc = new ProcessResultSet(); proc.process(rset, storeIn); /* clean up */ } </code></pre> <p>with an interface like:</p> <pre><code>private interface IProcessResultSet&lt;C&gt; { public void process(ResultSet rset, C storeIn); } </code></pre> <p>and a class implementing the interface like:</p> <pre><code>private class ProcessResultSet implements IProcessResultSet&lt;? extends Collection&gt; { public void process(ResultSet rset, Map storeIn) { /* do something */ } public void process(ResultSet rset, List storeIn) { /* do something else */ } } </code></pre> <p>so that the first method can call the appropriate <code>process</code> based on what type of <code>storeIn</code> it's given.</p> <p>In Haskell I could write</p> <pre><code>class Storeable c a where store :: a -&gt; c a -&gt; c a doQueryAndStoreData :: Storeable c a =&gt; ResultSet a -&gt; c a -&gt; c a doQueryAndStoreData (ResultSet rs) coll = foldr store coll rs </code></pre> <p>and provide <code>Storeable</code> instances for whatever collection type I want to store my <code>ResultSet</code> in.</p> <p>Is this the correct approach in Java? Because I feel like I'm somewhat fighting the langauge to accomplish this.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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