Тёмный

Zig in Depth: usingnamespace 

Dude the Builder
Подписаться 3,4 тыс.
Просмотров 1 тыс.
50% 1

NOTE: This series only covers Zig 0.11.0! Help me create more content like this!
www.paypal.com...
With usingnamespace, yout can mix in the declarations from one name space into another. In Zig, structs are name spaces and when you import a file or module, Zig treats that as a struct too, so that's a name space as well. This allows you to reorganize the public API of your project as you wish, while at the same time organizing your source code in logical units. As a bonus, we'll see how we can conditionally mix in declarations at compile time with usingnamespace.
The code: codeberg.org/d...
Relevant Links:
- ziglang.org/do...

Опубликовано:

 

18 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 8   
@b3owu1f
@b3owu1f 7 месяцев назад
So the usingnamespace is basically a way to bundle a variety of different types in to one to make your own code a little easier to use rather than multiple different imports? I like it, just not sure if this is something common to expect in zig source? One area I think it might be useful is for libraries.. in my case I am working on a simple library that has several struct types. Rather than importing each of those for use, it seems like I could provide a pub namespace MyLib{..} which contains all my different types as pub so that anyone importing my lib can just use MyLib.MyType1, etc? That comptime usingnamespace is very slick though.
@dudethebuilder
@dudethebuilder 7 месяцев назад
You could definitely do it with usingnamespace as you describe, but also you could expose such a library public API by dividing your code in separate files and then making those files exported modules via the build system. This is a part of Zig that's currently under heavy development, but already you can do some amazing things when it comes to creating a public interface for a library.
@vapandris
@vapandris 5 месяцев назад
Can you have conditional declaration with `pub usingnamespace if(. ..) . . .`? I tried it with a function and it works fine, however when I try adding it an extra field it doesn't seems to be added. When I try to access the field (or even at initialization), I get a compile error that the field doesn't exist. However the function declared in the same manner as the field is seen. Any idea why that would be the case?
@vapandris
@vapandris 5 месяцев назад
I managed to do a workaround like so: . . . shootTimer: switch (shipType) { .Enemy => timer.RepeateTimer, .Player => u0, } = undefined, . . . However I'm still not satisfied with this solution.
@dudethebuilder
@dudethebuilder 5 месяцев назад
Is the field marked as pub? EDIT: Whoops, if it's a field, it can't be marked pub! Sorry about that. So it seems that only pub declarations (var, const, and fn) can be accessed via usingnamespace.
@craighewetson2120
@craighewetson2120 9 месяцев назад
When it comes to that timeout function on the Request type. What happens if a variable is passed into the function and that variable's value goes from 0 to 1 etc ... and your code just calls the getTimeout method on each interation ... will you get a runtime error? or a compile time error? Basically i can imagine a scenario that the passed in value will be 0 without the compiler knowing about it ... then you will get a runtime exception?
@dudethebuilder
@dudethebuilder 9 месяцев назад
The evaluation of the usingnamespace statement happens at compile time, and since the timeout param is marked as comptime, the if condition will be evaluated at compile time only. So if the value of the timeout field at compile time was 1, the method is added, and even if the value changes at runtime, the method will already be there. This example isn't ver y realistic, and maybe that's why it can cause confusion, but normally this type of compile time method mix in is based on comptime params that are known to never change.
@craighewetson2120
@craighewetson2120 9 месяцев назад
I watched this video after comptime 1 video and did not watch the 2nd comptime video yet, so the confusion probably comes from my lack of understanding of comptime. I wrote a trivial example to see what would happen if I would use say the current timestamp ms as arg to that Request method/struct and the compiler did infact give an error saying that the value or the arg could not be known at compile time. I am impressed by the feature and language so far ... thanks for the taking the time to make the videos
Далее
Zig in Depth: defer and errdefer
17:34
Просмотров 507
Zig in Depth: Atomics
12:50
Просмотров 981
How to Build a Homemade Bike Using a Barrel
00:21
Просмотров 890 тыс.
My Neovim Dev Workflow
41:37
Просмотров 27 тыс.
Is 2024 The Year Of Zig ?
48:20
Просмотров 161 тыс.
Zig Master: Zig Project Structure & Functionality
22:27
Zig Roadmap 2023 - Andrew Kelley
43:20
Просмотров 73 тыс.
Just enough C to have fun
39:29
Просмотров 40 тыс.
They Made a Sequel to C
1:53:24
Просмотров 90 тыс.
Zig in Depth: More Allocators
16:12
Просмотров 1,5 тыс.
How to Build a Homemade Bike Using a Barrel
00:21
Просмотров 890 тыс.