If you're reading this, we've just migrated servers! If anything looks broken please email dion@thinkmoult.com :)

Q: Javascript

Hi all, anyone is familiar with JS?

I have these Types:

/**
 * Standard types
 */
export const Types = {
  Number: createType({
    name: "Number",
    default: 0,
    copy: copyValue,
    clone: cloneValue
  }),

  Boolean: createType({
    name: "Boolean",
    default: false,
    copy: copyValue,
    clone: cloneValue
  }),

  String: createType({
    name: "String",
    default: "",
    copy: copyValue,
    clone: cloneValue
  }),

  Array: createType({
    name: "Array",
    default: [],
    copy: copyArray,
    clone: cloneArray
  }),

  Ref: createType({
    name: "Ref",
    default: undefined,
    copy: copyValue,
    clone: cloneValue
  }),

  JSON: createType({
    name: "JSON",
    default: null,
    copy: copyJSON,
    clone: cloneJSON
  })
};

So, how can I build Types that have units and type formation?
Like:

LengthMeasure type: Types.Real "mm" 

// How to include units in types? for instance, LengthMeasure can be meter (m), centimeter (cm), and millimeter (mm)
or
Candela/Lumen (cd/lm) or (cd/klm)
or
maxLength
or
YYYY-MM-DDThh:mm:ss

Comments

  • You need a new type with two attributes: one containing the numerical value, and another containing the unit string (ideally not a string, it should belong to a set of enumerated constants, but the principle applies).

  • edited July 2020

    Thank you @Moult
    I don't know having those two are expensive in GPU or CPU or not?

    class Accel {
      constructor(value, unit) {
        this.value = value || 0
        this.unit = unit || Units.MS
      }
      clone() {
        return new Accel(this.value, this.unit)
      }
      copy(accel) {
        this.value = accel.value
        this.unit = accel.unit
      }
    }
    

    Because for instance Modelica just uses this simple line of code in C++ to do that:

    parameter Real A= 6 "m^2"
    
Sign In or Register to comment.