ModScript : how do I add another "string" constructor??

I'd like to have a ModScript "string" constructor that will handle a VBScript String like this:

   var str_PostData1 = string(Shell.PostData());

For the time being I'm doing this, but I'd like to learn how to do write my own constructors.  ModScript needs a lot of 'em.

   var str_PostData1 = string(Shell.PostData().to_string());

Below is my overloaded constructor function.  The runtime error is:

Line 395> this=v.to_string();
Error: "Error, cannot assign to temporary value."

def string::string(Variant v) {
   // If the Chai type is ModScript "Variant" type, i.e. a VBScript type object
   // AND the VBScript type is a string, initialize string to that string,
   // OTHERWISE initialize it to a blank string.

   if (v.type_name() == "Variant" && VarType(v)==vbString) {
      this=v.to_string();
   } else {
      this="";
   }
   return this; // Do I need this???
}

Tags:

  • 0  

    Hi Paul. Not sure, try it this way:

       var ret="";

       if (v.type_name() == "Variant" && VarType(v)==vbString) {
           ret=v.to_string();
       } else {
           ret="";
       }

       this = ret;
       return this;

    I leveraged this as reference:

     SBM ModScript, Part 3 - Adding Methods to a Class 

    If this works, it is probably because the assignments act like pointers. "this" would have pointed to the temporary result of to_string(). With this revised approach, we allocate a new string and set "this" equal to [the pointer of] the new string. If this approach doesn't work, I tried Slight smile

  • 0 in reply to   

    It was a good try.

       Line 414> this=ret;
    Error: "Error, cannot assign to temporary value."
  • 0   in reply to 

    Sorry :( I think it may require feedback from development.

  • 0 in reply to   

    I have discovered a wealth of ModScript/ChaiScript knowledge in Jason Turner's (the inventor of ChaiScript) github:

    https://github.com/ChaiScript/ChaiScript/

    Particularly in the "unittests" and "samples" sections:

    https://github.com/ChaiScript/ChaiScript/tree/develop/samples

    https://github.com/ChaiScript/ChaiScript/tree/develop/unittests

    Watch out for possible bogus github repositories

       https[://]github[.]com/skaag/chaiscript/

  • 0   in reply to 

    Nice find on the chai script.

    Replying to your original question, not sure you could pass in a non-string into the string constructor. I would assume that it would need to be of type 'string'.

    Ignoring the fact that string would be a nice name the function string, is there any reason you are just defining a new function that you pass in a variant and return the string ?

    When I created a method for string to split it, I would have to assign it to a new variant:

    def string::split(token) {
        var str = this;
        var result = [];
          while (str != "") {
              var index = str.find(token);
              if (index != string_npos) {
                    result.push_back(str.substr(0, index));
                    str = str.substr(index + token.size(), string_npos);
                    
                  } else {
                    result.push_back(str);
                    str = "";
                  }
          }
        return result;
    }

    var stringList = "one,two,three"

    var newV = stringList.split(",");

    I've also always included the return in the method. Not sure, but it seems it is needed.

    def Vector::TransposeArray() {
        var A := this;
        var B = Vector(A);
        var C = [];
        var lengthA = A.size();
        for (var i = 0; i < lengthA; ++i){
            C.push_back(B[lengthA - i-1]);
        }
        for (var i = 0; i < lengthA; ++i){
            A[i] = C[i];
        }  
     
        return A      
    }

    var orig = [1,3,12,16];
    orig.TransposeArray();

    //orig is now [16,12,3,1]

  • 0 in reply to   

    Michael:

    Thanks for the info.

    I want to have a "transparent" way to convert VBScript/AppScript objects into ChaiScript/ModScript objects, in this case a VBScript "String" (VarType 8) into a ChaiScript "string".  There is just soooooo much "AppScript-specific" infrastructure/baggage in ModScript that it really makes it difficult to drink the ChaiScript koolaid.  As it were ;-)

    My constructor runs, but it returns a ChaiScript "Dynamic_Object" instead of a "string" (according to my "VarInfo" function).  I'm going to revert to the explicit conversion via "string(Shell.PostData().to_string());" instead of trying to write a constructor.  My frustration level is too high at this point.  I'll just assume that ModScript will always be saddled with VBScript baggage.  

    See this suggestion and this suggestion in the "Idea Exchange" section.

  • 0   in reply to 

    I was lucky because I have been tasked with mostly creating new applications, so I got to avoid the headache of converting from AppScript.

    It doesn't mean that I still haven't run into issues with variants, probably the code that tries to make Mod/Chai backward compatible.  The one that has gotten me a few times is the GetId() call, which I assume is going to come back as an integer, but not always, so I have to run the GetId().to_int()  in order to put it into a Map which converts to JSON without failing.

    I like your suggestions.

    And I saw this page for the conversion from AppScript, which you have probably read, but just in case, as sometimes things get hidden in the SBM documentation: https://docs.microfocus.com/search/VB%2520string/all

  • 0

    I am out of ideas.

    I would like a simple example of how to create a ChaiScript constructor that returns a ChaiScript/ModScript "string" when given a parameter which is a ModScript "Variant"/VBScript String (vartype 8). What it actually does with the param is irrelevant. What I need documented is:
    1. How to assign a value to the underlying ChaiScript/ModScript "string" object inside the constructor.
    2. What to return and how to return something that so that the calling code gets back a ChaiScript / ModScript "string".

    I would like an example that actually works in a ModScript.

    Use the code below as the template and modify the contents of the constructor so that requirements 1 & 2 above are fulfilled. NOTE: I've submitted this same request to tech support but generally they will not handle scripting questions.

    def string::string(Variant v) : VarType(v)==vbString {
       // How do I assign something to the "string" object being constructed?
       // What do I "return" so that the caller gets back a valid ChaiScript / ModScript "string" ???
    }

    // try to use the constructor
    var s1 = Variant("VBScript String");
    var s2 = string(s1);

    Any help or clues would be greatly appreciated.  Thanks in advance.