<div dir="ltr">Ok,<div>3 getSingleton methods, one for each case, synchronized.</div><div>First one wins. Subsequent ones throw an exception, since you can't have more than one.</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Mar 12, 2019 at 2:28 PM Mark Evenson <<a href="mailto:evenson@panix.com">evenson@panix.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><br>
<br>
> On Mar 11, 2019, at 21:47, Alan Ruttenberg <<a href="mailto:alanruttenberg@gmail.com" target="_blank">alanruttenberg@gmail.com</a>> wrote:<br>
> <br>
> I haven't looked at this code before, but in looking now, I am confused.<br>
> Interpreter is intended to be a singleton, right? (that's what the comment says, as well as what is suggested by the use of the static interpreter variable.).<br>
> In that case why would you want to createInstance to ever return null? <br>
> Why isn't there just a single synchronized getSingleton() (call it getInterpreter if you want) method, which either returns returns the existing interpreter, or creates and initializes a new one and returns it.<br>
<br>
My current take on the code is that createInstance() returns null due to there<br>
being three different code paths for initialization, so the “getInstance()<br>
calls create” can’t be wrapped in a single method.<br>
<br>
The three different kinds of creation of the interpreter are:<br>
<br>
  1) createInstance() for “embedded" uses<br>
  2) createDefaultInstance() for bringing up the REPL<br>
  3) createJlispInstance() for use in the J editor <br>
<br>
Each initialization path populates the static reference for the Interpreter<br>
singleton, and then proceeds to mutate the necessary state for its purposes.<br>
As such, the mere existence of the interpreter reference does not mean that all<br>
the necessary mutation has completed.  In lieu of a proper semantic for having<br>
finished creation, each code path starts by checking to see if there is a value<br>
for interpreter, punting with a null value if this is not the case with the<br>
following stanza:<br>
<br>
        if (interpreter != null)<br>
            return null;<br>
<br>
This is all very suboptimal from a user perspective, as getting a null from a call to<br>
create expecting the caller to handle the error is just wrong.  <br>
<br>
Still thinking about how to simplify this…<br>
<br>
<br>
<br>
-- <br>
"A screaming comes across the sky.  It has happened before but there is nothing <br>
to compare to it now."<br>
<br>
<br>
<br>
<br>
<br>
<br>
</blockquote></div>