Tuples - Getting more for your money !?
- Phil Hepburn
- Posts: 743
- Joined: Sun Sep 11, 2016 2:16 pm
Tuples - Getting more for your money !?
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 :-
Yes, 'Items' are what are within Tuples - Item1, Items2, ........ etc.
Now a little test of my own making :-
After seeing the definition above, lets now see some testing code using the structure :-
The notes in MSDN show how many variations of Tuples that we can have :-
And here is some detail of just one of those, it is that for three items :-
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.
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 :-
Yes, 'Items' are what are within Tuples - Item1, Items2, ........ etc.
Now a little test of my own making :-
After seeing the definition above, lets now see some testing code using the structure :-
The notes in MSDN show how many variations of Tuples that we can have :-
And here is some detail of just one of those, it is that for three items :-
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.
Tuples - Getting more for your money !?
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
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
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
- lumberjack
- Posts: 727
- Joined: Fri Sep 25, 2015 3:11 pm
- Location: South Africa
Tuples - Getting more for your money !?
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>
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.
The above a bit over simplified but is really powerful.
Johan
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
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
Johan
______________________
Johan Nel
Boshof, South Africa
Johan Nel
Boshof, South Africa
Tuples - Getting more for your money !?
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
and have the possibility to access the parameter values directly:
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
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>
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
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
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
- lumberjack
- Posts: 727
- Joined: Fri Sep 25, 2015 3:11 pm
- Location: South Africa
Tuples - Getting more for your money !?
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"]
Johan
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 Nel
Boshof, South Africa
Johan Nel
Boshof, South Africa
Tuples - Getting more for your money !?
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>
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
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
- lumberjack
- Posts: 727
- Joined: Fri Sep 25, 2015 3:11 pm
- Location: South Africa
Tuples - Getting more for your money !?
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
Johan
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 Nel
Boshof, South Africa
Johan Nel
Boshof, South Africa
Tuples - Getting more for your money !?
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:
and the calling code
the following code gives the tuple elements a name
And you can use them by their name
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
or
That way tuples are becoming much like Anonymous classes.
Robert
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
}
Code: Select all
var names = LookupName(id);
WriteLine($"found {names.Item1} {names.Item3}.");
Code: Select all
(string first, string middle, string last) LookupName(long id) // tuple elements have names
Code: Select all
var names = LookupName(id);
WriteLine($"found {names.first} {names.last}.");
"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)
Code: Select all
FUNCTION LookupName(id AS LONG) AS ( First AS STRING, Middle AS STRING, Last AS STRING)
Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
The Netherlands
robert@xsharp.eu
- Phil Hepburn
- Posts: 743
- Joined: Sun Sep 11, 2016 2:16 pm
Tuples - Getting more for your money !?
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.
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.