Feature - Implemented Handle record literals as function parameters in ASH

soolar

Member
Right now, you can create record literals in variable definitions in ASH, so this example code works like you'd expect (prints 'hello: world')
Code:
void testFunction(string[string] example) {
  foreach a,b in example {
    print(a + ': ' + b);
  }
}
string[string] var = { 'hello': 'world' };
testFunction(var);

Unfortunately, you don't seem to be able to use them anywhere else, so the code below fails, saying it expected to find a ) but found a { instead.
Code:
void testFunction(string[string] example) {
  foreach a,b in example {
    print(a + ': ' + b);
  }
}
testFunction({ 'hello': 'world' });

I'm experimenting with reworking some stuff in ChIT atm to make it nicer to maintain and would love to be able to use the latter format for some things (mainly html tag related) to make the code a lot more readable. Seems like it shouldn't be too different to pass a literal to somewhere you have a definition for what you expect (a function parameter) vs somewhere else (a variable definition), so I'm cautiously hopeful that this isn't a big ask, and is instead just not something someone's wanted before.

EDIT: I should work on my reading comprehension, you CAN do this, it just requires you to specify the type of the aggregate in place. It would be nicer not to have to do that, but it's not a big deal.
 
Last edited:

Veracity

Developer
Staff member
I was going to point out that you weren't using "record literals", you were using "map literals", but you figured that out.
The reason that you have to specify the aggregate type is that you can overload functions.
I think using typedefs would make things easier for you.

Code:
typedef string[string] strstr;

void testFunction(strstr example) {
  foreach a,b in example {
    print(a + ': ' + b);
  }
}

typedef int[int] intint;

void testFunction(intint example) {
  foreach a,b in example {
    print(a + ': ' + b);
  }
}

testFunction(strstr {'hello' : 'world'});
testFunction(intint {1 : 10, 2 : 100, 3 : 1000});

yields

Code:
> literals

hello: world
1: 10
2: 100
3: 1000
 
Top