How do you use math.add in a factory 'add' function? #3028
-
Hello! I have a custom datatype Dim that is a nominal +/- a tolerance (supports numbers, Unit, etc.). I then have a factory function for adding these types. export const createDim = factory('Dim', ['typed', 'typeOf'], ({ typed, typeOf }) => {
function Dim (nominal, tolerance) {
if (typeOf(nominal) !== typeOf(tolerance)) {
throw new Error('Both nominal and tolerance must be of the same type: either numbers or instances of Unit');
}
this.nominal = nominal
this.tolerance = tolerance
}
Dim.prototype.isDim = true
Dim.prototype.toString = function () {
return `${this.nominal} ± ${this.tolerance}`
}
typed.addType({
name: 'Dim',
test: function (x) {
return x && x.isDim === true
}
})
return Dim
})
export const createAddDim = factory('add', ['typed', 'Dim'], ({ typed, Dim, add }) => {
return typed('add', {
'Dim, Dim': function (a, b) {
return new Dim(add(a.nominal, b.nominal), add(a.tolerance, b.tolerance))
}
})
}) Currently this only works if I also import math into this js file. Because the two properties of Dim are also math types, I have to use math.add to add them. I thought I could inject 'add' into the typed function, but maybe there's a conflict there. Do I need to import math here, or is there another option to make math.add available inside the function? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 23 replies
-
Hi, you might be missing an Try this export const createAddDim = factory('add', ['typed', 'Dim', 'add'], ({ typed, Dim, add }) => { |
Beta Was this translation helpful? Give feedback.
-
You can use export const createAddDim = factory('add', ['typed', 'Dim'], ({ typed, Dim }) => {
return typed('add', {
'Dim, Dim': typed.referToSelf(self => (a, b) => {
return new Dim(self(a.nominal, b.nominal), self(a.tolerance, b.tolerance))
})
})
}) There is also an EDIT: in the example there where two occurrences of |
Beta Was this translation helpful? Give feedback.
-
For future viewers: evaluator.js: import { create, all } from "mathjs"
import * as dimsum from "./dimsum"
math.import([
dimsum.createDim,
dimsum.createAddDim
...
])
math.import({
add: math.typed('add', {
'Dim, Dim': math.addDim
})
}) dimsum.js: import { factory } from 'mathjs'
export const createDim = factory('Dim', ['typed', 'typeOf'], ({ typed, typeOf }) => {
function Dim (nominal, tolerance) {
if (typeOf(nominal) !== typeOf(tolerance)) {
throw new Error('Both nominal and tolerance must be of the same type: either numbers or instances of Unit');
}
this.nominal = nominal
this.tolerance = tolerance
}
Dim.prototype.isDim = true
Dim.prototype.toString = function () {
return `${this.nominal} ± ${this.tolerance}`
}
typed.addType({
name: 'Dim',
test: function (x) {
return x && x.isDim === true
}
})
return Dim
})
export const createAddDim = factory('addDim', ['typed', 'Dim', 'add'], ({ typed, Dim, add }) => {
return typed('addDim', {
'Dim, Dim': function (a, b) {
return new Dim(add(a.nominal, b.nominal), add(a.tolerance, b.tolerance))
}
})
}) |
Beta Was this translation helpful? Give feedback.
You can use math.add inside createAddDim, but I think the conflict is the name you are using is causing a circular reference as the name of the created function is the name of one of the dependencias.
Could you try a different name and test if addDim works as expected?