Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for duck typing on events #31

Closed
kzu opened this issue Oct 31, 2022 · 0 comments
Closed

Add support for duck typing on events #31

kzu opened this issue Oct 31, 2022 · 0 comments
Labels
enhancement New feature or request

Comments

@kzu
Copy link
Member

kzu commented Oct 31, 2022

One of the challenges in an extensible app is the sharing of the event types that extensions can use. In strongly typed languages like C#, this usually involves referencing a library of such types somehow, and consequently, shipping and versioning become more costly.

This has never been an issue in Javascript or Typescript for example, and VSCode is a clear example where this benefit makes extensibility far simpler. Just "dot your way" into whatever event you're subscribing to, and subscribe by name.

Merq can do better, though. We could allow the ease of use of "copy-pasted event definitions" (similar to what you get by cloning a .d.ts in typescript) with some compile-time code generation to do custom conversion without incurring in any serialization costs. The event type can effectively become the event (full) name, and the type is just used to lookup a custom conversion, should it be necessary at notification time.

From the consumer and producer point of view, nothing changes from the existing API.
But now two libraries can have clones of the same event (record) definition, and notify/observe each other the same.

Initially, we should just support record types (given their typical simplicity) and primitive types for ctor args and properties.
If the feature gets broader adoption, we can expand to full deep-copy behavior.

@kzu kzu added the enhancement New feature or request label Oct 31, 2022
kzu added a commit that referenced this issue Oct 31, 2022
A new records-based generator emits an __[Type]Factory that can convert from dynamic to the target record type.

Observe now adds another Subject that can convert, and we register this subject as compatible whenever the target type doesn't equal the given event. We optimize this scenario by caching these new compatible subjects like we did before. If no converter is available, the event type will just never be invoked in this "duck typing" manner.

We offer two conversion mechanisms: the generated factory, and an explicitly declared `Create(dynamic)` method on the record type. This can allow more complex scenarios to still work even if we don't generate code for them.

Closes #31
kzu added a commit that referenced this issue Nov 1, 2022
A new records-based generator emits an __[Type]Factory that can convert from dynamic to the target record type.

Observe now adds another Subject that can convert, and we register this subject as compatible whenever the target type doesn't equal the given event. We optimize this scenario by caching these new compatible subjects like we did before. If no converter is available, the event type will just never be invoked in this "duck typing" manner.

We offer two conversion mechanisms: the generated factory, and an explicitly declared `Create(dynamic)` method on the record type. This can allow more complex scenarios to still work even if we don't generate code for them.

Closes #31
kzu added a commit that referenced this issue Nov 1, 2022
A new records-based generator emits an __[Type]Factory that can convert from dynamic to the target record type.

Observe now adds another Subject that can convert, and we register this subject as compatible whenever the target type doesn't equal the given event. We optimize this scenario by caching these new compatible subjects like we did before. If no converter is available, the event type will just never be invoked in this "duck typing" manner.

We offer two conversion mechanisms: the generated factory, and an explicitly declared `Create(dynamic)` method on the record type. This can allow more complex scenarios to still work even if we don't generate code for them.

Closes #31
kzu added a commit that referenced this issue Nov 1, 2022
A new records-based generator emits an __[Type]Factory that can convert from dynamic to the target record type.

Observe now adds another Subject that can convert, and we register this subject as compatible whenever the target type doesn't equal the given event. We optimize this scenario by caching these new compatible subjects like we did before. If no converter is available, the event type will just never be invoked in this "duck typing" manner.

We offer two conversion mechanisms: the generated factory, and an explicitly declared `Create(dynamic)` method on the record type. This can allow more complex scenarios to still work even if we don't generate code for them.

Closes #31
kzu added a commit that referenced this issue Nov 1, 2022
A new records-based generator emits an __[Type]Factory that can convert from dynamic to the target record type.

Observe now adds another Subject that can convert, and we register this subject as compatible whenever the target type doesn't equal the given event. We optimize this scenario by caching these new compatible subjects like we did before. If no converter is available, the event type will just never be invoked in this "duck typing" manner.

We offer two conversion mechanisms: the generated factory, and an explicitly declared `Create(dynamic)` method on the record type. This can allow more complex scenarios to still work even if we don't generate code for them.

Closes #31
@kzu kzu closed this as completed in ee4cfdc Nov 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant