[Solved]What's a comma and a semicolon

General help with the eC language.
Post Reply
samsam598
Posts: 212
Joined: Thu Apr 14, 2011 9:44 pm

[Solved]What's a comma and a semicolon

Post by samsam598 »

Sorry I was a bit confused.Given below code

Code: Select all


SavingDataBox colorBox   
   {            
      this, 
      size = { 116, 20 },    //q1
      position = { 176, 104 }; //q2
      data = &color; 
      type = class(Color);       
      bool NotifyChanged(bool closingDropDown)      
      {         
         background = color;         
         return true;      
       }   
    };                       
The two lines I marked as q1 and q2,what makes them different to us a comma and a semicolon?

BWT,another question,

Code: Select all

    Date date;   //L1
    DateTime now;   
    date = (now.GetLocalTime(), //L2
    Date { now.year, now.month, now.day }); //L3
What's a Date object ?Visual control CalenderPicker as other language?L1 defined a date instance and initialize the value on L2,so what's the purpose of L3?

Thanks for the help.

Regards,
Sam
Last edited by samsam598 on Thu Sep 08, 2011 12:29 am, edited 1 time in total.
jerome
Site Admin
Posts: 608
Joined: Sat Jan 16, 2010 11:16 pm

Re: What's a comma and a semicolon

Post by jerome »

Comma/Semicolons:

Within a class, a struct, or an instantiation, you can use commas or semicolons to separate members assignments. There can be extra semicolons, they won't hurt, but extra commas will cause syntax error. Before a function definition (or overriding virtual method), there must be a semicolon if there are any member assignments preceding it. There cannot be a comma left at the end, but there can be a semicolon at the end.

After a class definition, it doesn't matter if you put a semi colon or not.
After a struct definition, you must put a semicolon;

To answer your first question about your example of q1 and q2, they are exactly the same.

That date code is a little tricky.
eC lets you write assignments as part of a class definitions, which kind of replaces the purpose of a constructor. But you cannot put statements there.
So 'date = (now.GetLocalTime(), { now.year, now.month, now.day });' is a trick to embed a statement within an expression, so that we can assign it to our 'date' data member. The first part before the comma fills 'now' with the local time, and the part after the comma is the actual value of that whole expression within brackets, a 'Date' object that gets populated with the value in 'now'.

The 'Date' struct is defined as such:

Code: Select all

public struct Date
{
   int year;
   Month month;
   int day;
};
and is useful for handling dates :)

Other languages:

liqi has been working (maybe still is :) ) on a Chinese localization of the whole SDK. We're trying to get it going using gettext. This could work for the Calendar Control as well :) Of course you could always write your own control the way you like.
samsam598
Posts: 212
Joined: Thu Apr 14, 2011 9:44 pm

Re: What's a comma and a semicolon

Post by samsam598 »

Hi Jerome,

Thank you so much for the detailed explanation.
For the Date code example

Code: Select all

date = (now.GetLocalTime(), { now.year, now.month, now.day });
Since a Date is a struct that has three field{year,month,day},so still don't quit understand how come date=(xx,xx) works.For the evaluation order of comma (e.g. exp1,exp2),shouldn't the value pass from right to left?Is there a two step implementation looks like date=now.GetLocalTime;
date={now.year,now.month,now.day},or something like that?

What's more,just realized this:

Code: Select all

 Date date;   
    DateTime now;   
    date = (now.GetLocalTime(), 
    Date { now.year, now.month, now.day });      
SavingDataBox dateBox   
    {            
      this, 
      size = { 200, 20 }, 
      position = { 376, 104 }; 
      data = &date; 
      type = class(Date);   
     };    
     double d;   //q1
     d = Pi;         
Given the code I marked as q1,why it is allowed to declare a variable d here rather than at the begining?

Thanks and regards,
Sam
jerome
Site Admin
Posts: 608
Joined: Sat Jan 16, 2010 11:16 pm

Re: What's a comma and a semicolon

Post by jerome »

Hi Sam,

date = (now.GetLocalTime(), { now.year, now.month, now.day });

Notice the parentheses, (not curly brackets).

Parentheses in C/eC define an 'expression', a single value, in this case a 'Date' structure.

Some info about this syntax on IBM's website.
The left operand is fully evaluated, possibly producing side effects, and its value, if there is one, is discarded. The right operand is then evaluated.
The first part before the comma is executed, discarded, and the value for the whole expression between ( ) is the second part where the { } are (where there are 3 data members, but it could be just { now.year, now.month } and day would be left to the default value of 0).

Again, this is a just a trick to do it directly inside the class. You could instead write it in the constructor:

Code: Select all

class ColorFun : Window
{
   Date date;
   ColorFun()
   {
      DateTime now;
      now.GetLocalTime();
      date = { now.year, now.month, now.day };
   }
   SavingDataBox dateBox
   {      
      this, size = { 200, 20 }, position = { 376, 104 }; data = &date; type = class(Date);
   };
}
There you don't need tricks because you are allowed to write statements. Inside the class only assignment expressions are allowed.
why it is allowed to declare a variable d here rather than at the begining?
Because this is not a compound statement block inside a function, this is a class definition, where you are allowed to:
1. declare class members
2. assign default values for instances of the class to data members using assignment expressions

I hope this makes things clearer :)
samsam598
Posts: 212
Joined: Thu Apr 14, 2011 9:44 pm

Re: What's a comma and a semicolon

Post by samsam598 »

Thanks Jerome,sorry for asking stupid questions to you.
jerome
Site Admin
Posts: 608
Joined: Sat Jan 16, 2010 11:16 pm

Re: What's a comma and a semicolon

Post by jerome »

There are no stupid questions, only stupid answers :lol:
Post Reply