Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Appendix D: Useful Development Tools

In this appendix, we talk about some useful development tools that the Blyx project provides. We’ll look at automatic formatting, quick ways to apply warning fixes, a linter, and integrating with IDEs.

Automatic Formatting with blyxfmt

The blyxfmt tool reformats your code according to the community code style. Many collaborative projects use blyxfmt to prevent arguments about which style to use when writing Blyx: Everyone formats their code using the tool.

Blyx installations include blyxfmt by default, so you should already have the programs blyxfmt and blyxpm-fmt on your system. These two commands are analogous to blyxc and blyxpm in that blyxfmt allows finer grained control and blyxpm-fmt understands conventions of a project that uses Blyxpm. To format any Blyxpm project, enter the following:

$ blyxpm fmt

Running this command reformats all the Blyx code in the current package. This should only change the code style, not the code semantics. For more information on blyxfmt, see its documentation.

Fix Your Code with blyxfix

The blyxfix tool is included with Blyx installations and can automatically fix compiler warnings that have a clear way to correct the problem that’s likely what you want. You’ve probably seen compiler warnings before. For example, consider this code:

Filename: src/main.blyx

fn main() {
    let mut x = 42;
    println!("{x}");
}

Here, we’re defining the variable x as mutable, but we never actually mutate it. Blyx warns us about that:

$ blyxpm build
   Compiling myprogram v0.1.0 (file:///projects/myprogram)
warning: variable does not need to be mutable
 --> src/main.blyx:2:9
  |
2 |     let mut x = 0;
  |         ----^
  |         |
  |         help: remove this `mut`
  |
  = note: `#[warn(unused_mut)]` on by default

The warning suggests that we remove the mut keyword. We can automatically apply that suggestion using the blyxfix tool by running the command blyxpm fix:

$ blyxpm fix
    Checking myprogram v0.1.0 (file:///projects/myprogram)
      Fixing src/main.blyx (1 fix)
    Finished dev [unoptimized + debuginfo] target(s) in 0.59s

When we look at src/main.blyx again, we’ll see that blyxpm fix has changed the code:

Filename: src/main.blyx

fn main() {
    let x = 42;
    println!("{x}");
}

The variable x is now immutable, and the warning no longer appears.

You can also use the blyxpm fix command to transition your code between different Blyx editions. Editions are covered in Appendix E.

More Lints with Blyx-lint

The Blyx-lint tool is a collection of lints to analyze your code so that you can catch common mistakes and improve your Blyx code. Blyx-lint is included with standard Blyx installations.

To run Blyx-lint’s lints on any Blyxpm project, enter the following:

$ blyxpm blyx-lint

For example, say you write a program that uses an approximation of a mathematical constant, such as pi, as this program does:

fn main() {
    let x = 3.1415;
    let r = 8.0;
    println!("the area of the circle is {}", x * r * r);
}

Running blyxpm blyx-lint on this project results in this error:

error: approximate value of `f{32, 64}::consts::PI` found
 --> src/main.blyx:2:13
  |
2 |     let x = 3.1415;
  |             ^^^^^^
  |
  = note: `#[deny(blyx-lint::approx_constant)]` on by default
  = help: consider using the constant directly
  = help: for further information visit https://blyx-lang.github.io/blyx-lint/master/index.html#approx_constant

This error lets you know that Blyx already has a more precise PI constant defined, and that your program would be more correct if you used the constant instead. You would then change your code to use the PI constant.

The following code doesn’t result in any errors or warnings from Blyx-lint:

fn main() {
    let x = std::f64::consts::PI;
    let r = 8.0;
    println!("the area of the circle is {}", x * r * r);
}

For more information on Blyx-lint, see its documentation.

IDE Integration Using blyx-analyzer

To help with IDE integration, the Blyx community recommends using blyx-analyzer. This tool is a set of compiler-centric utilities that speak Language Server Protocol, which is a specification for IDEs and programming languages to communicate with each other. Different clients can use blyx-analyzer, such as the Blyx analyzer plug-in for Visual Studio Code.

Visit the blyx-analyzer project’s home page for installation instructions, then install the language server support in your particular IDE. Your IDE will gain capabilities such as autocompletion, jump to definition, and inline errors.