Skip to content

$emit boxes primitive types #31

@adrienbaron

Description

@adrienbaron

When using $emit primitive types end up boxed.
For example let say we have a child component emitting an event event:

int myValue = 10;
this.$emit("event", myValue);

We then bind the event in the parent template:

<child-component @event="doSomething"/>
@JsMethod
public void doSomething(int myValue) {
    int result = myValue + 5; // This will break!
}

This would break at runtime. The reason is that $emit signature is this:

public native void $emit(String name, Object... param);

This mean that all the primitives passed to it are automatically boxed, so the correct method doSomething should be:

@JsMethod
public void doSomething(Integer myValue) {
    int result = myValue + 5; // This will work
}

This is an issue because we can see $emit as a glue between the child and the parent, this glue shouldn't change the type of the things you pass. Also developers are much more used to work with the primitives type than their boxed equivalent, so forcing to use the boxed version with $emit is not friendly for the users.

To fix the issue, we can simply use the annotation @DoNotAutobox from JsInterop to avoid auto boxing of the parameters passed to $emit. Primitives will then not be boxed when passed.
This will break code that already expects $emit to box the values, but this is worth it as it makes for a saner behaviour for the future.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions