-
Notifications
You must be signed in to change notification settings - Fork 9.5k
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
core(tsc): make CPUNode and NetworkNode a discriminated union #5548
Conversation
other options:
|
er, and last option, we don't do this and keep casting. Not a big deal if not :) |
I'm probably not getting it 100% but you will always need some kind of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is great! one main Q that I think would make it 💯
const CPUNode = require('../../../lib/dependency-graph/cpu-node'); // eslint-disable-line no-unused-vars | ||
const NetworkNode = require('../../../lib/dependency-graph/network-node'); // eslint-disable-line no-unused-vars | ||
|
||
/** @typedef {Node.NodeType} NodeType */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this a necessary thing, or just prefer it over Node.NodeType
everywhere?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess I prefer it over Node.NodeType
, but honestly it had become rote c/p by this file so I didn't really think of that possibility when both are needed in a file :)
@@ -5,6 +5,12 @@ | |||
*/ | |||
'use strict'; | |||
|
|||
/** | |||
* A union of all types derived from Node, allowing type check disrimination |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
discrimination :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
/** | ||
* A union of all types derived from Node, allowing type check disrimination | ||
* based on `node.type`. If a new node type is created, it should be added here. | ||
* @typedef {import('./cpu-node.js') | import('./network-node.js')} NodeType |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how would you feel about calling this one Node
and the real base class BaseNode
or something? it feels weird to say that all those methods accept a thing of type NodeType
instead of saying they expect a Node
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it feels weird to say that all those methods accept a thing of type
NodeType
instead of saying they expect aNode
it does feel weird but I couldn't think of a decent alternative name for Node
:) This suggestion sounds good, will do
yes, true, I meant non-functional as in no more functionality than what you'd already be doing to tell if something was a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
@patrickhulce this is what I mentioned a while back (in #5072). The Node type passed around becomes a union of
CPUNode | NetworkNode
, which means things inside e.g. anif (node.type !== 'network')
block thatnode
becomes aCPUNode
with no cast or anything.The downsides are
a) naming (but we can tweak) and
b) double casts of
this
within theNode
class since the compiler doesn't know thatthis
isn't an incompatible subclass ofNode
(so first cast toNode
, then cast toCPUNode | NetworkNode
.