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 »

Hi Robert, Team and guys,

I am trying to provide in my LINQ eNotes a better way to 'Select' than to use an anonymous type, when it then causes issues afterwards - to access elements and their properties.

I have followed Nick's advice and made myself a working 'concrete' class call "EightP2S".

The attached image shows the working code but also a couple of commented and failed code attempts by me, to find some alternative syntax for specifying the property values at the point of instantiation - all in the 'select' clause.

I would like a much tighter version of line 82, and one where the string values for City and Email could not get mixed up (swapped around) just because I got my positioning (placement) wrong in my code. The compiler would not complain at my mistake!

Please can you help and make some suggestions etc.,
Cheers,
Phil.
ConcreteClasses_05.jpg
ConcreteClasses_05.jpg (66.85 KiB) Viewed 353 times
Cheers,
Phil.
User avatar
robert
Posts: 4225
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

Instantiation syntax - our own Classes etc.

Post by robert »

Phil,

The syntax for Named arguments is

Code: Select all

: <Identifier> := <Expression>
at this moment.

Try this:

Code: Select all

FUNCTION start AS VOID   
	VAR oPerson := Person{ :LastName := "Hepburn" , :FirstName := "Phil" }
	? "F", oPerson:FirstName
	? "L", oPerson:LastName  
	Console.ReadLine()	
	
CLASS Person
	PROTECT _firstName AS STRING
	PROTECT _lastName  AS STRING	                                        
	PROPERTY FirstName AS STRING GET _firstName PRIVATE SET _firstName := VALUE
	PROPERTY LastName  AS STRING GET _lastName  PRIVATE SET _lastName := VALUE
	CONSTRUCTOR (FirstName AS STRING, LastName AS STRING)
		_firstName := FirstName
		_lastName  := LastName
		RETURN
END CLASS

I will talk with Nikos to see if we really need the colon before the argument name...

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 - thanks,

I have no real issue with a colon at the moment - BUT - I can't seem to get this syntax to work - can you have a quick look at the attached images.

Am off to take my wife for her car.

Speak soon,
Phil.
ConcreteClasses_11.jpg
ConcreteClasses_11.jpg (59.78 KiB) Viewed 353 times
ConcreteClasses_12.jpg
ConcreteClasses_12.jpg (73.83 KiB) Viewed 353 times
ConcreteClasses_13.jpg
ConcreteClasses_13.jpg (26.27 KiB) Viewed 353 times
FFF
Posts: 1522
Joined: Fri Sep 25, 2015 4:52 pm
Location: Germany

Instantiation syntax - our own Classes etc.

Post by FFF »

Robert wrote:I will talk with Nikos to see if we really need the colon before the argument name...
Robert
Yes, please. Already to many dots on the screen ;)
Regards
Karl
(on Win8.1/64, Xide32 2.19, X#2.19.0.2.)
User avatar
robert
Posts: 4225
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

Instantiation syntax - our own Classes etc.

Post by robert »

Phil,
The argument name in the constructor call must match the parametername in the parameterlist of the constructor.
In your constructor you have used the parameter names cM, cE, cC, dtDB.
You are now trying to assign the properties. That is something different.
I know that C# has a syntax where you can do that :

Code: Select all

var person = new Person
                    {
                         FirstName = "Phil",
                         LastName = "Hepburn"
                    };
But this is something different. In that case you care calling a constructor without parameters and then you are assigning values to the properties.
CSharp calls this an ObjectInitializer() in stead of an Object constructor iirc.

The syntax that I used before is using a parameterized constructor.
We will have a look at how C# implements the ObjectInitializer and maybe we will come with a good syntax for that later.
Something like this maybe:

Code: Select all

var person := Person {{ FirstName := "Phil",LastName := "Hepburn }}
Do you have a suggestion ?

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 - Thanks yet again for your prompt and sensible / constructive reply, and positive comments.

I understand the current situation and have it working for me and my eNotes material - "great stuff".

I attach an image or two to show what I have, and to allow a possible short statement from me as to what I would like, and / or may make sense to others ?!

I have renamed the arguments in the Constructor to be more meaningful than 'cC' and 'cE' etc.
I have also tried the swapping around of 'inputs' when specifying the new object - this seems to work okay, so we can have any order as of now ;-0) Presumably we have to input arguments for all four, as this is the 'pattern' (signature) of the possible Constructor, other than the empty one.

Now then, I suggest that if possible, we need to have an Intellisense reminder of what the constructor arguments are - most essential I feel, if we are to keep this approach.

However, I am NOT at all sure I like a public face to the 'in-method' argument variables that I usually use for my own and VERY private use - for my eyes ONLY!

Also, what if we have Constructor overloads with different argument variables ? The properties on the other hand will stay the same for all Constructors.

Can we come up with a syntax that would allow us to be restricted to using public property names, and these to be in the Intellisense list ? - for ease of coding.

For now I am happy to go ahead with my research and eNote creation with things as they are, and make a note to readers that this could change and improve over the coming months.

We are getting close to what we can do in C#, and xbase (X#) is managing to do a whole heap that I like and enjoy, and to be honest, never thought that I would ever see.

But it would be nice to be able to do as in C#, they have obviously thought things through pretty well, whatever some of our colleagues say about MS and their staff.

Hope all this makes sense and is useful in some way or another.

Kindest Regards,
Phil.
ConcreteClasses_21.jpg
ConcreteClasses_21.jpg (22.89 KiB) Viewed 353 times
ConcreteClasses_22.jpg
ConcreteClasses_22.jpg (42.71 KiB) Viewed 353 times
NickFriend
Posts: 248
Joined: Fri Oct 14, 2016 7:09 am

Instantiation syntax - our own Classes etc.

Post by NickFriend »

This could get confusing!

Code: Select all

var person = new Person { FirstName = "Phil", LastName = "Hepburn" };
As you mention, this is really

Code: Select all

var person = new Person() { FirstName = "Phil", LastName = "Hepburn" };
ie. instantiating the object with a parameterless constructor and then setting the properties.

So for consistency I would have thought it ought to be more like

Code: Select all

var person := Person{} { FirstName := "Phil", LastName := "Hepburn" }
Otherwise it looks as though you're passing an array into the constructor. Also this way you get to start using curly brackets properly in X# ;-)

Nick
User avatar
robert
Posts: 4225
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

Instantiation syntax - our own Classes etc.

Post by robert »

Nick,

Code: Select all

var person = new Person { FirstName = "Phil", LastName = "Hepburn" };
As you mention, this is really

Code: Select all

var person = new Person() { FirstName = "Phil", LastName = "Hepburn" };
ie. instantiating the object with a parameterless constructor and then setting the properties.

So for consistency I would have thought it ought to be more like

Code: Select all

var person := Person{} { FirstName := "Phil", LastName := "Hepburn" }
Otherwise it looks as though you're passing an array into the constructor. Also this way you get to start using curly brackets properly in X# ;-)
Thanks for the suggestion. You are right about the confusion, especially since the class Person could have a constructor with an Array parameter. And in that case it would get really confusing (although the <Name> := <Expression> would normally not appear in a literal array).

Robert

Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
User avatar
robert
Posts: 4225
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

Instantiation syntax - our own Classes etc.

Post by robert »

Phil,
The intellisense thing is import, we are aware of that. But is also complicated, especially since you normally are mixing uncompiled (source) code with compiled code in referenced assemblies.

To reiterate (in C# syntax for now), there are 2 syntaxes that you can use to construct objects in C#:

Code: Select all

var oPerson := new Person( First = "Phil", Last = "Hepburn")
and

Code: Select all

var oPerson := new Person { FirstName = "Phil", LastName = "Hepburn"}
The first syntax uses (named) constructor arguments, the second syntax uses an object initializer.

The following pages on MsDn describes this as well:
https://msdn.microsoft.com/en-us/library/bb397680.aspx?f=255&MSPPError=-2147217396

The first syntax with the named arguments is supported in X# and requires the names to be the same as the argument names from the constructor.

The second syntax uses names that mutch match public (or internal) settable properties from the type. The second syntax also requires that there is a parameterless constructor for the type.
X# does not support the object initializer syntax yet, but we will add that (before Cologne, I am sure).

Intellisense should (and will eventually) list constructor parameters for the first method and public (settable) properties for second syntax.

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,

I am sure you will do me and all X# guys a good job, as usual. It all makes sense what you say and report back - its just a bit out of my league - experience, knowledge etc..

Help in Intellisense in some way(s) is very important I feel - as I found that when I did all my C# LINQ stuff I really found it most helpful, even deep into the LINQ query syntax, and the method syntax with Lambdas. The properties were all there ;-0)

Anything you can do to move us towards the C# situation will be most welcomed. I am sure you and Nikos will do us proud ;-0)

I can manage OK with what I have available at the moment, even driving VS without any intellisense at all !!!

Power to your elbow(s),

Regards,
Phil.

P.S. there are quite a few reasons to have to supply an empty Constructor - IIRC stuff related to WPF and data binding that I documented in the last two Cologne material set I provided.
Post Reply