Просмотрев этот участок кода, я не вижу здесь объединений, выбранный backend зависит от os и целевой платформы сборки и определяется во время компиляции.
После вызова wio.init backend должен быть инициализирован (либо инициализация должна вернуть ошибку), а до выполнения wio.init, вызывающего backend.init(options), значения backend ещё не инициализированы и все их значения по умолчанию в этот момент undefined:
https://github.com/ypsvlq/wio/blob/dd0f273fdbbd1ced2a8e716ea3a5526a6c4d83cf/src/unix.zig#L20-L98
Не понимаю такой сильной зависимости библиотеки от синглтонов и переменных уровня контейнера, инициализированных в undefined, мне это настолько не нравится, что я, скорее всего, выбрал бы другую библиотеку.
Вместо делегирования backend библиотека wio могла бы хранить статически выбранный backend в поле обычной структуры, избегая синглтона, чей backend существует в непригодном состоянии ещё до инициализации.
Похоже на чрезмерное стремление скрыть детали из API за повсеместными глобальными переменными.
Переведено с английского·Показать оригиналFrom reading through that part of the code, I don't see any unions involved here, the selected backend depends on the os and the target that you are building for at compile time.
After wio.init is called, that backend ought to be initialized (or initialization should return an error), whereas before wio.init runs (and calls backend.init(options)), its backend values have not been initialized and their default values are all undefined at that point:
https://github.com/ypsvlq/wio/blob/dd0f273fdbbd1ced2a8e716ea3a5526a6c4d83cf/src/unix.zig#L20-L98
I'm puzzled by the library's heavy reliance on singletons and container-level variables initialized to undefined, I dislike that enough that I'd probably choose a different library.
Rather than delegate to its backend, wio could store the statically selected backend in a field of an ordinary struct, avoiding a singleton whose backend exists in an unusable state before being initialized.
It looks excessively focused on concealing details from the API by using globals everywhere.