Note that there are some explanatory texts on larger screens.

plurals
  1. POScala: How can I create a function that has multiple possible return types? (given this edge case inside)
    primarykey
    data
    text
    <p>I am using Akka and scala. And so I am calling either system.actorOf or context.actorOf in my code. However, I am wrapping Akka's library for actor creation so I can add certain functionality. So, the issue arises with how to allow my higher-level abstractions to work in either context. Whether they are called within an environment where system is available (in test code and at the app's kernel) or if they are called within an actor (and only have access to the context variable).</p> <p>Getting this slapped together and working is easy. Can just make a function that takes both context and system implicitly and returns an ActorRef based on whichever is available. However, to weed out even more duplication I've run into a problem.</p> <p>There are many other places I'd want to access whichever is in scope (either context or system) automatically. For example, if I want to call system.config or context.system.config, I need to create yet another wrapper function for just that situation. </p> <p>I'd like to be able to do something like this:</p> <pre><code>appropriate.actorOf(Props[Whatever], name = "breakfast") </code></pre> <p>and the appropriate function returns whichever is available (either actor or system) and then I'd use this function in every place where there is a switch case between the two.</p> <p>The problem that I run into is that if I return Either[ActorContext, ActorSystem] from a function, I get an error that actorOf is not available on that type. So, it's checking it based on the type, not the fact that it's available on either type no matter which is returned.</p> <p>Is there a way to do what I am attempting?</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.
    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