[nem-en] Tupled . operator - what do you think?
Andrey Khropov
andrey.khropov at gmail.com
Thu Nov 23 01:09:06 CET 2006
Hello all,
I recently came up with the idea (examples are somewhat motivated by
http://en.wikibooks.org/wiki/Haskell/YAHT/Language_advanced#Pattern_Matching).
Sometimes we want to write a match against several properties of some object:
--------------------------------------------------------------------
[Record] class Color { public r: int; public g: int; public b: int; }
def myColor = Color(255,0,0);
def isBright = match( (myColor.r, myColor.g, myColor.b) )
{
| (255,_,_)
| (_,255,_)
| (_,_,255) =>
true
| _ =>
false
}
--------------------------------------------------------------------
or even worse, when we obtain the object through some (possibly expensive)
method call (Color is a property) and it's up to a compiler to optimize this to
one call to 'Color' property:
--------------------------------------------------------------------
def isBright = match( (myWindow.Color.r, myWindow.Color.g, myWindow.Color.b) )
{
| (255,_,_)
| (_,255,_)
| (_,_,255) =>
true
| _ =>
false
}
--------------------------------------------------------------------
of course this example should be rewritten as
--------------------------------------------------------------------
def windowColor = myWindow.Color;
def isBright = match( (windowColor.r, windowColor.g, windowColor.b) )
{
| (255,_,_)
| (_,255,_)
| (_,_,255) =>
true
| _ =>
false
}
--------------------------------------------------------------------
but nevertheless it's quite wordy and one extra variable.
So, I thought maybe it's possible to introduce tupled '.' operator that returns
a tuple of methods' results or values or properties called for this object:
--------------------------------------------------------------------
def isBright = match( myWindow.Color.(r,g,b) )
{
| (255,_,_)
| (_,255,_)
| (_,_,255) =>
true
| _ =>
false
}
--------------------------------------------------------------------
One another use-case:
--------------------------------------------------------------------
class Person {
...
public Name : string { set; }
public Surname : string { set; }
...
}
...
def employee = Person(); // for some reason we want to delay initialization
...
employee.(Name,Surname) = ("John","Smith");
--------------------------------------------------------------------
--
AKhropov
More information about the devel-en
mailing list