Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IOApp.ResourceApp #1958

Merged
merged 9 commits into from
May 30, 2021
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/js/src/main/scala/cats/effect/IOApp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ trait IOApp {
protected def runtimeConfig: unsafe.IORuntimeConfig = unsafe.IORuntimeConfig()

def run(args: List[String]): IO[ExitCode]

final def main(args: Array[String]): Unit = {
if (runtime == null) {
import unsafe.IORuntime
Expand Down
1 change: 1 addition & 0 deletions core/jvm/src/main/scala/cats/effect/IOApp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ trait IOApp {
Thread.currentThread().interrupt()
}
}

}

object IOApp {
Expand Down
52 changes: 52 additions & 0 deletions core/shared/src/main/scala/cats/effect/ResourceApp.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2020-2021 Typelevel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cats.effect

import cats.syntax.all._

trait ResourceApp { self =>
def run(args: List[String]): Resource[IO, ExitCode]

final def main(args: Array[String]): Unit = {
val ioApp = new IOApp {
override def run(args: List[String]): IO[ExitCode] =
self.run(args).use(IO.pure(_))
}

ioApp.main(args)
}
}

object ResourceApp {
trait Simple extends ResourceApp {
def run: Resource[IO, Unit]
final def run(args: List[String]): Resource[IO, ExitCode] = run.as(ExitCode.Success)
}

trait Forever { self =>
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really think it makes sense to have an ExitCode here... does it? also, you'll notice it doesn't extend ResourceApp for that reason, but also because we can't override main since it's declared final for now

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, I'm good with Unit / Any

def run(args: List[String]): Resource[IO, Unit]

final def main(args: Array[String]): Unit = {
val ioApp = new IOApp {
override def run(args: List[String]): IO[ExitCode] =
self.run(args).useForever
}

ioApp.main(args)
}
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does a ForeverSimple or Forever.Simple seem useful? It just wouldn't take any args but not sure how appealing that is if Forever doesn't need to return an ExitCode to begin with

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess, if even just for consistency with the other ones. I would certainly use it, since we usually take arguments from env vars.

}