Skip to content

Improve documentation for Task.Anon (or improve its functionality) #5812

@arminsumo

Description

@arminsumo

Current documentation: https://mill-build.org/mill/fundamentals/tasks.html#_anonymous_tasks


You can define anonymous tasks using the Task.Anon {...​} syntax. These are not runnable from the command-line, but can be used to share common code you find yourself repeating in Tasks and Commands.

Anonymous task’s output does not need to be JSON-serializable, their output is not cached, and they can be defined with or without arguments. Unlike Tasks or Commands, anonymous tasks can be defined anywhere and passed around any way you want, until you finally make use of them within a downstream task or command.


From this description, it is not clear why this does not work

  def taskA = Task {
    val a = 1+2
    taskB(a)() // instead, doing taskB(1+2)() does work. taskB(taskC())() also does not work.
  }

  def taskB(n: Int) = Task.Anon {
    println(Task.dest) // prints taskA.dest as expected, if there is no compilation error
    println(n)
  }

  def taskC = Task{
    1 + 2
  }

results in error
Task#apply() call cannot use val a defined within the Task{...} block

It seems like this limitation somehow defeats the purpose of Anonymous tasks as helper functions for reusable pieces of code, as it's too limiting to not use any locally defined variables. This leads to a lot of code duplication, because there doesn't seem to be a good way to split a Task into smaller pieces. It would be great if Anon task can be updated to reflect this, and also to document what the best practices are for splitting Tasks up into smaller ones when the logic is not trivial.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions