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

鸿蒙版本的MMKV,double、float、int、bool现在不能返回undefined #1310

Open
HideNameUser opened this issue May 24, 2024 · 4 comments

Comments

@HideNameUser
Copy link

let defaultValue:number | undefined = undefined
MMKV.decodeDouble("KEY_NOT_FUND", defaultValue)
返回的结果为0,不是想要的默认值undefined
标题中提到的int、float也是同样的
boolean类型返回默认值为false,也无法返回undefined。
返回undefined能知道是没有存过这个值。

@lingol
Copy link
Collaborator

lingol commented May 24, 2024

Noted.

@lingol
Copy link
Collaborator

lingol commented May 24, 2024

I have taken a deeper look into this issue. It is more complicated than I thought.

The problem is that the Android interface, which many of you OHOS developers are familiar with, returns 0/false when a primitive type of value does not exist. If we declared the interface as

public decodeBool(key: string, defaultValue?: boolean): boolean | undefined;

By default, it will return undefined on non-exist values, even if you have not passed undefined as the defaultValue. This behavior doesn't agree with the Andoird/Java interface many of you have been familiar with.

Now, how about we declare it as:

public decodeBool(key: string, defaultValue?: boolean = false): boolean | undefined;

The problem is that it won't compile.

@lingol
Copy link
Collaborator

lingol commented May 25, 2024

Maybe we have to drop the undefined default value.

@lingol
Copy link
Collaborator

lingol commented May 27, 2024

I've decided that undefined will be returned on non-existent values unless a specific defaultValue is passed in.
It's fixed in 1.3.6-beta2 on OHPM.

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

No branches or pull requests

2 participants