ch 8: Maybe-missing inspect
class Maybe {
static of(x) {
return new Maybe(x);
}
get isNothing() {
return this.$value === null || this.$value === undefined;
}
constructor(x) {
this.$value = x;
}
map(fn) {
return this.isNothing ? this : Maybe.of(fn(this.$value));
}
inspect() {
return this.isNothing ? 'Nothing' : `Just(${inspect(this.$value)})`;
}
}
I know inspect is defined in appendix, but it looks misleading here as it's a solely illustration. I think Just(${this.$value}) might be just fine here.
Either way I'm receiving
(node:6857) [DEP0079] DeprecationWarning: Custom inspection function on Objects via .inspect() is deprecated
Should this be avoided due to deprecation?
Simply import the required module.
const inspect = require('util').inspect
@tarasowski I mentioned, I know inspect is defined in appendix, it just looks misleading here. The code for illustration just makes enough sense without the inspect.
Think those read the book online at gitbook may try the codes in browser, or those may want to try the code in Node on fly, by copy->paste->run. I don't think it's a good practice to make them have to check the verbose appendix to avoid the following situations:
In browser, Chrome has an built-in inspect, try Maybe.of(() => {}).inspect().
The Chrome debug tool will lead you to the definition, which is unexpected.
In Node, if you just want to copy the code and try Maybe.of(1).inspect(), you will get ReferenceError: inspect is not defined or the deprecated warnings like dephora metioned.
So, when I was reading the book and run the code in Chrome, I manully avoided these with inspect = _ => _. It's easy to avoid, but better if readers don't have to.
Trivial: const { inspect } = require('util') is better practice than const inspect = require('util').inspect as far as I'm concerned.
Why is it better?
Trivial: const { inspect } = require('util') is better practice than const inspect = require('util').inspect as far as I'm concerned.
@tarasowski Because since then when you have to require more functions, you can just add it after inspect instead of add a new line. It makes sense that contents of the same module are imported together.
const fA1 = require('moduleA').fA1;
const fA2 = require('moduleA').fA2;
const fB1 = require('moduleB').fB1;
const fB2 = require('moduleB').fB2;
// or
const A = require('moduleA');
const fA1 = A.fA1;
const fA2 = A.fA2;
// or
const A = require('moduleA');
const { fA1, fA2 } = A;
is too verbose than
const { fA1, fA2 } = require('moduleA');
const { fB2, fB2 } = require('moduleB');
It's like decisions of package contruction patterns between import { f1, f2 } from 'module' or from module import f1, f2 the ES6/Python style or import package.f the Java/Ruby style.
I prefer const { attr } = object in common statements than const attr = object.attr for even another small advantage as when you want to migrate your code to typescript and have to use any:
const attr = (obj as any).attr
// ^ ^^^^^^^^ have to add/remove brackets
const { attr } = obj as any
// ^^^^^^^ just add/remove after it
As I said, all just my point of view. And, it's quite trivial, forget it :D
I also experienced misleading of
inspect() {
return this.isNothing ? 'Nothing' : `Just(${inspect(this.$value)})`;
}
It would better to modify here. First one is method of instance, and last one is function from outside. They have the same name. Just(${this.$value}) is much clear.
Hi. Sorry for bringing this back. But I cannot find inspect anywhere. As @somarlyonks said, I copy the code and run it in node. Finding an inspect in Appendix, like this:
const inspect = (x) => { if (x && typeof x.inspect === 'function') { return x.inspect(); } }
But it doesn't work at all. Is this the right inspect? I can't require('util') because I'm in a seperate folder. The two inspect is very confusing for me. Thanks