Equal
The Equal module provides a simple and convenient way to define and check for equality between two values in TypeScript.
Here are some key reasons why Effect exports an Equal module:
-
Value-Based Equality: JavaScript’s native equality operators (
===
and==
) check for equality by reference, meaning they compare objects based on their memory addresses rather than their content. This behavior can be problematic when you want to compare objects with the same values but different references. The Equal module offers a solution by allowing developers to define custom equality checks based on the values of objects. -
Custom Equality: The Equal module enables developers to implement custom equality checks for their data types and classes. This is crucial when you have specific requirements for determining when two objects should be considered equal. By implementing the
Equal
interface, developers can define their own equality logic. -
Data Integrity: In some applications, maintaining data integrity is crucial. The ability to perform value-based equality checks ensures that identical data is not duplicated within collections like sets or maps. This can lead to more efficient memory usage and more predictable behavior.
-
Predictable Behavior: The Equal module promotes more predictable behavior when comparing objects. By explicitly defining equality criteria, developers can avoid unexpected results that may occur with JavaScript’s default reference-based equality checks.
In Effect it’s advisable to stop using JavaScript’s ===
and ==
operators and instead rely on the Equal.equals
function.
This function can work with any data type that implements the Equal
trait.
Some examples of such data types include Option, Either, HashSet, and HashMap.
When you use Equal.equals
and your objects do not implement the Equal
trait, it defaults to using the ===
operator for object comparison:
1import { import Equal
Equal } from "effect"2
3const const a: {
name: string;
age: number;
}
a = { (property) name: string
name: "Alice", (property) age: number
age: 30 }4const const b: {
name: string;
age: number;
}
b = { (property) name: string
name: "Alice", (property) age: number
age: 30 }5
6namespace console
var console: Console
The `console` module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
* A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream.
* A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and
[`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module.
_**Warning**_: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for
more information.
Example using the global `console`:
```js
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
```
Example using the `Console` class:
```js
const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err
```
console.(method) Console.log(message?: any, ...optionalParams: any[]): void
Prints to `stdout` with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html)
(the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)).
```js
const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout
```
See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.
log(import Equal
Equal.function equals<{
name: string;
age: number;
}, {
name: string;
age: number;
}>(self: {
name: string;
age: number;
}, that: {
name: string;
age: number;
}): boolean (+1 overload)
equals(const a: {
name: string;
age: number;
}
a, const b: {
name: string;
age: number;
}
b)) // Output: false
In this example, a
and b
are two separate objects with the same contents. However, ===
considers them different because they occupy different memory locations. This behavior can lead to unexpected results when you want to compare values based on their content.
However, you can configure your models to ensure that Equal.equals
behaves consistently with your custom equality checks. There are two alternative approaches:
-
Implementing the
Equal
Interface: This method is useful when you need to define your custom equality check. -
Using the Data Module: For simple value equality, the Data module provides a more straightforward solution by automatically generating default implementations for
Equal
.
Let’s delve into both solutions.
To create custom equality behavior, you can implement the Equal
interface in your models. This interface extends the Hash
interface from the Hash module.
Here’s an example of implementing the Equal
interface for a Person
class:
1import { import Equal
Equal, import Hash
Hash } from "effect"2
3class class Person
Person implements import Equal
Equal.interface Equal
Equal {4 constructor(5 readonly (property) Person.id: number
id: number, // Unique identifier for each person6 readonly (property) Person.name: string
name: string,7 readonly (property) Person.age: number
age: number8 ) {}9
10 // Defines equality based on id, name, and age11 [import Equal
Equal.const symbol: typeof Equal.symbol
symbol]((parameter) that: Equal.Equal
that: import Equal
Equal.interface Equal
Equal): boolean {12 if ((parameter) that: Equal.Equal
that instanceof class Person
Person) {13 return (14 import Equal
Equal.function equals<number, number>(self: number, that: number): boolean (+1 overload)
equals(this.(property) Person.id: number
id, (parameter) that: Person
that.(property) Person.id: number
id) &&15 import Equal
Equal.function equals<string, string>(self: string, that: string): boolean (+1 overload)
equals(this.(property) Person.name: string
name, (parameter) that: Person
that.(property) Person.name: string
name) &&16 import Equal
Equal.function equals<number, number>(self: number, that: number): boolean (+1 overload)
equals(this.(property) Person.age: number
age, (parameter) that: Person
that.(property) Person.age: number
age)17 )18 }19 return false20 }21
22 // Generates a hash code based primarily on the unique id23 [import Hash
Hash.const symbol: typeof Hash.symbol
symbol](): number {24 return import Hash
Hash.const hash: <number>(self: number) => number
hash(this.(property) Person.id: number
id)25 }26}
In the above code, we define a custom equality function [Equal.symbol]
and a hash function [Hash.symbol]
for the Person
class. The Hash
interface optimizes equality checks by comparing hash values instead of the objects themselves. When you use the Equal.equals
function to compare two objects, it first checks if their hash values are equal. If not, it quickly determines that the objects are not equal, avoiding the need for a detailed property-by-property comparison.
Once you’ve implemented the Equal
interface, you can utilize the Equal.equals
function to check for equality using your custom logic. Here’s an example using the Person
class:
1import { import Equal
Equal, import Hash
Hash } from "effect"2
24 collapsed lines
3class class Person
Person implements import Equal
Equal.interface Equal
Equal {4 constructor(5 readonly (property) Person.id: number
id: number, // Unique identifier for each person6 readonly (property) Person.name: string
name: string,7 readonly (property) Person.age: number
age: number8 ) {}9
10 // Defines equality based on id, name, and age11 [import Equal
Equal.const symbol: typeof Equal.symbol
symbol]((parameter) that: Equal.Equal
that: import Equal
Equal.interface Equal
Equal): boolean {12 if ((parameter) that: Equal.Equal
that instanceof class Person
Person) {13 return (14 import Equal
Equal.function equals<number, number>(self: number, that: number): boolean (+1 overload)
equals(this.(property) Person.id: number
id, (parameter) that: Person
that.(property) Person.id: number
id) &&15 import Equal
Equal.function equals<string, string>(self: string, that: string): boolean (+1 overload)
equals(this.(property) Person.name: string
name, (parameter) that: Person
that.(property) Person.name: string
name) &&16 import Equal
Equal.function equals<number, number>(self: number, that: number): boolean (+1 overload)
equals(this.(property) Person.age: number
age, (parameter) that: Person
that.(property) Person.age: number
age)17 )18 }19 return false20 }21
22 // Generates a hash code based primarily on the unique id23 [import Hash
Hash.const symbol: typeof Hash.symbol
symbol](): number {24 return import Hash
Hash.const hash: <number>(self: number) => number
hash(this.(property) Person.id: number
id)25 }26}27
28const const alice: Person
alice = new constructor Person(id: number, name: string, age: number): Person
Person(1, "Alice", 30)29namespace console
var console: Console
The `console` module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
* A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream.
* A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and
[`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module.
_**Warning**_: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for
more information.
Example using the global `console`:
```js
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
```
Example using the `Console` class:
```js
const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err
```
console.(method) Console.log(message?: any, ...optionalParams: any[]): void
Prints to `stdout` with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html)
(the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)).
```js
const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout
```
See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.
log(import Equal
Equal.function equals<Person, Person>(self: Person, that: Person): boolean (+1 overload)
equals(const alice: Person
alice, new constructor Person(id: number, name: string, age: number): Person
Person(1, "Alice", 30)))30// Output: true31
32const const bob: Person
bob = new constructor Person(id: number, name: string, age: number): Person
Person(2, "Bob", 40)33namespace console
var console: Console
The `console` module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
* A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream.
* A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and
[`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module.
_**Warning**_: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for
more information.
Example using the global `console`:
```js
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
```
Example using the `Console` class:
```js
const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err
```
console.(method) Console.log(message?: any, ...optionalParams: any[]): void
Prints to `stdout` with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html)
(the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)).
```js
const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout
```
See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.
log(import Equal
Equal.function equals<Person, Person>(self: Person, that: Person): boolean (+1 overload)
equals(const alice: Person
alice, const bob: Person
bob))34// Output: false
In this code, the equality check returns true
when comparing alice
to a new Person
object with identical property values and false
when comparing alice
to bob
due to their differing property values.
Implementing both Equal
and Hash
can become cumbersome when all you need is straightforward value equality checks. Luckily, the Data module provides a simpler solution. It offers APIs that automatically generate default implementations for both Equal
and Hash
.
Let’s see how it works:
1import { import Equal
Equal, import Data
Data } from "effect"2
3const const alice: {
readonly name: string;
readonly age: number;
}
alice = import Data
Data.const struct: <{
name: string;
age: number;
}>(a: {
name: string;
age: number;
}) => {
readonly name: string;
readonly age: number;
}
struct({ (property) name: string
name: "Alice", (property) age: number
age: 30 })4
5const const bob: {
readonly name: string;
readonly age: number;
}
bob = import Data
Data.const struct: <{
name: string;
age: number;
}>(a: {
name: string;
age: number;
}) => {
readonly name: string;
readonly age: number;
}
struct({ (property) name: string
name: "Bob", (property) age: number
age: 40 })6
7namespace console
var console: Console
The `console` module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
* A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream.
* A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and
[`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module.
_**Warning**_: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for
more information.
Example using the global `console`:
```js
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
```
Example using the `Console` class:
```js
const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err
```
console.(method) Console.log(message?: any, ...optionalParams: any[]): void
Prints to `stdout` with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html)
(the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)).
```js
const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout
```
See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.
log(import Equal
Equal.function equals<{
readonly name: string;
readonly age: number;
}, {
readonly name: string;
readonly age: number;
}>(self: {
readonly name: string;
readonly age: number;
}, that: {
readonly name: string;
readonly age: number;
}): boolean (+1 overload)
equals(const alice: {
readonly name: string;
readonly age: number;
}
alice, const alice: {
readonly name: string;
readonly age: number;
}
alice))8// Output: true9
10namespace console
var console: Console
The `console` module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
* A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream.
* A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and
[`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module.
_**Warning**_: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for
more information.
Example using the global `console`:
```js
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
```
Example using the `Console` class:
```js
const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err
```
console.(method) Console.log(message?: any, ...optionalParams: any[]): void
Prints to `stdout` with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html)
(the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)).
```js
const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout
```
See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.
log(import Equal
Equal.function equals<{
readonly name: string;
readonly age: number;
}, {
readonly name: string;
readonly age: number;
}>(self: {
readonly name: string;
readonly age: number;
}, that: {
readonly name: string;
readonly age: number;
}): boolean (+1 overload)
equals(const alice: {
readonly name: string;
readonly age: number;
}
alice, import Data
Data.const struct: <{
name: string;
age: number;
}>(a: {
name: string;
age: number;
}) => {
readonly name: string;
readonly age: number;
}
struct({ (property) name: string
name: "Alice", (property) age: number
age: 30 })))11// Output: true12
13namespace console
var console: Console
The `console` module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
* A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream.
* A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and
[`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module.
_**Warning**_: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for
more information.
Example using the global `console`:
```js
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
```
Example using the `Console` class:
```js
const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err
```
console.(method) Console.log(message?: any, ...optionalParams: any[]): void
Prints to `stdout` with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html)
(the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)).
```js
const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout
```
See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.
log(import Equal
Equal.function equals<{
readonly name: string;
readonly age: number;
}, {
name: string;
age: number;
}>(self: {
readonly name: string;
readonly age: number;
}, that: {
name: string;
age: number;
}): boolean (+1 overload)
equals(const alice: {
readonly name: string;
readonly age: number;
}
alice, { (property) name: string
name: "Alice", (property) age: number
age: 30 }))14// Output: false15
16namespace console
var console: Console
The `console` module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
* A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream.
* A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and
[`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module.
_**Warning**_: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for
more information.
Example using the global `console`:
```js
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
```
Example using the `Console` class:
```js
const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err
```
console.(method) Console.log(message?: any, ...optionalParams: any[]): void
Prints to `stdout` with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html)
(the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)).
```js
const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout
```
See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.
log(import Equal
Equal.function equals<{
readonly name: string;
readonly age: number;
}, {
readonly name: string;
readonly age: number;
}>(self: {
readonly name: string;
readonly age: number;
}, that: {
readonly name: string;
readonly age: number;
}): boolean (+1 overload)
equals(const alice: {
readonly name: string;
readonly age: number;
}
alice, const bob: {
readonly name: string;
readonly age: number;
}
bob))17// Output: false
In this example, we use the Data.struct
function to create structured data objects and check their equality using Equal.equals
. The Data module simplifies the process by providing a default implementation for both Equal
and Hash
, allowing you to focus on comparing values without the need for explicit implementations.
The Data module isn’t limited to just structs. It can handle various data types, including tuples, arrays, and records. If you’re curious about how to leverage its full range of features, you can explore the Data module documentation.
JavaScript’s built-in Set
and Map
can be a bit tricky when it comes to checking equality:
1export const const set: Set<unknown>
set = new var Set: SetConstructor
new <unknown>(iterable?: Iterable<unknown> | null | undefined) => Set<unknown> (+1 overload)
Set()2
3const set: Set<unknown>
set.(method) Set<unknown>.add(value: unknown): Set<unknown>
Appends a new element with a specified value to the end of the Set.
add({ (property) name: string
name: "Alice", (property) age: number
age: 30 })4const set: Set<unknown>
set.(method) Set<unknown>.add(value: unknown): Set<unknown>
Appends a new element with a specified value to the end of the Set.
add({ (property) name: string
name: "Alice", (property) age: number
age: 30 })5
6namespace console
var console: Console
The `console` module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
* A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream.
* A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and
[`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module.
_**Warning**_: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for
more information.
Example using the global `console`:
```js
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
```
Example using the `Console` class:
```js
const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err
```
console.(method) Console.log(message?: any, ...optionalParams: any[]): void
Prints to `stdout` with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html)
(the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)).
```js
const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout
```
See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.
log(const set: Set<unknown>
set.(property) Set<unknown>.size: number
size) // Output: 2
Even though the two elements in the set have the same values, the set contains two elements. Why? JavaScript’s Set
checks for equality by reference, not by values.
To perform value-based equality checks, you’ll need to use the Hash*
collection types available in the effect
package. These collection types, such as HashSet and HashMap, provide support for the Equal
trait.
Let’s take a closer look at how to use HashSet
for value-based equality checks:
1import { import HashSet
HashSet, import Data
Data } from "effect"2
3const const set: HashSet.HashSet<{
readonly name: string;
readonly age: number;
}>
set = import HashSet
HashSet.const empty: <never>() => HashSet.HashSet<never>
Creates an empty `HashSet`.
empty().(method) Pipeable.pipe<HashSet.HashSet<never>, HashSet.HashSet<{
readonly name: string;
readonly age: number;
}>, HashSet.HashSet<{
readonly name: string;
readonly age: number;
}>>(this: HashSet.HashSet<...>, ab: (_: HashSet.HashSet<...>) => HashSet.HashSet<...>, bc: (_: HashSet.HashSet<...>) => HashSet.HashSet<...>): HashSet.HashSet<...> (+21 overloads)
pipe(4 import HashSet
HashSet.const add: <{
readonly name: string;
readonly age: number;
}>(value: {
readonly name: string;
readonly age: number;
}) => (self: HashSet.HashSet<{
readonly name: string;
readonly age: number;
}>) => HashSet.HashSet<...> (+1 overload)
Adds a value to the `HashSet`.
add(import Data
Data.const struct: <{
name: string;
age: number;
}>(a: {
name: string;
age: number;
}) => {
readonly name: string;
readonly age: number;
}
struct({ (property) name: string
name: "Alice", (property) age: number
age: 30 })),5 import HashSet
HashSet.const add: <{
readonly name: string;
readonly age: number;
}>(value: {
readonly name: string;
readonly age: number;
}) => (self: HashSet.HashSet<{
readonly name: string;
readonly age: number;
}>) => HashSet.HashSet<...> (+1 overload)
Adds a value to the `HashSet`.
add(import Data
Data.const struct: <{
name: string;
age: number;
}>(a: {
name: string;
age: number;
}) => {
readonly name: string;
readonly age: number;
}
struct({ (property) name: string
name: "Alice", (property) age: number
age: 30 }))6)7
8namespace console
var console: Console
The `console` module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
* A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream.
* A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and
[`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module.
_**Warning**_: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for
more information.
Example using the global `console`:
```js
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
```
Example using the `Console` class:
```js
const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err
```
console.(method) Console.log(message?: any, ...optionalParams: any[]): void
Prints to `stdout` with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html)
(the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)).
```js
const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout
```
See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.
log(import HashSet
HashSet.const size: <{
readonly name: string;
readonly age: number;
}>(self: HashSet.HashSet<{
readonly name: string;
readonly age: number;
}>) => number
Calculates the number of values in the `HashSet`.
size(const set: HashSet.HashSet<{
readonly name: string;
readonly age: number;
}>
set)) // Output: 1
When you use the HashSet
, it correctly handles value-based equality checks. In this example, even though you’re adding two objects with the same values, the HashSet
treats them as a single element.
Note: It’s crucial to use elements that implement the Equal
trait, either by implementing custom equality checks or by using the Data module. This ensures proper functionality when working with HashSet
. Without this, you’ll encounter the same behavior as the native Set
data type:
1import { import HashSet
HashSet } from "effect"2
3const const set: HashSet.HashSet<{
name: string;
age: number;
}>
set = import HashSet
HashSet.const empty: <never>() => HashSet.HashSet<never>
Creates an empty `HashSet`.
empty().(method) Pipeable.pipe<HashSet.HashSet<never>, HashSet.HashSet<{
name: string;
age: number;
}>, HashSet.HashSet<{
name: string;
age: number;
}>>(this: HashSet.HashSet<...>, ab: (_: HashSet.HashSet<never>) => HashSet.HashSet<...>, bc: (_: HashSet.HashSet<...>) => HashSet.HashSet<...>): HashSet.HashSet<...> (+21 overloads)
pipe(4 import HashSet
HashSet.const add: <{
name: string;
age: number;
}>(value: {
name: string;
age: number;
}) => (self: HashSet.HashSet<{
name: string;
age: number;
}>) => HashSet.HashSet<{
name: string;
age: number;
}> (+1 overload)
Adds a value to the `HashSet`.
add({ (property) name: string
name: "Alice", (property) age: number
age: 30 }),5 import HashSet
HashSet.const add: <{
name: string;
age: number;
}>(value: {
name: string;
age: number;
}) => (self: HashSet.HashSet<{
name: string;
age: number;
}>) => HashSet.HashSet<{
name: string;
age: number;
}> (+1 overload)
Adds a value to the `HashSet`.
add({ (property) name: string
name: "Alice", (property) age: number
age: 30 })6)7
8namespace console
var console: Console
The `console` module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
* A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream.
* A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and
[`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module.
_**Warning**_: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for
more information.
Example using the global `console`:
```js
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
```
Example using the `Console` class:
```js
const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err
```
console.(method) Console.log(message?: any, ...optionalParams: any[]): void
Prints to `stdout` with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html)
(the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)).
```js
const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout
```
See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.
log(import HashSet
HashSet.const size: <{
name: string;
age: number;
}>(self: HashSet.HashSet<{
name: string;
age: number;
}>) => number
Calculates the number of values in the `HashSet`.
size(const set: HashSet.HashSet<{
name: string;
age: number;
}>
set)) // Output: 2
In this case, without using the Data module alongside HashSet
, you’ll experience the same behavior as the native Set
data type. The set contains two elements because it checks for equality by reference, not by values.
When working with the HashMap
, you have the advantage of comparing keys by their values instead of their references. This is particularly helpful in scenarios where you want to associate values with keys based on their content.
Let’s explore this concept with a practical example:
1import { import HashMap
HashMap, import Data
Data } from "effect"2
3const const map: HashMap.HashMap<{
readonly name: string;
readonly age: number;
}, number>
map = import HashMap
HashMap.const empty: <never, never>() => HashMap.HashMap<never, never>
Creates a new `HashMap`.
empty().(method) Pipeable.pipe<HashMap.HashMap<never, never>, HashMap.HashMap<{
readonly name: string;
readonly age: number;
}, number>, HashMap.HashMap<{
readonly name: string;
readonly age: number;
}, number>>(this: HashMap.HashMap<...>, ab: (_: HashMap.HashMap<...>) => HashMap.HashMap<...>, bc: (_: HashMap.HashMap<...>) => HashMap.HashMap<...>): HashMap.HashMap<...> (+21 overloads)
pipe(4 import HashMap
HashMap.const set: <{
readonly name: string;
readonly age: number;
}, number>(key: {
readonly name: string;
readonly age: number;
}, value: number) => (self: HashMap.HashMap<{
readonly name: string;
readonly age: number;
}, number>) => HashMap.HashMap<...> (+1 overload)
Sets the specified key to the specified value using the internal hashing
function.
set(import Data
Data.const struct: <{
name: string;
age: number;
}>(a: {
name: string;
age: number;
}) => {
readonly name: string;
readonly age: number;
}
struct({ (property) name: string
name: "Alice", (property) age: number
age: 30 }), 1),5 import HashMap
HashMap.const set: <{
readonly name: string;
readonly age: number;
}, number>(key: {
readonly name: string;
readonly age: number;
}, value: number) => (self: HashMap.HashMap<{
readonly name: string;
readonly age: number;
}, number>) => HashMap.HashMap<...> (+1 overload)
Sets the specified key to the specified value using the internal hashing
function.
set(import Data
Data.const struct: <{
name: string;
age: number;
}>(a: {
name: string;
age: number;
}) => {
readonly name: string;
readonly age: number;
}
struct({ (property) name: string
name: "Alice", (property) age: number
age: 30 }), 2)6)7
8namespace console
var console: Console
The `console` module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
* A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream.
* A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and
[`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module.
_**Warning**_: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for
more information.
Example using the global `console`:
```js
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
```
Example using the `Console` class:
```js
const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err
```
console.(method) Console.log(message?: any, ...optionalParams: any[]): void
Prints to `stdout` with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html)
(the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)).
```js
const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout
```
See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.
log(import HashMap
HashMap.const size: <{
readonly name: string;
readonly age: number;
}, number>(self: HashMap.HashMap<{
readonly name: string;
readonly age: number;
}, number>) => number
Returns the number of entries within the `HashMap`.
size(const map: HashMap.HashMap<{
readonly name: string;
readonly age: number;
}, number>
map)) // Output: 19
10namespace console
var console: Console
The `console` module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
* A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream.
* A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and
[`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module.
_**Warning**_: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for
more information.
Example using the global `console`:
```js
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
```
Example using the `Console` class:
```js
const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err
```
console.(method) Console.log(message?: any, ...optionalParams: any[]): void
Prints to `stdout` with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html)
(the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)).
```js
const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout
```
See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.
log(import HashMap
HashMap.const get: <{
readonly name: string;
readonly age: number;
}, number, {
readonly name: string;
readonly age: number;
}>(self: HashMap.HashMap<{
readonly name: string;
readonly age: number;
}, number>, key: {
...;
}) => Option<...> (+1 overload)
Safely lookup the value for the specified key in the `HashMap` using the
internal hashing function.
get(const map: HashMap.HashMap<{
readonly name: string;
readonly age: number;
}, number>
map, import Data
Data.const struct: <{
name: string;
age: number;
}>(a: {
name: string;
age: number;
}) => {
readonly name: string;
readonly age: number;
}
struct({ (property) name: string
name: "Alice", (property) age: number
age: 30 })))11/*12Output:13{14 _id: "Option",15 _tag: "Some",16 value: 217}18*/
In this code snippet, we use the HashMap
data structure to create a map where keys are objects created using Data.struct
. These objects have the same values, which would typically result in multiple entries in a traditional JavaScript map.
However, with HashMap
, the keys are compared by their values rather than their memory references. As a result, even though we add two objects with identical content as keys, the map correctly handles them as a single key-value pair.
To retrieve a value associated with a specific key, we can use HashMap.get
. In this example, when we query the map with an object having the same values as the key, it returns the associated value, which is 2
.