Zig in Depth: usingnamespace

แชร์
ฝัง
  • เผยแพร่เมื่อ 27 พ.ย. 2024
  • 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...

ความคิดเห็น • 9

  • @joebuydem
    @joebuydem 2 หลายเดือนก่อน

    Super helpful video.

  • @b3owu1f
    @b3owu1f 9 หลายเดือนก่อน +1

    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  9 หลายเดือนก่อน +2

      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 8 หลายเดือนก่อน

    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 8 หลายเดือนก่อน

      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  8 หลายเดือนก่อน

      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 11 หลายเดือนก่อน +1

    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  11 หลายเดือนก่อน +3

      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 11 หลายเดือนก่อน

      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