JADE Environment Development Ideas

string interpolation

Many languages now have string interpolation so that instead of

 "Name: " & name & " Age: " & age

you can do something like

"Name: {name} Age: {age}"

which is faster to type and more readable.

 

It would also be nice when interpolating to automatically convert any non-string values to string by doing the default .String cast so that if age was an Integer we could do

"Age: {age}"

instead of

"Age: {age.String}"
  • John Munro
  • Nov 21 2019
  • Future consideration (Long term)
  • Attach files
  • BeeJay commented
    21 Nov, 2019 07:19pm

    In the interim, in case it's useful I've utilised the ParamListType in the past to create my own primitive method to do the text substitution as per the following example.  Feel free to create something similar yourself:

     

    getFormattedString( pValues : ParamListType ) : String;

    vars
    formattedString : String;
    valueIndex : Integer;
    value : Any;
    replacementCount : Integer;
    placeholderString : String;
    missingPlaceholders : String;

    begin
        formattedString := self;

        foreach valueIndex in 1 to app.getParamListTypeLength( pValues ) do
            value := app.getParamListTypeEntry( valueIndex, pValues );

            placeholderString := "{" & ( valueIndex - 1 ).String & "}"; // Making the placeholders "zero relative" for increased familiarity for C# developers

            replacementCount := formattedString.replaceString( placeholderString, value.String );

            if replacementCount = 0 then
                if missingPlaceholders <> null then
                    missingPlaceholders := missingPlaceholders & ", ";
                endif;
                missingPlaceholders := missingPlaceholders & placeholderString;
            endif;
        endforeach;

        if missingPlaceholders <> null then
            app.raiseDeveloperException( "Request to format string but unable to find the following placeholders: " & missingPlaceholders );
        endif;

        return formattedString;
    end;

     

    Here's a couple of examples of using the method.  The first example displays 'Something with Bob and Dan in the text' to the interpreter output window.  The second example results in a developer exception being raised, and no additional details in the interpreter output window:

    vars
        string : String;

    begin
        string := "Something with {0} and {1} in the text".getFormattedString( "Bob", "Dan" );

        write string;

        string := "A call which will fail due to missing placeholders. We have {0} and {1} but we're passing in 4 parameters".getFormattedString( "Bob", "Dan", "Bill", "Doug" );

        write string;
    end;

     

     

  • BeeJay commented
    21 Nov, 2019 07:02pm

    It would be good if it supported 'calling methods' which return values as well as local variables/parameters/properties.

    For example: "Name : {person.getStandardNameDisplay()} Age: {person.getAge()}"

    It would also be good if auto-complete could still make suggestions inside the {} even though you're defining a 'string literal'.