Better HN
topnewbestaskshowjobs
Log inSign up

mo_al_

3 karma · joined Apr 17, 2021 (5y ago) · 3 submissions

Submissions

comment 4y ago · link · parent

I think that Zig will eventually add a way to constrain types.

A note about C++. Prior to C++20 concepts, you could always add constraints to templates via [SFINAE](https://en.cppreference.com/w/cpp/language/sfinae), it had the tendency to be verbose:

  template<typename T, typename = 
  enable_if_t<is_integral_v<T>>>
  T add_2(T x) {
      return x + 2;
  }

  struct Writer {
      virtual void write(const char *) = 0;
  };
  struct SubWriter: public Writer {
      virtual void write(const char *s) override {
          puts(s);
      }
  };

  template<typename T, typename = 
  enable_if_t<is_base_of_v<Writer, T>>>
  void write_to(T x, const char *msg) {
      x.write(msg);
  }

  int main() {
      printf("%d\n", add_2(4)); // works
      write_to(SubWriter{}, "Hello"); // works
  }

With C++20 concepts:

  integral auto add_2(integral auto x) {
      return x + 2;
  }

  struct Writer {
      virtual void write(const char *) = 0;
  };

  template<typename T>
  concept Writable1 = is_base_of_v<Writer, T>; // uses 
  type_traits

  template<typename T>
  concept Writable2 = requires (T t) { // uses compile time 
  requirements, similar to Go interfaces
      { t.write("Hello") };
  };

  struct SubWriter1: public Writer { // satisfies Writable1 
  and Writable2
      virtual void write(const char *s) override {
          puts(s);
      }
  };

  struct SubWriter2 { // satisfies only Writable2
      void write(const char *s) {
          puts(s);
      }
  };

  // accepts both SubWriter1 and SubWriter2
  void write_to(Writable2 auto x, const char \*msg) { 
      x.write(msg);
  }

  int main() {
      printf("%d\n", add_2(4)); // works
      write_to(SubWriter1{}, "Hello"); // works
  }

comment 5y ago · link · parent

fltk-rs is a Rust wrapper to FLTK (the GUI library).

(The Rust wrapper wraps a C wrapper which wraps FLTK (C++), the repo be found here: https://github.com/MoAlyousef/cfltk)

The API is quite similar to the C++ api to benefit from the existing FLTK documentation and examples. It also opts to primarily statically linking FLTK, so the executables are single binaries which can be easily deployed. During working on this, I discovered how customizable FLTK is, which basically beats my own prior assumptions that it's ugly, which it can be if no scheme is chosen or no customization is done!

3.

Show HN: Fltk-Rs 1.0.0 Released(github.com ↗)

4 points · by mo_al_ · 5y ago · 1 comment