Skip to content

feat: Introduce new parse methods for builtin value types. Deprecate XX.parseInt/parseFloat #2465

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

Merged
merged 13 commits into from
Aug 26, 2022

Conversation

MaxGraey
Copy link
Member

@MaxGraey MaxGraey commented Aug 24, 2022

New static methods:

u8.parse
i8.parse
u16.parse
i16.parse
u32.parse
i32.parse
u64.parse
i64.parse

f32.parse
f64.parse

bool.parse

Deprecated:

U8.parseInt
I8.parseInt
U16.parseInt
I16.parseInt
U32.parseInt
I32.parseInt
U64.parseInt
I64.parseInt

F32.parseInt
F64.parseInt
F32.parseFloat
F64.parseFloat

Motivation

U8.parseInt and F32.parseInt / F32.parseFloat are a tracing of Number.parseFloat and Number.parseInt which makes sense in JS due to we have on Number and can't distinguish from int and float.

In AssemblyScript we have a completely different story. So I propose to declare these methods obsolete and use simpler and clearer names for parsing instead.

It is also good correlated with Date.parse, Temporal.parse and JSON.parse

Portability compatibility

globalThis.parseFloat === Number.parseFloat and globalThis.parseInt === Number.parseInt. It's just aliasing introduced in ES2015 for better modularize. So you can just use parseFloat / parseInt is you want to suppress a deprecated warns in editor

  • I've read the contributing guidelines
  • I've added my name and email to the NOTICE file

@dcodeIO
Copy link
Member

dcodeIO commented Aug 24, 2022

To summarize what we have talked about, I think we should keep non-standard additions on the respective lowercase namespaces, here f32 etc., where similar additions are already present. Then we'd have a non-standard f32.parse for example, which can freely provide such additions, plus standards-aligned (that is: as of the Number class in JS) functionality on F32 etc. (which all mimic Number but per concrete Wasm type). A follow-up could be to add a @deprecated("some message") decorator on the F32.xy APIs that prints a message hinting at f32.parse being the better API.

@MaxGraey MaxGraey changed the title feat: Introduce U8.parse, I32.parse .. F64.parse static method. Deprecate U8.parseInt, F64.parseInt/parseFloat feat: Introduce u8.parse, i32.parse .. f64.parse methods. Deprecate U8.parseInt, F64.parseInt/parseFloat Aug 24, 2022
@MaxGraey
Copy link
Member Author

MaxGraey commented Aug 24, 2022

Yes, I added non-standard "parse" methods only for lowercase types. At the moment, the // @deprecated decorator is used. We can add a more specialized deprecated decorator from our side in the future.

@MaxGraey MaxGraey changed the title feat: Introduce u8.parse, i32.parse .. f64.parse methods. Deprecate U8.parseInt, F64.parseInt/parseFloat feat: Introduce new parse methods for builtin value types. Deprecate XX.parseInt/parseFloat Aug 25, 2022
@MaxGraey MaxGraey merged commit 12b3f35 into AssemblyScript:main Aug 26, 2022
@MaxGraey MaxGraey deleted the parse branch August 26, 2022 12:05
@trusktr
Copy link
Member

trusktr commented Aug 30, 2022

Number.parseFloat and Number.parseInt are about string types being parsed, not about the output number type. At least one of those functions has different behavior based on string inputs.

Why remove if portable mode still needs?

@MaxGraey
Copy link
Member Author

Number.parseFloat and Number.parseInt didn't remove. Just deprecated for AS only usage. Portability is a different story. If you want a portable code, you can still use these methods

@trusktr
Copy link
Member

trusktr commented Aug 30, 2022

Oh, got it. I overlooked.

@MaxGraey
Copy link
Member Author

MaxGraey commented Aug 30, 2022

Also note globalThis.parseFloat === Number.parseFloat and globalThis.parseInt === Number.parseInt. It's just aliases introduced in ES2015 for better modularize. So you can just use parseFloat / parseInt is you want to suppress a deprecated warns in editor

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

Successfully merging this pull request may close these issues.

3 participants