DEV Community

Discussion on: Simple trick to instance a class without `new`.

Collapse
 
arqex profile image
Javier Marquez

Reflection is always so magic! I've used them in a couple of situations in my code, and when I come back to them, I have trouble to understand what's happening because it's too magical to remember ¯_(ツ)_/¯

Collapse
 
adam_cyclones profile image
Adam Crockett 🌀

I know thats the problem, they are not idiomatic. I tend to write libraries, lots of libraries and I always want to give the best experience for developers, usually thats why my libraries never get finished. So I do usually avoid magic, the thing is I am doing code generation via typescript and reflection and so I needed to do this stuff.

class codegen {
  @glsl drawCircle(a: float, b: float, c: vec2): float {
    return 0;
  }

  @glsl drawTri(a: float): vec2 {
    return [];
  }
}


GLR({
  el: "#app",
  coorinate: "px",
  frag(gl) {
    gl.fragColor();
  },
  vert(gl) {
    gl.uniform<vec4>("color");

    gl.main(() => {
      gl.drawCircle(1, 2, 3);
      gl.position();
    });
  },
  codegen,
  state: {
    color: rgba(0, 0, 0, 255)
  },
  watch: {
    foo: ({ value }: { value: any }) => {
      console.log("oh hey!", value);
    }
  }
});

The above code is a snippet from my abstraction over Regl, I am attempting to create a reactive webgl framework in the style of Vue.js.

Collapse
 
arqex profile image
Javier Marquez

Reactive webgl, that sounds like a great idea! I hope you share it!

I completely see the point of reflection within libraries, abstracting their "magic" from the developers that use them and offering features that weren't possible before.

I struggled when I use reflection for the business logic... I just won't do it anymore :D