Add structural comparison

This commit is contained in:
Andrew Morris
2023-06-29 11:05:12 +10:00
parent b2bb618c52
commit 05efdfd517
5 changed files with 206 additions and 10 deletions

View File

@@ -11,6 +11,7 @@ export const orderedFiles = [
"/tutorial/revertOnCatch.ts",
"/tutorial/strings.ts",
"/tutorial/const.ts",
"/tutorial/structuralComparison.ts",
"/tutorial/binaryTree.ts",
"/tutorial/specialFunctions.ts",
"/tutorial/treeShaking.ts",

View File

@@ -0,0 +1,22 @@
// Also due to value semantics, arrays and objects are compared on their content instead of their
// identity.
export default function () {
return vec3(-5, 7, 12) === vec3(-5, 7, 12);
// JavaScript: false
// ValueScript: true
}
function vec3(x: number, y: number, z: number) {
return { x, y, z };
}
// Caveat:
// - TypeScript will emit an error for expressions like `[a, b] === [c, d]`.
//
// This is unfortunate. It's to protect you from accidentally expecting structural comparison in JS,
// but in ValueScript, that's exactly how it *does* work.
//
// ValueScript will still happily evaluate these expressions (ValueScript doesn't use the TS
// compiler), but you might want to rewrite these expressions as `eq([a, b], [c, d])` until
// dedicated ValueScript intellisense is available.