Using Odin for Scripts and Simple Utilities

What are your thoughts on using Odin for simple scripts and utilities?

Why Odin?

Odin may not replace very simple shell scripts but it can certainly replace languages like Python, Golang, etc.

Odin has the following features that makes it suitable:

  • Easy and simple to learn, write, read and understand.
  • Can be written as a single file or a folder (package) of files.
  • Has so many things built-in, including vendor packages (even more than Go and Python).
  • Can be written like a High Level Language.
  • Low Level control when needed.

I can write simple scripts and also simple utilities in Odin. When I need to add more features to my scripts or utilities, I don’t need to rewrite it in another language, which is often the case for programs written in shell scripts or Python.

Why not Python?

For very simple scripts I use shell scripts. But for other not-so-simple scripts, I use Python.

But:

  • Odin has so many things built-in compared to Python.
  • Odin is fast. Python has extremely slow loops.
  • Odin can be compiled to a single binary, which makes it ultra-fast to start the program. This matters on my low-tier laptop when many other programs are running.
  • Odin has other nice features, see below the Golang section.

Why not Golang?

Golang is a good replacement for Python, and can be compiled to a single binary if I want to. But I am not used to Go (I know it is easy to pick up).

But the main reasons are:

  • Odin has so many vendored packages compared to Go.
  • Odin has some nice things such as:
    • Concise error handling.
    • Useful data types such as actual Enums, Bit Sets, etc.
    • Ergonomic and powerful switch statement.
    • Useful commonly used functions such as printfln(), eprintfln(), etc.
    • Better type annotation syntax.
      (Go’s type annotation lacks a separator symbol like colon (:), which is hard to read for complex type annotations).
    • And many other features that are found to be useful.

Why not C?

I also sometimes use C instead of Python to gain experience in C programming. I started using C for small scripts because of the video in Ginger Bill’s article:
The Video That Inspired Me To Create Odin

But the problem with C is that I need to write many things from scratch. I like this if I have time, but if I want something quick then I need to use shell scripts or Python.

Instead, I can also use Odin.

Example

I wanted an Eye Dropper tool that can pick color using mouse from anywhere on the screen. The existing Eye Dropper tools are written using toolkits such as GTK. This makes it slow to open the Eye Dropper tool on my slow computer, especially when I have other programs such as InkScape, Web Browser, etc. are running. My computer has 4GB Ram, Intel i3 processor and a Hard Disk (not SSD). I want the Eye Dropper tool to open instantly when pressing a global keyboard shortcut.

I decided to write a simple Eye Dropper for X11 in Linux that outputs the color to stdout, which can then be copied to clipboard. It is easy to write in C. But I decided to write it in Odin instead. I used the Xlib library binding, which is already included in Odin as a vendor package.

When I wanted a new feature, extending the program was also easier.

2 Likes

I also wrote a shell script called odinx, which can be used as a Shebang line in an Odin file. This allows running an Odin file as an executable on Linux.

hello.odin file with a shebang line:

#!/usr/bin/env odinx

package main

import "core:fmt"

main :: proc() {
	fmt.println("Hello, World!")
}

Surprisingly, Odin supports shebang lines.

I can run this file by running ./hello.odin. The only downside is that Odin
compiler requires that the source file contains .odin file extension. Does
anyone have a solution to this?


odinx is a simple shell program:

#!/bin/sh

odin run -file "${1}"

Put odinx in ~/.local/bin or any other directory in PATH.
Then, make both odinx and hello.odin executable using chmod.

chmod u+x odinx
chmod u+x hello.odin
2 Likes

Could you provide more examples where you use Odin for scripts and simple utilities? What type of work do you usually do?

I wonder whether I miss something.

For my scripts I use Janet, and I think it is comparable to Python from your comparison. Janet has good speed for me, starts pretty instantly on my laptop. I don’t know whether loops are slow or fast, for me it computes thousands of loops under a second.

My needs are pretty high-level, I don’t need any system stuff usually, my use cases include: replace shell scripting (backup all the things, upload somewhere), ad-hoc string/file processing (get an html document, extract all links, download them to separate files; or parse a bunch of files and generate a SQL script from them). I imagine Odin wouldn’t make these things simpler.

Normally I need a lot of string manipulation in little scripts. I feel odin is too cumbersome when doing that (even with tprintf or string_builder). So I stick to python with these tasks.

It has not been much time since I started using Odin, so I don’t have many examples to provide.

One example is when I wrote an Odin script to manage versions of manually downloaded programs on Linux by using symlinks. I can do this easily with a Shell Script, but I want to expand the script to handle downloading, finding the available versions, installing and uninstalling, etc. while being able to manage the versions manually without the script when I need to fix something.

So, my usecases include simple utitlity scripts that can be later expanded into an application when more features are needed.

My main reason is “the Joy of Programming” in Odin.
This is because:

  • Useful language constructs/features.
  • Batteries-included (especially the vendor packages).
  • Easy build system.
  • Can use it like Low Level or High Level Language.
  • SIMPLICITY

Another IMPORTANT feature is that I can expand the features and turn the script into an application much more easily if it is already written in Odin. Many programs that are written using Shell scripts or Python are later rewritten in other general purpose languages (especially to those that can be compiled into single small binary).

Using scripting languages for programs other than simple scripts is not usually a good experience. When people started to write entire applications using JavaScript, creating TypeScript was necessary for a good developer experience. Even TypeScript compiler is now rewritten in Go.

Also, when installing some tool like stow that is written in Perl, I also need to install Perl. To use the tool AsciiDoctor that is written in Ruby, I need to install Ruby. Then there are tools that are written in JavaScript that require NodeJS and npm (especially Language Servers). The list goes on.

Now compare this to a single binary that can be distributed. I can still ‘technically’ bundle scripting languages into a binary, but it is bloated.

Janet

This is the first time I am hearing about Janet programming language. This is the one you are talking about, right?

I also like Lisp, but never used it for anything other than when learning Common Lisp. Maybe I will try Janet for small scripts, and also to embed it like Lua in an Odin program, especially since it has much better core library than Lua and can be compiled to statically-linked native binaries. Distributing dynamically linked binaries on Linux is not a good experience.

The features of Janet amaze me Features of Janet.

If it is really good as it is stated and becomes popular, maybe we could create a proposal to add it to the Odin vendor collection like Lua.

Thank you for introducing me to Janet.

1 Like