Instantiation syntax - our own Classes etc.

This forum is meant for questions and discussions about the X# language and tools
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Instantiation syntax - our own Classes etc.

Post by Phil Hepburn »

Hello again Robert,

Sorry, I have had a few days away from LINQ 'stuff' doing some house maintenance - replacing floor boards in a bedroom !! What a task.

However, I do have something positive to post, on the issue of 'in-line' instantiation. Since your last post, I had a play around and a think, and used the current compiler facilities to come up with some better, tidier, and easier to read syntax. The three attached and inserted small images will help explain all. Sorry the 'system' has inserted them in the reverse order to my selection, so checkout '_01' first, then '_02' and '_03'.

By naming the Constructor variables 'in_xxxx' where the X's represent the exact property names, then the code all gets a lot more understandable.

Hope this makes sense, but I would still prefer to use an empty Constructor and then specify (in any order) the property name and the value to be assigned. This way we can effectively have all possible overloads, as zero to 'N' inputs can be provided.
In_Param_01.jpg
In_Param_01.jpg (69.45 KiB) Viewed 309 times
In_Param_02.jpg
In_Param_02.jpg (85.89 KiB) Viewed 309 times
In_param_03.jpg
In_param_03.jpg (64.91 KiB) Viewed 309 times
Regards to all,
Phil.
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Instantiation syntax - our own Classes etc.

Post by Phil Hepburn »

Whoops ! - an Addendum :-

The last image shows that we can only be as tight in our code as our human frailties (inabilities) allows us to be.

If you check out the commented code and its related part, you see we can assign incorrectly as long as the type is correct, and the compiler is none the wiser ;-0)

Regards,
Phil.
User avatar
robert
Posts: 4520
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

Instantiation syntax - our own Classes etc.

Post by robert »

Phil,
You will be pleased to hear that we have added object initializers and collection initializers for the next build.

An example of the collection initializer:

Code: Select all

FUNCTION Start AS VOID
LOCAL a AS ArrayList
a := ArrayList{}{1,2,3}
? a[1] 
     Console.Read()

An example of the Object Initializer, with a nested collection initializer for the Phones property.
Please also note that an AUTO property can have an initial value too, without a constructor !)

Code: Select all

FUNCTION Start AS VOID
     VAR oPerson := Person{}{ Last := "Van Der Hulst", First :=
"Robert", Phones := {"123","456"} }
     ? oPerson
     Console.Read()


CLASS Person
     PROPERTY First     AS STRING AUTO
     PROPERTY Last      AS STRING AUTO
     PROPERTY Phones    AS List<STRING> AUTO := List<STRING>{}
     METHOD ToString() AS STRING
         VAR result := First+" "+Last
         FOREACH VAR p IN Phones
             result += " P: "+p
         NEXT
         RETURN result
END CLASS 
Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Instantiation syntax - our own Classes etc.

Post by Phil Hepburn »

Hi Robert,

This sounds really good, and also a VERY worthwhile step forward. I look forward to using it when you make it publically available. I certainly will have it in my examples and eNotes for Cologne.

For the moment I have plenty to keep me busy, apart from my floor replacement tasks ;-0)

Next on my list is to look at "LINQ to SQL" and create some X# samples and eLearning notes. There are a few things which differentiate LINQ to SQL from "LINQ to Objects", even though the basic / fundamental syntax code has many similarities.
Please give my best wishes to all the Team.

Cheers,
Phil.

P.S. I will make a note in my session material of what you posted - just so they know what is coming soon.
Post Reply