Namespace != object



  • OK I read up all about namespaces, and they seem nice for removing code clutter but, as far as I can see they only build a static single instance of something, and I need to build a large number of (cough) objects, that contain another large number of objects...

    I'm unclear how I would do that using namespaces...

    my psudo code:

    read 50 2D-arrays of data
    for each array create an object
    for each attribute in each object created place one of the 2nd dimensions values in turn

    This is actually pretty trivial using objects but as I say I dont know how to do it in namespaces...



  • HISE Script != OOP

    How to fake it - https://forum.hise.audio/topic/177/javascript-version/5



  • @d-healey said in Namespace != object:

    HISE Script != OOP

    How to fake it - https://forum.hise.audio/topic/177/javascript-version/5

    Well your code from there:

    namespace myClass
    {
        reg _privateVariable; //Use underscore for variables that should be private
        reg publicVariable;
    
        inline function createInstanceOfClass(id, someParameter)
        {
            local obj = {};
            
            obj.id = id;
            obj.someParameter = someParameter;
            return obj;
        }
    
        inline function changeParameter(obj, parameter, value) //Pass in the instance of the object
        {
            obj[parameter] = value;
        }
    }
    

    Is pretty similar to where I got to but Im not using name spaces-- but its still creating objects {}

    But now I need to serailise these objects and thats worrying me - as well as the no garbage collection.

    So the assertion :
    HISE Script != OOP

    will hold for the general rules of OO, but when you can do this:

    var myObj = {};

    then you are on the path to OO at least...



  • Can the task not be accomplished procedurally instead?



  • @d-healey said in Namespace != object:

    Can the task not be accomplished procedurally instead?

    "Procedurally"? -- it will be very very very very very ugly...heres what a small sample of the table looks like (it actually has 10 values for each cell - here you are seeing only 3):

    38221087-a23a-4551-aee5-cf9728dd373b-image.png

    -- no ones idea of fun -and filled with potential fail.



  • What's it for?



  • @d-healey -- not really in a position to say publicly...but generally its about cross fading sounds together to get a required outcome..



  • @Lindon Sounds fun! Do you need to use namespaces if you have it working with objects?



  • @d-healey said in Namespace != object:

    @Lindon Sounds fun! Do you need to use namespaces if you have it working with objects?

    No I dont , but I'm a bit worried about performance, garbage collection (tho its all a std tree structure for the objects in here), and serialisation.



  • -- yep and as I suspect:

    Engine.dumpAsJSON(myObj1, "my.json");
    ..
    ..
    ..
    var myObj2 = Engine.loadFromJSON("my.json");
    

    leaves myObj2 = undefined

    even tho my.json looks like this:

    {
      "soundName": "S",
      "group": 17,
      "planSet": [
        {
          "soundName": "B",
          "group": 2,
          "preWait": {
            "minVal": 4,
            "currentVal": 10,
            "maxVal": 25
          },
          "postWait": {
            "minVal": 1,
            "currentVal": 1,
            "maxVal": 1
          },
          "aCurve": 0.5,
          "aTime": 0,
          "aLevel": 0,
          "hTime": 100,
          "dCurve": 0.43,
          "dTime": 50,
          "sLevel": 100,
          "rTime": 0
        }
      ],
      "targetPlan": function (target){
              for (idx= 0;idx<this.planSet.length;idx++)
              {
                  if (this.planSet[idx].soundName == target)
                        return this.planSet[idx];
              };
            }
        
    }
    


  • @Lindon - yep loadFromJSON wont load object definitions...so serialisation will be a problem...



  • @Lindon Does it work if you save the object definition as a string and use the exec command?



  • @d-healey dunno - I will try --- tomorrow...
    err.. looking up exec gives me this:

    https://www.w3schools.com/jsref/jsref_regexp_exec.asp

    --which is seraching in strings



  • @Lindon I have a video about the exec command waiting to be edited, basically it executes a string as if it was a line of code, but it's a bit finicky and I haven't tried it with functions.

    So you could do something like this.

    var myCommand = "Console.print('HELLO WORLD');";
    exec(myCommand);
    


  • @Lindon said in Namespace != object:

    garbage collection

    Don't worry about garbage collection. This only gets a problem if you have cyclic references, which are pretty easy to avoid:

    var x = {};
    var y = {};
    
    x.child = y;
    y.child = x; // <= Baam, leak
    

    A tree normally doesn't have references from the child back to the parent, so unless you specifically want to shoot yourself in the foot, it doesn't matter.

    BTW, there's a cyclic reference checker built into HISE, that would detect the cycle in the (trivial) example above and spit out an error.

    8d0c7162-fb77-4242-8d0c-b8e0154a6208-image.png



  • Oh and to your original question:

    Back in the C days when there was no OOP built into the language, people were using POD structs (= plain old data) along with functions that take a reference to that object and manipulate it. This is pretty similar to what I would suggest trying out in HiseScript.

    So instead of a "real" OOP approach where you create objects that contain methods that operate on themselves you'll write "global" functions (or tuck them into a namespace for increased code clarity), then pass any object to it as parameter:

    namespace MyObjectFunctions
    {
        inline function initObject(obj)
        {
            obj.myVariable = 2;
        }
    
        inline function printVariable(obj)
        {
            Console.print(obj.myVariable);
        }
    }
    
    const var objList = [];
    
    for(i = 0; i < 1200; i++)
    {
        var obj = {};
        MyObjectFunctions.initObject(obj);
        objList.push(obj);
    }
    
    for(o in objList)
        MyObjectFunctions.printVariable(o);
    

    This is equivalent to the "correct" OOP way (I'm using C++ here, but you'll get the point)

    class MyFunctionObject
    {
        void init()
        {
            myVariable = 2;
        }
    
        void printVariable()
        {
            Console.print(myVariable);
        }
    
        int myVariable;
    };
    
    MyFunctionObject objList[1200];
    
    for(int i = 0; i < 1200; i++)
        objList[i].init();
    
    for(auto& o: objList)
        o.printVariable();
    

    As you can see, there is not much overhead compared to Javascript, but this separation between data and logic keeps things tidy - you can serialize the data using JSON etc.



  • @Christoph-Hart yeah this is sort of what I feared - now I have a lot of additional code to write to serialise and unseralise these arrays....when (as you say) I'm using simple pretty strict tree structures for my object collection.... so no garbage collection leaks -- it just seems a shame the JSON dump and load commands wont dump AND load objects - pretty ironic given the JSON acronym...



  • @d-healey said in Namespace != object:

    @Lindon I have a video about the exec command waiting to be edited, basically it executes a string as if it was a line of code, but it's a bit finicky and I haven't tried it with functions.

    So you could do something like this.

    var myCommand = "Console.print('HELLO WORLD');";
    exec(myCommand);
    

    Sadly:

    myTester = exec(Engine.loadFromJSON("test2.js"));
    
    var myCommand = "var mTestyNew = {'SoundName':'B','group':2};";
    exec(myCommand);
    

    where test2.js contains:

    var myTestFile2 = {
      "soundName": "S",
      "group": 17,
      "planSet": [
        {
          "soundName": "B",
          "group": 2,
          "preWait": {
            "minVal": 4,
            "currentVal": 10,
            "maxVal": 25
          },
          "postWait": {
            "minVal": 1,
            "currentVal": 1,
            "maxVal": 1
          },
          "aCurve": 0.5,
          "aTime": 0,
          "aLevel": 0,
          "hTime": 100,
          "dCurve": 0.43,
          "dTime": 50,
          "sLevel": 100,
          "rTime": 0
        }
      ],
      "targetPlan": function (target){
              for (idx= 0;idx<this.planSet.length;idx++)
              {
                  if (this.planSet[idx].soundName == target)
                        return this.planSet[idx];
              };
            }
        
    };
    

    Results in an object called mTestyNew (so that works) and myTester = undefined,so the LoadFromJSON fails...

    Even if it worked I have no idea how I would save an object in a way that would "exec"

    It looks like what Im asking for is

    JSON.stringify() <---which seems to be present, so I can turn an object into a string

    and

    JSON.parse() <-- which is missing so I cant turn it back into an object
    however this works:

    var TestOutString = JSON.stringify(myObject);
    var TestNewObj = eval(TestOutString);
    

    So all I need now is a way to save this string.....



  • But isn't the only problem the fact that you put a function into the object? JSON is a data format so you can't put logic in it...



  • Oh, and this:

    var myTestFile2 = {
      "soundName": "S",
      "group": 17,
      "planSet": [
        {
          "soundName": "B",
          "group": 2,
          "preWait": {
            "minVal": 4,
            "currentVal": 10,
            "maxVal": 25
          },
          "postWait": {
            "minVal": 1,
            "currentVal": 1,
            "maxVal": 1
          },
          "aCurve": 0.5,
          "aTime": 0,
          "aLevel": 0,
          "hTime": 100,
          "dCurve": 0.43,
          "dTime": 50,
          "sLevel": 100,
          "rTime": 0
        }
      ],
      "targetPlan": function (target){
              for (idx= 0;idx<this.planSet.length;idx++)
              {
                  if (this.planSet[idx].soundName == target)
                        return this.planSet[idx];
              };
            }
        
    };
    

    is Javascript, not JSON. You need to remove the var myTestFile2 = at the beginning so it's just the object declaration...


Log in to reply
 

8
Online

647
Users

2.6k
Topics

21.4k
Posts