Tue Jun 25, 2019 3:58 am
Login Register Lost Password? Contact Us


error while using #EXPAND

Comments and questions related to the Enterprise Control Language

Sun Mar 24, 2019 1:09 pm Change Time Zone

I am trying to use #EXPAND to evaluate a constant expression and it works fine.
Code: Select all
Rec := {STRING a,STRING b,STRING c};

fieldList := 'a,b,c';

ds := DATASET([{ 'PORTLY', 'STUART' , '39'},
              { 'PORTLY', 'STACIE' , '36'},
              { 'PORTLY', 'DARA' , ' 1'},
              { 'PORTLY', 'GARRETT', ' 4'}], Rec);
                     

mac_fn(inds,infl) := FUNCTIONMACRO
   RETURN TABLE(inds,{#expand(infl)});
ENDMACRO;
mac_fn(ds,fieldlist);;


When I try to call a deterministic function on a constant and pass that to #EXPAND, it complains. Is there a way to force the compiler to constant fold firstfield?

Code: Select all
Rec := {STRING a,STRING b,STRING c};

fieldList := 'a,b,c';

ds := DATASET([{ 'PORTLY', 'STUART' , '39'},
              { 'PORTLY', 'STACIE' , '36'},
              { 'PORTLY', 'DARA' , ' 1'},
              { 'PORTLY', 'GARRETT', ' 4'}], Rec);
                     
mac_fn2(inds,infl) := FUNCTIONMACRO
   RETURN TABLE(inds,{#expand(infl)});
ENDMACRO;

IMPORT Std.Str;
firstfield := Str.SplitWords(fieldList,',')[1];
mac_fn2(ds,firstfield);


-Srini
omnibuzz
 
Posts: 94
Joined: Wed Feb 06, 2013 2:02 pm

Mon Mar 25, 2019 2:24 pm Change Time Zone

Srini,

This works for me:
Code: Select all
Rec := {STRING aaa,STRING bbb,STRING ccc};

fieldList := 'aaa,bbb,ccc';

ds := DATASET([{ 'PORTLY', 'STUART' , '39'},
              { 'PORTLY', 'STACIE' , '36'},
              { 'PORTLY', 'DARA' , ' 1'},
              { 'PORTLY', 'GARRETT', ' 4'}], Rec);
                     
mac_fn2(inds,infl) := FUNCTIONMACRO
   RETURN TABLE(inds,{#expand(infl)});
ENDMACRO;

IMPORT Std.Str;
// firstfield := Str.SplitWords(fieldList,',')[1];
// mac_fn2(ds,firstfield);
mac_fn2(ds,fieldList[1..Str.Find(fieldList,',',1)-1]);
I think the difference is the Find can resolve to a string constant and the SplitWords to a dynamic set.

HTH,

Richard
rtaylor
Community Advisory Board Member
Community Advisory Board Member
 
Posts: 1459
Joined: Wed Oct 26, 2011 7:40 pm

Mon Mar 25, 2019 3:16 pm Change Time Zone

Thank you, Richard. That is awesome.
May be I missed it in the documentation, but is there a list of functions that are constant folded?

Cheers
Srini
omnibuzz
 
Posts: 94
Joined: Wed Feb 06, 2013 2:02 pm

Mon Mar 25, 2019 3:57 pm Change Time Zone

Srini,
is there a list of functions that are constant folded?
Not that I know of.

In this case I think it has to do with the context and not the function itself.

Passing a string constant to the FUNCTIONMACRO is the key here. And in this case that constant is just truncated by the Find function's return value.

IOW, these both work for me:
Code: Select all
mac_fn2(ds,fieldList[1..Str.Find(fieldList,',',1)-1]);
mac_fn2(ds,fieldList[1..Str.Find(fieldList,',',2)-1]);
So the constant value requirement for Template Language is met by passing in a string constant to the FUNCTIONMACRO and the compiler treats the calculated range as a constant expression (and probably does the calculation) at compile time.

HTH,

Richard
rtaylor
Community Advisory Board Member
Community Advisory Board Member
 
Posts: 1459
Joined: Wed Oct 26, 2011 7:40 pm

Mon Mar 25, 2019 8:00 pm Change Time Zone

Srini,

Since you already have the fieldnames in a string constant, why not just add a set of field names to the code, like this:
Code: Select all
Rec := {STRING aaa,STRING bbb,STRING ccc};

fieldList := 'aaa,bbb,ccc';
fieldSet := ['aaa','bbb','ccc'];

ds := DATASET([{ 'PORTLY', 'STUART' , '39'},
              { 'PORTLY', 'STACIE' , '36'},
              { 'PORTLY', 'DARA' , ' 1'},
              { 'PORTLY', 'GARRETT', ' 4'}], Rec);
                     
mac_fn2(inds,infl) := FUNCTIONMACRO
   RETURN TABLE(inds,{#expand(infl)});
ENDMACRO;

mac_fn2(ds,fieldSet[1]);
mac_fn2(ds,fieldSet[1]+','+fieldSet[3]);

That way you could have the best of both worlds.

HTH,

Richard
rtaylor
Community Advisory Board Member
Community Advisory Board Member
 
Posts: 1459
Joined: Wed Oct 26, 2011 7:40 pm

Mon Mar 25, 2019 8:28 pm Change Time Zone

Thank you, Richard. I got the idea. What I gave was a contrived self-contained example to explain the issue. I have a more complex transformation to be done on a string constant.

Cheers
Srini
omnibuzz
 
Posts: 94
Joined: Wed Feb 06, 2013 2:02 pm


Return to ECL

Who is online

Users browsing this forum: Bing [Bot] and 0 guests

cron