Skip to content

More concise way to render param types and values #136

@wookayin

Description

@wookayin

The old way

For example, we have templates such as

${<if !e.Output.Param.Type.Array}
        __expected = ${e.Output}
${<else}
        __expected = (${foreach e.Output.ValueList v ,}
            ${v}${end}
        )
${<end}

or

${<if !in.Param.Type.Array}
            ${in.Param.Type.Primitive} ${in.Param.Name} = ${in};
${<else}
${<if !Options.cpp11}
            ${in.Param.Type.Primitive} ${in.Param.Name}[] = {${foreach in.ValueList v ,}
                ${v}${end}
            };
${<else}
            vector<${in.Param.Type.Primitive}> ${in.Param.Name} = {${foreach in.ValueList v ,}
                ${v}${end}
            };
${<end}
${<end}

Suggestion

A little messy, isn't it? Instead, I imagine a way that the parameter value is rendered directly in a polymorphic way: if it was a scalar, render it as-is, and if was a vector, render it in the way the compiler can interpret it.

We can know the language trait informations and the way how the parameter values should be rendered, given the context and the options.

The new way

Here are examples:

        __expected = ${e.Output}
  • Assuming the parameter is a primitive -- rendered as 1 or "YES" or 3.14159265358979 ...
  • Assuming the parameter is an array. Currently, ${e.Output} renders into the form of {1, 2, 3, 4, 5}. The modified render emits (1, 2, 3, 4, 5) -- the tuple, which is the way it should be written in python.

or

            ${in.Param.Type} ${in.Param.Name} = ${in};
  • Type becomes, for example, int, vector<int>, vector<long long> (in C++), or int[] (in Java), and so on...
  • ${in} (or in.Value ?) becomes 3, {1, 2, 3}, ... (no foreach blahblah)
    • Here comes a pitfall. Without C++11, we can't use initializer lists. But given the rendering context, we are provided whether C++11 feature is enabled or not in the configuration. So an alternative is required depending on this.
    • Surely, if we can't achieve it in a generic way, then we should branch the cases (as right now)
    • Or, introducing ${in.Declaration} -- writing the variable declaration code in the Java code, not in the template?

Remarks

  • This is just an idea for improvement - not a major nor pressing issue.
  • Considering various ways and cases, w.r.t various languages and parameter types (and grid-like options for String[] maybe), there are lots of things to be concerned.
  • Hope it helps us to maintain a much clean and readable template.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions