Trim Examples

Hi, I would like to see some (more) examples of code using the new trim functionality. I did experiment a little and found it difficult to get type stability correctly. Not all that is allowed in Julia JIT is allowed in Julia AOT. Maybe some are willing to share examples. I think it will help others too. Here are two. The first is a simple extension of Julia 1.12 Highlights. If my examples are not good feel free to adjust them.

module PrintProject

function @main(ARGS)
    name::String = !isempty(ARGS) ? ARGS[1] : "world"
    println(Core.stdout, "Hello, $(name)!")
    return 0
end

end
thisuser:~/.julia/dev/PrintProject$ juliac --output-exe app_test_exe --bundle build --trim=safe --experimental ../PrintProject
[...]
thisuser:~/.julia/dev/PrintProject$ /home/thisuser/.julia/dev/PrintProject/build/bin/app_test_exe
Hello, world!
thisuser:~/.julia/dev/PrintProject$ /home/thisuser/.julia/dev/PrintProject/build/bin/app_test_exe Pete
Hello, Pete!
module SimpleCalcProject

using TOML

const version = TOML.parsefile("Project.toml")["version"]

function print_help()
    println(Core.stdout,
        """
SimpleCalcProject CLI
Usage:
  simplecalc <command> [args...]

Commands:
  add <a> <b>        Add two numbers
  sub <a> <b>        Subtract second number from first
  sum <nums...>      Sum all numbers (sum)

Options:
  -h, --help         Show this help message
  -v, --version      Show version information
""")
end

function print_version()
    println(Core.stdout, "SimpleCalcProject CLI v$(version)")
end

function parse_numbers(remaining)
    try
        tryparse.(Float64, remaining)::Vector{Float64}
    catch 
        println(Core.stdout, "Error: invalid argument(s)")
        Float64[]
    end
end

function print_calculation(cmd, numbers)
    if cmd == "add" && length(numbers) == 2
        println(Core.stdout, "Result: $(numbers[1] + numbers[2])")
    elseif cmd == "sub" && length(numbers) == 2
        println(Core.stdout, "Result: $(numbers[1] - numbers[2])")
    elseif cmd == "sum" && !isempty(numbers)
        println(Core.stdout, "Result: $(sum(numbers))")
    else
        println(Core.stdout, "Invalid command or arguments. Try --help.")
    end
end

function cli(args)

    if isempty(args)
        print_help()
        return 0
    end

    cmd = args[1]

    if cmd in ["--help", "-h"]
        print_help()
        return 0
    elseif cmd in ["--version", "-v"]
        print_version()
        return 0
    end
    
    remaining = args[2:end]
    
    numbers = parse_numbers(remaining)
    
    length(numbers) == 0 && return 1
    
    print_calculation(cmd, numbers)
    
    return 0
end

function @main(ARGS)
    return cli(ARGS)
end

end
thisuser:~/.julia/dev/SimpleCalcProject$ juliac --output-exe app_test_exe --bundle build --trim=safe --experimental ../SimpleCalcProject
[...]
thisuser:~/.julia/dev/SimpleCalcProject$ /home/thisuser/.julia/dev/SimpleCalcProject/build/bin/app_test_exe
SimpleCalcProject CLI
Usage:
  simplecalc <command> [args...]

Commands:
  add <a> <b>        Add two numbers
  sub <a> <b>        Subtract second number from first
  sum <nums...>      Sum all numbers (sum)

Options:
  -h, --help         Show this help message
  -v, --version      Show version information

thisuser:~/.julia/dev/SimpleCalcProject$ /home/thisuser/.julia/dev/SimpleCalcProject/build/bin/app_test_exe 5 5
Result: 10.0
2 Likes

Here are two somewhat documented demos


And here’s another one