Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add struct-like data structure #39

Open
ikskuh opened this issue Oct 6, 2020 · 5 comments
Open

Add struct-like data structure #39

ikskuh opened this issue Oct 6, 2020 · 5 comments
Labels
language This issue is related to language syntax or semantics. proposal This issue proposes changes to the language that are not yet accepted and need some thinking.
Milestone

Comments

@ikskuh
Copy link
Owner

ikskuh commented Oct 6, 2020

Add a new data structure that works similar to arrays, but uses names as indices instead of numbers:

var struct = [
  .x = 10,
  .y = 20,
];
struct.x = 10;
Print(struct.y);

alternative:

var struct = [
  x: 10,
  y: 20,
];
struct.x = 10;
Print(struct.y);
@ikskuh ikskuh added language This issue is related to language syntax or semantics. proposal This issue proposes changes to the language that are not yet accepted and need some thinking. labels Oct 6, 2020
@ikskuh ikskuh added this to the 1.0 milestone Oct 6, 2020
@kristoff-it
Copy link

What about var struct = [foo: 1, bar: 2]?

@aeris170
Copy link

aeris170 commented Oct 26, 2023

If there are plans to implement an "#include someOtherScript" like functionality, we might want to just go ahead and use JSON for struct declarations and definitons. JSON is already well definedand this way independent JSON files can contribute to our scripts flawlessly.

@ikskuh
Copy link
Owner Author

ikskuh commented Oct 26, 2023

I have no idea how that would fit the language at all. LoLa doesn't need "includes" in a classical sense, you can just do a Include("file.lola"); function if you desire includes.

Also JSON doesn't define structures, and how would that help in using structures in the language itself?

@aeris170
Copy link

aeris170 commented Oct 26, 2023

Sorry for the confusing comment. I must have not gotten fully awake when I wrote that. Please forget the part where I mention including other files.

JSON can't define anything, but it can declare objects. Here's your example but with JSON:

var struct = {
  x : 10,
  y : 20
};
struct.x = 10;
Print(struct.y);

I'm not familiar with Zig and it's community but I'm sure there exists a good JSON Parser you can integrate into LoLa to parse JSON strings into objects. My guess is implementation would be much easier compared to rolling out your own syntax, and JSON syntax is already familiar to many developers so users could seamlessly internalize the struct declaration syntax.

The only downside I see is inconsistent assignment operator characters, semicolon(:) inside the structs and equal sign(=) outside of structs. JavaScript does the same, so I guess people don't care all that about consistency.

@ikskuh
Copy link
Owner Author

ikskuh commented Oct 27, 2023

You are aware that LoLa already has a parser and you'd have to integrate another parser into this one, whereas a "self-rolled" parser would be a work of half an hour or so?

Also using a json parser would be a bad idea because LoLa uses a different type system than json ;) Also you want to have expressions inside struct decls, otherwise it would be completly useless, as you can only ever hardcode values:

var struct = [
  x: GetFoo(),
  y: 20 + 5 * array[3],
];

must be valid syntax, assuming we use a [ x: … ] syntax

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
language This issue is related to language syntax or semantics. proposal This issue proposes changes to the language that are not yet accepted and need some thinking.
Projects
None yet
Development

No branches or pull requests

3 participants