Skip to content

Make API accept named parameters instead of receiver lambdas when appropriate  #12

@natanfudge

Description

@natanfudge

Currently in many places the API looks like:

foo {
   param1 = 2
   param2 = "hello"
   ...
}

For example:

xaxis {
   name = "x axis name"
   mode = Mode.lines
}

The api would be easier to use if it was in this form:

xaxis = Axis(
   title = "sin"
   type= Type.`-`
)

There are a couple of upsides to this:

  1. By looking at the function signature of xaxis{} you don't know what kind of configuration is available. Sometimes the available variables are not even in the same file as the builder.
    When having Axis() accept the parameters directly, they are available immediately in the call site with their default values, and will be shown by IntelliJ when you type the constructor.
  2. With named parameters IntelliJ will nicely autocomplete all of the available parameters, unlike when using lambdas.
  3. Having a function body implies you can do something other than assign variables, which is not true in many cases.
  4. Calling the constructor of Foo is the natural way to create an object of type Foo, instead of some Foo.build{} function.

Places where passing parameters in receiver lambdas is still appropriate:

  • When additional function calls are nested, like plot in Plotly.Page{} (title should still be a named parameter though), or trace in plot.

@altavir If you agree I would like to submit a PR to start improving the API in this direction.

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureNew feature or requestwontfixThis will not be worked on

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions