-
-
Notifications
You must be signed in to change notification settings - Fork 669
Triple equality and unexpected result on union types #1069
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
Comments
Hi, behaviour and differences about tripple equal described in docs. |
But |
Thanks for quick reply! I'm not expert on TS/AS but wouldn't something like: class Uint32 { value: u32 };
function readU32(bytes: Uint8Array): Uint32 | null { ... } work, then as a workaround? |
Yes, that's call "boxing" so you could do something like this as workaround. But this not efficient! enum Status {
Ok = 0, // no error
ErrorDecodeInvalid = 1,
ErrorUnknown = 2,
// etc
}
let lastError = Status.Ok;
function readU32(bytes: Uint8Array): u32 {
// .... some code ....
if (ok) {
lastError = Status.Ok;
return result;
}
// some error overwise
lastError = Status.ErrorDecodeInvalid;
return 0;
}
// usage
let byte = readU32(buffer);
if (lastError != Status.Ok) {
// handle error
// reset. But this optional
lastError = Status.Ok;
} Or you could use special unsigned value if you sure it can't produced by function like |
@mpapierski Another work around (not sure your use case) is to use a signed 64-bit number and if it is negative you have an error, otherways you can cast it back to a 32-bit. Or you can do something like this: https://github.com/WebAssemblyOS/wasmos/blob/master/packages/assemblyscript/assembly/wasa/index.ts Though in that example the result class should use a singleton object instead of allocating a fresh one each time. |
@MaxGraey @willemneal thanks for replies! Of course still hoping one day uppercase numbers could became nullable. @dcodeIO Another question which I think could be related to this: I just updated Simplified code: export function readU32(bytes: Uint8Array): U32 | null {
if (bytes.length < 4) {
return null;
}
const number = <u32>load<u32>(bytes.dataStart);
return <U32>number;
} Under 0.9.1 throws following error:
It's confusing and the code looks valid (at least under 0.8.1 it worked). If it's breaking change, then what would be the new syntax for such cast? Edit: new behaviour makes it also hard to test: |
@mpapierski |
Ahh right! It seems to work now. Again, thanks for fast response. |
Also you could simplify it to: @inline
export function readU32(bytes: Uint8Array): U32 | null {
return bytes.length < 4 ? null : load<U32>(bytes.dataStart);
} but this will not work for U64, U16 end etc |
Good point!
Just noticed it:
And error:
What's the difference between U32/U16/U64 etc. They seem to be implemented in a similar fashion on the AS side (sealed unmanaged classes)?
Now it makes sense |
The uppercase variants are wrapper classes providing the implementations of |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Hello!
I'm learning AssemblyScript language and I stumbled upon very strange behavior of
===
identity operator when combined with upper case numeric types (i.e. U32) and null values.It seems like a function that return values of type
U32 | null
which are tested withfoo() === null
returns true for bothresult == 0
andresult == null;
. It seems like its impossible to differentiate those two cases andx === null
always returns true forx
values of[0, null, <U32>null, <U32>0]
etc.Example:
I have a function that reads an
U32
value from bytes ofUint8Array
type. If length of those bytes is less than 4, then function returnsnull
(byte stream is invalid), otherwise it returnsload<u32>
casted toU32
(byte stream contains a valid U32).Example usage that demonstrates the issue:
Expected behavior would be to make
result === null
afalse
when result contains zero value, buttrue
ifresult
is actually a null.If the current behavior of
===
is intended, what would be the workaround to differentiate those two separate values?The text was updated successfully, but these errors were encountered: