Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You should use the subtype <code>ActorRefFactory</code> to make this generic as both <code>ActorContext</code> and <code>ActorSystem</code> inherit from it. You can either return it from your function which figures out which type to return or you can fold the <code>Either</code> that you mentioned:</p> <pre><code> val either:Either[ActorContext, ActorSystem] = ... val refProvider = either.fold[ActorRefFactory](ac =&gt; ac, as =&gt; as) refProvider.actorOf(Props[Whatever], name = "breakfast") </code></pre> <p><em><strong>UPDATE</em></strong></p> <p>To answer your question in the comments, first, you're creating an <code>Either</code> object so you shouldn't expect to have any method of it's contents available on it. <code>Either</code> holds a left or right object and is an object itself with it's own methods. The <code>fold</code> over <code>Either</code> takes two functions, one with takes a left type and returns a new data type and another function with takes a right type and returns the same data type. By using fold, we can get a value out of the either. If you want to a more in depth look at how this works I highly recommend <a href="http://www.manning.com/bjarnason/" rel="nofollow">Functional Programming in Scala</a>.</p> <p>Additionally, you can simplify your code and remove the either all together as it's really unnecessary in your context:</p> <pre><code> def appropriateContext(implicit context: ActorContext = null, system: ActorSystem = null): ActorRefFactory = { if (context != null) context else system } </code></pre>
 

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