Hot-code loading

With a Lisp you can dynamically rebind any symbol at any time, including function symbols. With Forth you can use MARKER to erase part of the dictionary and then rebuild it (with some risk to pointers into the dictionary). With Erlang you can run two versions of a module at the same time and control how processes move from old to new module. With V you can run a program with a -live flag and get more or less automatic updates of [live]-tagged functions.

In pretty much any language though, including Odin, you can achieve hot code reloading by loading dynamically linked libraries at runtime.

Linux example

Given live.odin:

package main

import "core:dynlib"
import "core:fmt"
import "core:os"
import "core:time"

LIB :: "./greet.so"

load :: proc() -> (dynlib.Library, proc()) {
	lib: dynlib.Library
	f: rawptr
	ok: bool

	lib, ok = dynlib.load_library(LIB)
	if !ok do fmt.panicf("can't load lib")
	f, ok = dynlib.symbol_address(lib, "greet")
	if !ok do fmt.panicf("can't find sym")

	return lib, cast(proc())f
}

unload :: proc(lib: dynlib.Library) {
	if !dynlib.unload_library(lib) {
		fmt.panicf("failed to unload lib")
	}
}

fresh :: proc(last: time.Time) -> (time.Time, bool) {
	stat, err := os.stat(LIB)
	if err != 0 do fmt.panicf("stat failed: %v", err)
	return stat.modification_time, time.diff(last, stat.modification_time) != 0
}

main :: proc() {
	last, _ := fresh({})
	lib, f := load()
	defer unload(lib)
	reload: bool

	for {
		last, reload = fresh(last)
		if reload {
			unload(lib)
			lib, f = load()
		}
		f()
		time.sleep(time.Millisecond * 200)
	}
}

and greet.odin:

package main

import "core:fmt"

@(export)
greet :: proc() {
	fmt.println("Hello, world?")
}

you can start running a program from live.odin, having it dynamically load greet.odin multiple times at runtime:

$ odin build greet.odin -build-mode:dll -file
$ odin run live.odin -file

Make a change to greet.odin and recompile and the changes will be reflected in a moment.

Windows example

Windows has additional requirements on a hot-code loading scheme owing to additional security measures. c.f. discord discussion, with some code.