Poll
Question: How should ENIGMA handle struct{} ambiguity?
User's responsibility
Dissallow immediate instantiation
Use cpp %{ %}
Hurr
Treat a newline following } as a semicolon

Pages: 1 2 »
  Print  
Author Topic: Quick Poll  (Read 19615 times)
Offline (Male) Josh @ Dreamland
Posted on: March 08, 2010, 08:58:12 am

Prince of all Goldfish
Developer
Location: Pittsburgh, PA, USA
Joined: Feb 2008
Posts: 2950

View Profile Email
As the more conscious of you realize by now, ENIGMA allows for C++-style declarations to be used in your projects. As you all should know, GM is very loose about its semicolons.

I asked Ism to create a script-like resource that will allow users to define their own structures and functions for use in the rest of the project. However, it is also legal in C to define structs in your code, and I don't want to alienate that as a feature.

For those who are new to C, structs are a simple data structure that lays out how chunks of memory look. They allow you to keep track of multiple things, like a named array. Instead of having, for example, a 2-Dimensional array to store x-y pairs, you could create a structure like the one featured below.

Veterans know that R3 allowed for cpp {} to handle such things. The cpp statement created a block of un-parsed code to allow users access to C++ without the parser having to check it for error, creating a workaround as well as a decent expandability. That block is no longer necessary for its old purpose; now we can choose to have it serve a new one along with it.

Say that in a create event, you want to create a structure with an X and Y pair. I don't know why you'd want to do this since you may as well just keep two arrays, but for the sake of example pretend you do.

Being used to the GM way, you say this:

Code: [Select]
struct xy {
  var x;
  var y;
}
direction = 0
speed = 0

That's a problem.
The parser would naively make your code look like this:
Code: [Select]
struct xy {
  var x;
  var y;
}
direction = 0;
speed = 0;

The above code looks correct, but in actuality, it redeclares direction as an xy structure in that piece of code. This is because the proper form for structure declaration in C is this:
Code: [Select]
struct name { int members; } instance; I'm sure you can all imagine why that isn't good... For those who can't, it means that for the rest of that code, the word "direction" will NOT refer to the builtin variable direction. It will instead refer to an instance of your xy structure, which behaves like an instance in GML. You could say direction.x = 0; direction.y = 1;, and it would be valid. To fix this, preventing the instantiation of immediate varnames (in this case, direction), you would simply add a semicolon following the closing brace.

Code: [Select]
struct xy {
  var x;
  var y;
};
direction = 0;
speed = 0;
The above piece behaves as one would expect.



All this considered, we are left with three options.
  • We can leave it up to the user to remember that semicolon.
     I still forget that semicolon at times. It is one of the most common mistakes in C/C++.
     It won't error, because direction is declared elsewhere. You'll have to figure it out yourself.
  • We can disallow immediate instantiation.
     This means that code will be treated as if the {} had been followed by a semicolon immediately, and that you cannot declare a new structure and instantiate it with one semicolon.
  • We can use cpp %{ %} for such declarations, erroring if the struct isn't in one.
     This is like the first option, but it will remind users to watch what they're doing. It's also pretty inconvenient, in my opinion. Whenever you want a new structure, you will have to input code similar to that below. I did like this suggestion when Miky made it, but then I couldn't make up my mind.

Code as entered for option 3:
Code: [Select]
cpp %{
  struct a {
    int b;
  } c;
%}
Creates a structure called a, with a member called b, and instantiates it in a variable called c. You can say c.b = 0, for example.

Please read over carefully and make your choice; feel free to ask questions or justify your answer. You can change your vote (assuming SMF works like the checkbox implies it will), so don't be afraid to just place one.

Note that the script-like resource mentioned is pure C++, and so expects perfect grammar. This does not apply to it.

I also ask that you don't just randomly pick one, though picking one because your retarded friend told you to do so is fine. I figure cheating on polls is a good thing; it reflects who cares most.
« Last Edit: March 08, 2010, 09:31:36 am by Josh @ Dreamland » Logged
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble
"I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
Offline (Male) retep998
Reply #1 Posted on: March 08, 2010, 09:50:31 am

Member
Location: Where else?
Joined: Jan 2010
Posts: 248
MSN Messenger - retep998@charter.net AOL Instant Messenger - retep998 Yahoo Instant Messenger - retep998
View Profile Email
I never really liked that immediate instantiation feature anyways.
I would always just create my class/struct and then later instance as many as I need.
Logged
Post made March 08, 2010, 09:57:14 am was deleted at the author's request.
Offline (Unknown gender) Game_boy
Reply #3 Posted on: March 08, 2010, 11:52:20 am
Member
Joined: Apr 2008
Posts: 228

View Profile
Just to clarify: the outcome of this poll won't affect games if you don't use that resource?
Logged
Offline (Unknown gender) luiscubal
Reply #4 Posted on: March 08, 2010, 12:01:51 pm
Member
Joined: Jun 2009
Posts: 452

View Profile Email
Also, this will probably affect class as well.
Logged
Offline (Unknown gender) The 11th plague of Egypt
Reply #5 Posted on: March 08, 2010, 01:20:44 pm
Member
Joined: Dec 2009
Posts: 274

View Profile
Nice, now I understand why the damn editor always wanted me to add that semicolon :eng101:

Really, that's a nice thing to learn. Couldn't we just advice the user to check that at compile time?
Like a non-critical error you can choose not to hear anymore?
Logged
Offline (Male) Josh @ Dreamland
Reply #6 Posted on: March 08, 2010, 02:27:17 pm

Prince of all Goldfish
Developer
Location: Pittsburgh, PA, USA
Joined: Feb 2008
Posts: 2950

View Profile Email
Game_boy:
Correct. This doesn't even affect anyone who does use the resource; only those who want to declare structures in individual events, which is a hassle anyway.

Luis:
Yes. Didn't want to complicate things with a second new vocabulary word that's essentially synonymous to the first.

The 11th plague of Egypt:
The problem with that is figuring out when you're ready to remove the training wheels and stop hearing that particular error. It'd confuse people when pressing the syntax check button revealed a warning. What about the rest of the document? Is this the only problem? Perhaps clicking the syntax button again would suppress that warning... I really don't have an intuitive system planned out for it, though.
Logged
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble
"I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
Offline (Unknown gender) The 11th plague of Egypt
Reply #7 Posted on: March 08, 2010, 03:05:05 pm
Member
Joined: Dec 2009
Posts: 274

View Profile
Add it as an option to tick/untick.
Logged
Offline (Unknown gender) MrJackSparrow2
Reply #8 Posted on: March 08, 2010, 04:06:43 pm
Member
Joined: Apr 2008
Posts: 35

View Profile Email
I think you should leave it at the hands of the users, because if they want to use C++, they should know what the hell they are doing.
Logged
Offline (Unknown gender) Micah
Reply #9 Posted on: March 08, 2010, 04:15:05 pm

Resident Troll
Joined: May 2008
Posts: 128

View Profile
I think that if you want to allow anything more than C++ operators, types, methods, etc., then I would vote for acting like there's a semicolon when there's a newline after the "}", but that would be unnecessarily replicating a lot of work and making the parser even more complicated than it already is.
Logged
Offline (Unknown gender) The 11th plague of Egypt
Reply #10 Posted on: March 09, 2010, 01:03:32 pm
Member
Joined: Dec 2009
Posts: 274

View Profile
I think that if you want to allow anything more than C++ operators, types, methods, etc., then I would vote for acting like there's a semicolon when there's a newline after the "}", but that would be unnecessarily replicating a lot of work and making the parser even more complicated than it already is.
I agree with this solution. Saves GM users and allows C++ tricks.
Logged
Offline (Male) Josh @ Dreamland
Reply #11 Posted on: March 09, 2010, 02:16:25 pm

Prince of all Goldfish
Developer
Location: Pittsburgh, PA, USA
Joined: Feb 2008
Posts: 2950

View Profile Email
Ah, only when there's a newline, hm... That can probably be arranged, but I think it'd confuse the hell out of people due to potentially appearing inconsistent. I'll add an option for it.

That's basically the principle on which JavaScript was founded, though, so...
« Last Edit: March 09, 2010, 02:18:06 pm by Josh @ Dreamland » Logged
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble
"I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
Post made March 09, 2010, 02:30:08 pm was deleted at the author's request.
Offline (Male) Josh @ Dreamland
Reply #13 Posted on: March 09, 2010, 02:33:36 pm

Prince of all Goldfish
Developer
Location: Pittsburgh, PA, USA
Joined: Feb 2008
Posts: 2950

View Profile Email
I meant the poll. I can't just have a checkbox for whether you want the compiler to bitch about that; you're bound to make the mistake again even once you're aware anyway.

I see what you mean now, and I like it.

Of the 14 who voted, 10 thought that it should either be the user's responsibility or that immediate instantiation should be disallowed; about a 50-50 split between those. That being the case, I think it'd behoove me to make it a checkbox between the two, defaulted to the one with more votes. So, by default, } will be };, but that can be turned off.
« Last Edit: March 10, 2010, 05:10:52 pm by Josh @ Dreamland » Logged
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble
"I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
Offline (Female) serprex
Reply #14 Posted on: March 12, 2010, 06:21:09 am
Smooth ER
Developer
Joined: Apr 2008
Posts: 106

View Profile WWW
Checkboxes are bad. Different sources will have different habits, and this causes code to be less transferable. The semicolon at the end of a struct is really just some hack of an attempt to have C++ be compatible with C while still extending the syntax to make it feel more OO
Logged
Pages: 1 2 »
  Print