[Dlang Episode 90] D Language - Compile-Time Programming - Part 2 of n - String Mixins

Поділитися
Вставка
  • Опубліковано 1 тра 2024
  • ►Full DLang Series Playlist: • D Language (DLang) Pro...
    ►Find full courses on: courses.mshah.io/
    ►Join as member to get perks: / @mikeshah
    ►Lesson Description: In this lesson I introduce the idea of a mixin -- specifically a 'string mixin'. This is a simple but powerful feature of the D programming language, which can be used with compile-time programming and the idea of generating code. A string mixin is nothing more than a literal 'string' that is known at compile-time that is valid D code. What makes this interesting is when you either import strings, or otherwise compose functions to generate code. String mixins are a powerful feature combined with templates to get the compiler working for you. In this lesson I'll also show you a little trick with the '-mixed' flag to help debug mixins.
    ►Please like and subscribe to help the channel!
    ►UA-cam Channel: / mikeshah
    ►Join our free community: courses.mshah.io/communities/...
  • Наука та технологія

КОМЕНТАРІ • 15

  • @stevenschveighoffer7738
    @stevenschveighoffer7738 28 днів тому +1

    2 things -- you can just mixin multiple things and they are automatically concatenated. e.g. mixin("writeln(\"", msg, "\");"); This is nice because parameters are automatically converted to strings before mixing in. This obviates the need to e.g. use to!string on things. e.g. if msg was an integer and not a string, that still works.
    Also, you can use wysiwyg strings to avoid dealing with quote escapes: mixin(`writeln("`, msg, `");`);

    • @MikeShah
      @MikeShah  28 днів тому +1

      Excellent -- will keep this pinned for folks! wysiwyg strings are a good call! Probably worth a separate video to point that out.

  • @stevenschveighoffer7738
    @stevenschveighoffer7738 28 днів тому +1

    Glad you are talking about these things, Mike, metaprogramming is the crown jewel of D!

    • @MikeShah
      @MikeShah  28 днів тому +1

      Cheers Steve -- 100% agreed!

  • @bsdooby
    @bsdooby Місяць тому +1

    A really neat feature of D; could this be used to embed binary data as well, e g., assets, etc.?

    • @disdroid
      @disdroid Місяць тому +1

      you can use "include(filename)" which is an expression - it doesnt say in the manual which types it can convert to, I've only used strings but I'm sure theres a simple way to do binary.

    • @MikeShah
      @MikeShah  Місяць тому +1

      @@disdroid I suspect you can do the same trick with binary 'mixin(import(binary_file))' and cast to a ubyte.

    • @MikeShah
      @MikeShah  Місяць тому +1

      Sorry, cast to ubyte[] (an array of bytes)

    • @bsdooby
      @bsdooby Місяць тому +2

      Thx for the hints @disdroid, @MikeShah

    • @stevenschveighoffer7738
      @stevenschveighoffer7738 28 днів тому

      @@disdroid import, not include. It does just give you bytes, but the type is string, so you just have to cast it.

  • @disdroid
    @disdroid Місяць тому +1

    string GenStruct() if( traits(compiles,".....") ) { .... }
    If you pass in invalid tokens then the error would just show that the function signature couldn't be found, instead of a nested error from inside the string.

    • @MikeShah
      @MikeShah  Місяць тому +1

      Cheers -- that's another great strategy -- perhaps better in some cases!

    • @disdroid
      @disdroid Місяць тому +1

      @@MikeShah it also allows for multiple versions of the same function name, so it's extensible. I prefer that to a single function with multiple static ifs.

    • @disdroid
      @disdroid Місяць тому +1

      and each of these strategies so far contains potential vulnerabilities - a crafted string, if a file were downloaded and included as part of the build for example, could escape from the struct and add something malicious to the mixin. so in our function declaration we could use ctRegEx to validate the input instead of traits compiles.