libvaxis icon indicating copy to clipboard operation
libvaxis copied to clipboard

Fixed Type casting error & Added ListView example

Open vg006 opened this issue 1 month ago • 1 comments

Problem:

TLDR:

  • Fixed type casting issue in TextView widget, by explicit type casting using @as function.
  • Added Nix flakes and configs to setup the development environment for libvaxis
  • I noticed this issue, when building my project with the TextView Widget that contains a line, that attempts to perform type coercion between error union types.

  • Actually the problem arises, when Zig compiler sees a return type of error!u3 from the std.unicode.utf8CodepointSequenceLength(), instead of error!usize

  • Zig allows type coercion between integer types like u3 to usize, but doesn't allow coercion between error unions of type error!u3 and error!usize. Even in the presence of the catch expression, it expects the payload of type error!usize.

Error Message:

❯ zig build
install
└─ install main
   └─ compile exe main Debug native 1 errors
/<USER_HOME_PATH>/.cache/zig/p/vaxis-0.5.1-BWNV_OAsCQA13jnKNnBh97YjTP4K4YwAT-pkH-RaHlMI/src/widgets/TextView.zig:88:82: error: expected type '@typeInfo(@typeInfo(@TypeOf(unicode.utf8CodepointSequenceLength)).@"fn".return_type.?).error_union.error_set!usize', found '@typeInfo(@typeInfo(@TypeOf(unicode.utf8CodepointSequenceLength)).@"fn".return_type.?).error_union.error_set!u3'
                grapheme_start = iter.i - std.unicode.utf8CodepointSequenceLength(result.cp) catch 1;
                                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~
/<USER_HOME_PATH>/.cache/zig/p/vaxis-0.5.1-BWNV_OAsCQA13jnKNnBh97YjTP4K4YwAT-pkH-RaHlMI/src/widgets/TextView.zig:88:82: note: error union payload 'u3' cannot cast into error union payload 'usize'
referenced by:
    write: /<USER_HOME_PATH>/.cache/zig/p/vaxis-0.5.1-BWNV_OAsCQA13jnKNnBh97YjTP4K4YwAT-pkH-RaHlMI/src/widgets/TextView.zig:20:31
    write [inlined]: /nix/store/09yzmfr207ad618mcm5jdy344ih3nj8w-zig-0.15.2/lib/zig/std/Io.zig:342:27
    main: src/main.zig:33:21
    3 reference(s) hidden; use '-freference-trace=6' to see all references
error: the following command failed with 1 compilation errors:
/nix/store/09yzmfr207ad618mcm5jdy344ih3nj8w-zig-0.15.2/bin/zig build-exe -I/nix/store/zxsb4q47qhbqchf2xhwrrv357m1mg2z3-libgit2-1.9.1-dev/include -L/nix/store/v1rqcqjajvr1n7splqzgxw1ffp2c7fqq-libgit2-1.9.1-lib/lib -lgit2 -ODebug --dep giz --dep vaxis -Mroot=/<USER_HOME_PATH>/<PROJECT_DIR_PATH>/src/main.zig -Mgiz=/<USER_HOME_PATH>/<PROJECT_DIR_PATH>/src/table.zig -ODebug --dep zigimg --dep uucode -Mvaxis=/<USER_HOME_PATH>/.cache/zig/p/vaxis-0.5.1-BWNV_OAsCQA13jnKNnBh97YjTP4K4YwAT-pkH-RaHlMI/src/main.zig -ODebug --dep zigimg -Mzigimg=/<USER_HOME_PATH>/.cache/zig/p/zigimg-0.1.0-8_eo2vHnEwCIVW34Q14Ec-xUlzIoVg86-7FU2ypPtxms/zigimg.zig -ODebug --dep types.zig --dep config.zig --dep types.x.zig --dep tables --dep get.zig -Muucode=/<USER_HOME_PATH>/.cache/zig/p/uucode-0.1.0-ZZjBPj96QADXyt5sqwBJUnhaDYs_qBeeKijZvlRa0eqM/src/root.zig -ODebug --dep config.zig --dep get.zig -Mtypes.zig=/<USER_HOME_PATH>/.cache/zig/p/uucode-0.1.0-ZZjBPj96QADXyt5sqwBJUnhaDYs_qBeeKijZvlRa0eqM/src/types.zig -ODebug --dep types.zig -Mconfig.zig=/<USER_HOME_PATH>/.cache/zig/p/uucode-0.1.0-ZZjBPj96QADXyt5sqwBJUnhaDYs_qBeeKijZvlRa0eqM/src/config.zig -ODebug --dep config.x.zig -Mtypes.x.zig=/<USER_HOME_PATH>/.cache/zig/p/uucode-0.1.0-ZZjBPj96QADXyt5sqwBJUnhaDYs_qBeeKijZvlRa0eqM/src/x/types.x.zig -ODebug --dep types.zig --dep types.x.zig --dep config.zig --dep build_config -Mtables=.zig-cache/o/25e92e77a3862bb271c84448e440f19b/tables.zig -ODebug --dep types.zig --dep tables -Mget.zig=/<USER_HOME_PATH>/.cache/zig/p/uucode-0.1.0-ZZjBPj96QADXyt5sqwBJUnhaDYs_qBeeKijZvlRa0eqM/src/get.zig -ODebug --dep types.x.zig --dep types.zig --dep config.zig -Mconfig.x.zig=/<USER_HOME_PATH>/.cache/zig/p/uucode-0.1.0-ZZjBPj96QADXyt5sqwBJUnhaDYs_qBeeKijZvlRa0eqM/src/x/config.x.zig --dep types.zig --dep config.zig --dep types.x.zig --dep config.x.zig -Mbuild_config=.zig-cache/o/fd18b32249ff398bc4015853405e77cf/build_config2.zig -lc --cache-dir .zig-cache --global-cache-dir /<USER_HOME_PATH>/.cache/zig --name main --zig-lib-dir /nix/store/09yzmfr207ad618mcm5jdy344ih3nj8w-zig-0.15.2/lib/zig/ --listen=-

Build Summary: 3/6 steps succeeded; 1 failed
install transitive failure
└─ install main transitive failure
   └─ compile exe main Debug native 1 errors

error: the following build command failed with exit code 1:

Solution:

  • This problem can be resolved by explicit type casting from u3 to usize using the builtin @as() function.
  • In addition to this, I have added minimal Nix flake and config to setup the development environment for the libvaxis library.

vg006 avatar Nov 06 '25 03:11 vg006