Skip to content

Commit

Permalink
Remove "use" todo
Browse files Browse the repository at this point in the history
I tried unpacking results with use. It is, perhaps, a little tidier than the
pipeline syntax:

```
fn write_program(program: program.CompiledProgram) -> Result(Nil, errors.Error) {
  let build_directory = program.build_directory(program.base_directory)
  let source_directory = program.source_directory(program.base_directory)
  use _ <- result.try(output.delete(build_directory))
  use _ <- result.try(output.create_directory(build_directory))
  use _ <- result.try(output.write_prelude_file(build_directory))
  use _ <- result.try(output.write_py_main(build_directory, program.main_module))
  use _ <- result.try(output.copy_externals(
    build_directory,
    source_directory,
    program.external_import_files |> set.to_list,
  ))
  use state, name, module <- dict.fold(program.modules, Ok(Nil))
  {
    use _ <- result.try(state)
    build_directory
    |> filepath.join(name)
    |> output.replace_extension()
    |> output.write(module, _)
  }
}
```

Pros of use syntax:
* Each `use _ <- result.try` is slightly shorter than `|> result.try(fn(_) {...`
* Total line count is marginally smaller and there is less syntax
  overall

Cons:
* I had to manually *add back* indentation to make it clear what was
  happening in the `dict.fold` case. The curlies around the nested
  expression aren't necessary, but they make it more readable
* I have to *think* about how `use` maps to nested dedentation, compared
  to the explicitness of the pipeline syntax
* Even with the extra curlies, it's not as clear to me that the
  `dict.fold` "pseudo-callback" is called multiple times, but the
  result.try ones are only called once.

Overall, I think the pipeline syntax is marginally easier to
*comprehend*, even if the use syntax is marginally easior to *read*.
  • Loading branch information
dusty-phillips committed Aug 27, 2024
1 parent ee29f5e commit 449dbf3
Showing 1 changed file with 0 additions and 1 deletion.
1 change: 0 additions & 1 deletion src/macabre.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ pub fn usage(message: String) -> Nil {
fn write_program(program: program.CompiledProgram) -> Result(Nil, errors.Error) {
let build_directory = program.build_directory(program.base_directory)
let source_directory = program.source_directory(program.base_directory)
// TODO: would use make this more pleasant?
output.delete(build_directory)
|> result.try(fn(_) { output.create_directory(build_directory) })
|> result.try(fn(_) { output.write_prelude_file(build_directory) })
Expand Down

0 comments on commit 449dbf3

Please sign in to comment.