title: Using scala + akka to generate unique ID
body: <p>Let me first explain my problem a little bit. I ahve a repository which stores all the known IDs. And this repository is an actor, let call him <code>repo</code>. I definied <code>GetId(id: Id)</code> message and <code>GetIdReply(Option[Id])</code> in this actor in order to request for specific id and get reply back if it already exists or no. Pretty straight forward I'd say.</p>
  
  <p>Now I have another actor which is responsible for generating an unique ID. So I tried to implement this function like:</p>
  
  <pre><code>private def getUniqueId(): Id = {
    implicit val timeout = Timeout(500.millis)
    var id = Id(UUID.randomUUID().toString)
    val getIdReply = repo ? GetId(Id)
    getIdReply.mapTo[GetIdReply] onSuccess {
      case success =&gt;
        success.id match {
          case Some(matchedId) =&gt;
            // Generated ID already exists, let's call this function recursively
            id = getUniqueId
          case None =&gt;
            // Generated ID is OK
        }
    }
    id
  }
  </code></pre>
  
  <p>I tried to implement this as a recursion and this thing works only if the generated ID is unique on first attempt. On second and all the other attempts it doesn't work.
  So my question here is how to properly construct such a function that will "loop" until ID is really unique?</p>
  
  <p>Thx!</p>
  
  <p>Best</p>