stampit-org / stamp

Stamps - better OOP model
https://stampit.js.org
MIT License
25 stars 3 forks source link

Using the Flow with stamps #25

Open danielkcz opened 7 years ago

danielkcz commented 7 years ago

I am really curious what could be done about this to make the whole experience less painful. I am still rather a rookie user of the Flow so it's possible I am just missing some obvious path. Let's consider some very basic stamp like this.

const SomeStamp = stampit.compose({
  methods: {
    usefulMethod(input: string): string {
      return input.toLowercase()
    }
  }
})

It would be really lovely to be able to extract type definition out of this so I can do the following and know that passed object is really an instance of that stamp and I can safely use its method without checking manually.

function(proxy: SomeStamp) {
  proxy.usefulMethod('BOOM')
}

Right now to achieve something like that I have to essentially duplicate all signatures to a separate interface type.

interface SomeStamp {
    usefulMethod(input: string): string
}

However, that's rather tedious and not that useful in the end. I am aware that C++ has header files that are kinda similar, but there is at least a compiler that can scream when something is out of sync.

Any ideas what about can be done about this? I am certain it would bring stamps to another level of existence. It gets even more powerful with composing a multiple number of small stamps together.

danielkcz commented 7 years ago

Hm, no one has an idea how to approach this? Is that perhaps disadvantage of stamps?

koresar commented 7 years ago

Flow/TS/else require support from the tooling. By tooling I mean IDEs and/or transpilers.

Just like classes - IDEs know how to highlight and lint classes. But they don't know how to lint stamps. E.g. Atom would require a plugin to highlight syntax and do typeahead.

Flow and TS are not pluginable AFAIK. We can't teach them to type check stamps. (Am I right?) But eslint is pluignable. Something like eslint-plugin-stamp would do. :)

danielkcz commented 7 years ago

That's interesting. Didn't thought about it this way before. Actually, the flow-runtime seems to be a good candidate for this. There is a babel-plugin-flow-runtime which can transform Flow annotations to a runtime code. Same thing could be theoretically done for stamps.

Sadly this would require someone more familiar with AST and stuff. I still haven't been able to wrap my head around it. Everything I've seen looks too messy for my taste.

koresar commented 7 years ago

Looks trivial to me to fork this https://github.com/codemix/flow-runtime/tree/master/packages/flow-runtime-mobx Rename mobx to stamp. And just do "obj instanceof Stamp".

On Sun, 7 May 2017, 21:45 Daniel K. notifications@github.com wrote:

That's interesting. Didn't thought about it this way before. Actually, the flow-runtime seems to be a good candidate for this. There is a babel-plugin-flow-runtime https://github.com/codemix/flow-runtime/tree/master/packages/babel-plugin-flow-runtime which can transform Flow annotations to a runtime code. Same thing could be theoretically done for stamps.

Sadly this would require someone more familiar with AST and stuff. I still haven't been able to wrap my head around it. Everything I've seen looks too messy for my taste.

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/stampit-org/stamp/issues/25#issuecomment-299700723, or mute the thread https://github.com/notifications/unsubscribe-auth/ABjCL4XJcuTmYcmKiR1TX8jkey5GzeiDks5r3a7cgaJpZM4NLjeV .

danielkcz commented 7 years ago

Uhh, what? :) Stamps don't work with instanceOf or do they now? I am not even sure what that would solve. The plugin would need to essentially add dynamic annotations based on the descriptor before it spits out to babel plugin to do a transformation.

koresar commented 7 years ago

Sorry. I meant "obj isinstanceof Stamp.compose.methods"

PS Going to bed now. Cheers

On Sun, 7 May 2017, 21:58 Daniel K. notifications@github.com wrote:

Uhh, what? :) Stamps don't work with instanceOf or do they now? I am not even sure what that would solve. The plugin would need to essentially add dynamic annotations based on the descriptor before it spits out to babel plugin to do a transformation.

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/stampit-org/stamp/issues/25#issuecomment-299701500, or mute the thread https://github.com/notifications/unsubscribe-auth/ABjCL-S-uYAmPRLFq0N3K0VyzUNO28bJks5r3bHegaJpZM4NLjeV .

koresar commented 6 years ago

@FredyC JFYI instanceof can be implemented as a utility stamp. Here, I wrote one: https://www.npmjs.com/package/@stamp/instanceof