[stringtemplate-interest] formal arg checking

Nate misc at n4te.com
Mon Apr 30 20:16:30 PDT 2007


Terence Parr wrote:
> On Apr 26, 2007, at 12:09 PM, Nate wrote:
>> assume you are proposing this...
>>
>> $if (errorMessage)$
>> $table(emptyMessage=errorMessage)$
>> $else$
>> $table(emptyMessage="The list is empty.")$
>> $endif$
>>
>> While this would solve my specific scenario, formal argument
>> checking is
>> still flawed. I should be able to use undefined attributes when
>> applying
>> a template, like this...
>>
>> $table(emptyMessage={$errorMessage; null="The list is empty."$})$
>>
>> When "errorMessage" is not defined, that code currently throws an
>> error
>> saying that the attribute "errorMessage" is not a valid formal
>> argument
>> for the "table" template.
>
> Is error message defined anywhere in the surrounding context? that
> is, in an enclosing template? If not, you cannot reference an
> undefined attribute just like in a programming language. If I were
> you, I would just add errorMessage to the template that invokes table
> and you problem disappears.
No, errorMessage is only defined in the surrounding context when there 
is an error message. I am pretty sure that ST out of the box allows this 
to work...

$table(emptyMessage=errorMessage)$

But not this...

$table(emptyMessage={$errorMessage$})$

In the latter, ST explodes with an exception saying that "errorMessage" 
is not a formal argument to "table". While this is true, it is not 
consistent behavior with the former.

That is what doesn't make sense to me.

I have removed formal argument checking from my local copy of ST. I'm 
posting this because I think ST formal argument checking should be fixed 
in the official distribution.

This should fail with "table doesn't have a formal argument named nate" 
exception...

$table(nate="moo")$

This should NOT fail even if "cow" is undefined ("rows" is a valid 
formal arg)...

$table(rows={$cow$})$


I'd like to tack on a couple unrelated items since I'm here typing anyway...

A. Why does this fail?... $first(tests).name$
Seems like that should be valid.
$first(tests):{$it.name$}$ is quite an ugly workaround.

B. Would index access to lists break separation? Eg...
$tests[0].name$
$tests[1].name$
[0] can be done with first(), but getting the nth element is impossible 
otherwise.

C. Imagine this code...
$tests:{test|
  $test.values:{value|
    $value$,
  }$
}
Output might be...
      value1, value2
test1 123, 456
test2 321, 654
test3 789, 987
Now I want to output the table differently, rotated 90 degrees like this...
       test1, test2, test3
value1 123, 321, 789
value2 456, 654, 987
How is this possible with ST? Please don't say just do it in the 
controller. :) Eg, using psuedo code...
for (i : 0 to numValues-1)
  for (test : tests)
    $test.values[i]$,
Please assume all tests have the same number of values.

-Nate



More information about the stringtemplate-interest mailing list