Special property ‣ dollar sign - chung-leong/zigar GitHub Wiki

The dollar-sign property represents the value of the object. Its main purpose is to allow you to assign a new value to the object as a whole. For example, suppose you have an instance of the following struct:

pub const Hello = struct {
    number1: i32 = 0,
    number2: i32 = 0,
    number3: i32 = 0,
};

Assignment to its $ would basically reinitialize the object:

import { Hello } from './dollar-sign-example-1.zig';

const hello = new Hello({ number1: 1000, number2: 2000 });
console.log(hello.valueOf());
hello.$ = { number3: 3000 };
console.log(hello.valueOf());
{ number1: 1000, number2: 2000, number3: 0 }
{ number1: 0, number2: 0, number3: 3000 }

The dollar sign is also the mean by which you can access the value of a standalone scalar:

pub const I32 = i32;
import { I32 } from './dollar-sign-example-2.zig';

const number = new I32(1234);
console.log(number);
console.log(number.$);
i32 {
  [Symbol(memory)]: DataView {
    byteLength: 4,
    byteOffset: 0,
    buffer: ArrayBuffer { [Uint8Contents]: <d2 04 00 00>, byteLength: 4 }
  }
}
1234

Special properites