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

fix: overload get method definition (help needed) #617

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

danielwpz
Copy link

@danielwpz danielwpz commented Jul 10, 2021

The current get method in both storage and persist map has return type V | null.
However, if a defaultValue of type V is provided, it won't return null.

I tried to fix the declaration of get method with function overloading but get an error when compiling:
ERROR TS2391: Function implementation is missing or not immediately following the declaration.
But similar code can compile if using plain typescript.

Doesn't assembly script support function overloading or I did it wrong?

if (defaultValue) {
return storage.get<V>(this._key(key), defaultValue);
}
return storage.get<V>(this._key(key));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default value in storage.get is null by default.

@willemneal
Copy link
Contributor

AS currently doesn't support overloading (though I've been thinking of looking into myself as I have come across a lot of places that could use it). The reason that the overloading work stalled is because (especially early in the project) the desire to stay close to TS's syntax was very important.

I agree it's annoying but for now we can either add another method getWithDefault that isn't nullable, or at the call site you can cast it. E.g.

let x = map.get<string>("hello", "world") as string

Note you can also use ! like typescript but while in TS this informs the compiler to make it non-null, it's a runtime check for AS.

```ts
let x = map.get<string>("hello", "world")! // Runtime check in AS that will abort if null.

@danielwpz
Copy link
Author

@willemneal thanks for the quick reply! Yeah when I came across this issue I did add an ! mark to explicitly tell the compiler the return value won't be null.
Add a getWithDefault method is definitely a solution, but i'm still wondering if we have a plan to support method overloading in AS? Since it has been in typescirpt for a while and is actually very useful 😎

@willemneal
Copy link
Contributor

@danielwpz You could make a comment here AssemblyScript/assemblyscript#816

It's been a goal for sometime but no one has taken the time to figure it out and still make ts happy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants