Tuples - Getting more for your money !?

Public forum to share code snippets, screen shorts, experiences, etc.
Post Reply
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Tuples - Getting more for your money !?

Post by Phil Hepburn »

Hi guys,

In following up my Generics research work at the moment, I came across 'Tuples'.

Yes, apparently 'Tuples' are seen as the new sexy thing in C#, for a lot of folks that is.

Now that we have in our xBase armoury all that C# con do, I thought I would digress from my Generics for a moment, and have a look at the syntax required within X# for Tuple use.

It seems like Tuples (structures) can be used simply when we need to return from a method (for example) more than one item of data - more than one value, object or collections.

Have any of you started to use Tuples ? And if so have you any nice examples you can share with the rest of the X# Forum ?

Here below are some images of Tuples - details and working code in use :-
Tuple_01.jpg
Tuple_01.jpg (30.59 KiB) Viewed 600 times
Yes, 'Items' are what are within Tuples - Item1, Items2, ........ etc.

Now a little test of my own making :-
Tuple_02.jpg
Tuple_02.jpg (32.23 KiB) Viewed 600 times
After seeing the definition above, lets now see some testing code using the structure :-
Tuple_03.jpg
Tuple_03.jpg (47.28 KiB) Viewed 600 times
Tuple_04.jpg
Tuple_04.jpg (47.86 KiB) Viewed 600 times
The notes in MSDN show how many variations of Tuples that we can have :-
Tuple_05.jpg
Tuple_05.jpg (31.45 KiB) Viewed 600 times
And here is some detail of just one of those, it is that for three items :-
Tuple_06.jpg
Tuple_06.jpg (41.52 KiB) Viewed 600 times
Can anyone take us further with Tuples and their use - and - can any one warn us about "over-use", you know, the sort of things we all do when finding something new.

I think the phrase is something along the lines of "... when what you have in your hand is a hammer, everything looks like a nail ..."

Remember Interfaces ??? They got a bit 'done to death' as I recall ;-0)

Any contributions - good, bad, verbal, visual etc., etc.. ???

Cheers, and Best regards,
Phil.
Wales, UK.
User avatar
wriedmann
Posts: 3755
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Tuples - Getting more for your money !?

Post by wriedmann »

Hi Phil,

yes, I had come across tuples some time ago, but never used them. They can be useful if you need more values as return value and don't like to define an own class for it. I see several uses for them....
In VO, I had used an array without all the advantages of strong typing.
In X#, for passing back and for multiple values, I use a Dictionary<string,object>, but of course I loose strong typing of the members, but can name them.

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
lumberjack
Posts: 727
Joined: Fri Sep 25, 2015 3:11 pm
Location: South Africa

Tuples - Getting more for your money !?

Post by lumberjack »

Wolfgang,

Are you using Dictionary<string, object> to use a string property inside the object to access it?

There is a class for that KeyedCollection<K, V>

Code: Select all

CLASS MyKeyCollection INHERIT KeyedCollecion<STRING, MyClass>
  METHOD GetKeyForItem(o AS MyClass) AS STRING
  RETURN o:Name
END CLASS
That is all code you have to write to use it.

Have also done some enhancement to it to work on certain classes to allow for using different properties in a class to build it on depending the requirement.

Code: Select all

CLASS MyKeyCollection INHERIT KeyedCollecion<STRING, MyClass>
  EXPORT Key AS STRING
  CONSTRUCTOR(k AS STRING)
    SUPER()
    Key := k
  RETURN
  PROTECTED METHOD GetKeyForItem(o AS MyClass) AS STRING
  RETURN o[Key]
END CLASS
CLASS MyClass
  PROTECT kvp AS KeyValuePair<STRING, STRING>
  PROPERTY SELF[k AS STRING] AS STRING GET SELF[k] SET SELF[k] := VALUE
END CLASS
The above a bit over simplified but is really powerful.

Johan
______________________
Johan Nel
Boshof, South Africa
User avatar
wriedmann
Posts: 3755
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Tuples - Getting more for your money !?

Post by wriedmann »

Hi Johan,

no, my parameters can have every type of value, therefore I have typed the dictionary as <string,object>.

Basically I'm using this for my messaging between viewmodels or views.

My message class is defined as

Code: Select all

class MessengerMessage implements IMessengerMessage
  protect _cMessage as string
  protect _oSender as IMessengerClient
  protect _oReceiver as IMessengerClient
  protect _oData as object
  protect _oParameters as Dictionary<string,object>
and have the possibility to access the parameter values directly:

Code: Select all

public method AddParameter( cName as string, oValue as object ) as void 
	
if _oParameters == null
  _oParameters := Dictionary<string,object>{}
endif
if _oParameters:ContainsKey( cName )
  _oParameters[cName] := oValue
else
  _oParameters:Add( cName, oValue )
endif
	
return
	
public virtual method GetParameter( cName as string ) as object
local oValue as object
	
if _oParameters:ContainsKey( cName )
  oValue := _oParameters[cName]
else
  oValue := null
endif
	
return oValue
so the internal implementation of the various parameters in the message class is hidden to the user.
Of course, the GetParameter() method could be implemented using generics....

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
lumberjack
Posts: 727
Joined: Fri Sep 25, 2015 3:11 pm
Location: South Africa

Tuples - Getting more for your money !?

Post by lumberjack »

Hi Wolfgang,
I use a very similar approach to you. For your specific example I don't have a Get/PutParameter() method but implement it as follows as a property, less code to type... o["PropertyName"]

Code: Select all

PROPERTY SELF[name AS STRING] AS OBJECT
  GET
    // your GetParameter code here
  END  GET
  SET
    // Your PutParameter code here
  END SET
END PROPERTY
Johan
______________________
Johan Nel
Boshof, South Africa
User avatar
wriedmann
Posts: 3755
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Tuples - Getting more for your money !?

Post by wriedmann »

Hi Johan,

thanks! This was again a demonstration that I have a LOT to learn!

Your solution seems much better to my that mine... Maybe I change my interface and implementation - there is not so much code to change.... And thanks to interfaces and strong typing the compiler gives me the places where I have to change my code.

Wolfgang

P.S. maybe I'm too fixed at my VO programming style <g>
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
lumberjack
Posts: 727
Joined: Fri Sep 25, 2015 3:11 pm
Location: South Africa

Tuples - Getting more for your money !?

Post by lumberjack »

Wolfgang,
You welcome.
What I actually do is adding my "late bound" parameters as part of the constructor. Then if I get/set the parameter by accident incorrectly by typo it will throw an error in the GET / SET, parameter not found.

Nothing wrong with the VO thinking, I still have a late bound assembly in .net that use the NoIVarGet()/NoIVarPut()/NoMethod() methods of VO. Have in that a wrapper class around macro compiled codeblocks so that I can do

Code: Select all

{|o| o:Par1 + o:Par2 * o:Par3 } // Where o is the codeblock itself.
Johan
______________________
Johan Nel
Boshof, South Africa
User avatar
robert
Posts: 4520
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

Tuples - Getting more for your money !?

Post by robert »

Phil,

C# 7 has added Tuples in a lot of other places as well. You can specify that a method returns a tuple of strings like this:

Code: Select all

(string, string, string) LookupName(long id) // tuple return type
{
    ... // retrieve first, middle and last from data storage
    return (first, middle, last); // tuple literal
}

and the calling code

Code: Select all

var names = LookupName(id);
WriteLine($"found {names.Item1} {names.Item3}.");
the following code gives the tuple elements a name

Code: Select all

(string first, string middle, string last) LookupName(long id) // tuple elements have names
And you can use them by their name

Code: Select all

var names = LookupName(id);
WriteLine($"found {names.first} {names.last}.");
Nikos is in the process of merging our codebase with the C# 7 Roslyn codebase. That will allow us to add features like this to X# as well.
"All" we have to do is define a proper syntax and glue our parser front end to the Roslyn backend.

Maybe something like this

Code: Select all

FUNCTION LookupName(id AS LONG) AS (STRING,STRING,STRING)
or

Code: Select all

FUNCTION LookupName(id AS LONG) AS ( First AS STRING, Middle AS STRING, Last AS STRING)
That way tuples are becoming much like Anonymous classes.

Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Tuples - Getting more for your money !?

Post by Phil Hepburn »

Hi Robert,

Very interesting - thanks for the heads up.

I like (and prefer) the second syntax as it removes the need for the coder to get the position of the data/variable correct.

One thing I did not like CSV stuff for was the weak link between place and meaning.

And that is why we all like Classes - the data position is taken out of the equation.

Just now struggling with Interfaces and Generics to make my own Data Access system. hard the first time you get your head around this ;-0((

Cheers,
Phil.
Post Reply