Search code examples
scalataskscala-catszio

How to return empty Vector as a result of Task?


I have created a simple project and one of the method is (it is ZIO Task

type Task[+A] = ZIO[Any, Throwable, A]):

def findSmth(..) : Task[Either[Exception, Vector[SomeData]]] 

I would like to return an empty Vector in some cases. I tried to do it this way:

ZIO.fromFuture { implicit ctx =>
   Future.successful(Right(Seq.empty[SomeData].toVector))
}

or

ZIO.fromFuture { implicit ctx =>
   Future.successful(Right(Vector.empty))
}

But it always return None instead of empty vector (empty list). How should I refactor this code to return just an empty result (vector with no data inside), without exceptions or Nones?


Solution

  • using the apply method on the Task object,

    Task(Right(Vector.empty))