Expanded function syntax
Hello,
(Sorry. What I meant to write is below.)
May I make a pitch for an expanded function syntax? This example will say it all (I hope).
What I have to write now:
getFirstChildOfElementWithTagName(Papa, 'baby');
What I'd like to be able to write:
getFirstChildOfElement(Papa)withTagName('baby');
function getFirstChildOfElement(Papa)withTagName(babyTagName) { alert(arguments[0].tagName); // Show Papa's tag name alert(arguments[1]); // Show desired baby's tag name }
I don't think such an expanded capability would have any backward compatibility issues. It would make function calls more readable and easier to remember.
Ciao -- Mark Filipak
Method overloading is the same thing as you know from C. When I said that, I was more trying to get to what your goal was in suggesting the feature. Do you want to be able to split this function into two optional pieces, or would it be always called with both parts? If the former, then proposed overloading (not sure if it was accepted in es4) would do the same thing, though your syntax might be nicer. If your idea was just to gain clarity in the purpose of each arguments then named arguments might fulfill the same thing.
I'm not sure I feel very compelled by the idea, and I'd prefer to have overloading and named arguments, as they are well understood and familiar to programmers from other languages. In any case, I am sure it is far too late to make significant syntax changes at this point...
Peter
On Jan 31, 2008, at 5:13 PM, Mark Filipak wrote:
Hello,
(Sorry. What I meant to write is below.)
May I make a pitch for an expanded function syntax? This example will say it all (I hope).
What I have to write now:
getFirstChildOfElementWithTagName(Papa, 'baby');
What I'd like to be able to write:
getFirstChildOfElement(Papa)withTagName('baby');
If you put a . after the first ) then you can write such a chained
expression today. It costs one more function object and one more .
character, but if it's important to you for clarity, those costs may
be worth paying.
function getFirstChildOfElement(Papa)withTagName(babyTagName) { alert(arguments[0].tagName); // Show Papa's tag name alert(arguments[1]); // Show desired baby's tag name }
I don't think such an expanded capability would have any backward compatibility issues. It would make function calls more readable and easier to remember.
It's unusual syntax; some including me would argue that it's less
readable. It's too late for a non-trivial sugaring such as this
without stronger motivation, in my opinion. Others should weight in
if they feel differently.
On Feb 1, 2008 12:01 PM, Brendan Eich <brendan at mozilla.org> wrote:
On Jan 31, 2008, at 5:13 PM, Mark Filipak wrote:
What I have to write now:
getFirstChildOfElementWithTagName(Papa, 'baby');
What I'd like to be able to write:
getFirstChildOfElement(Papa)withTagName('baby');
If you put a . after the first ) then you can write such a chained expression today. It costs one more function object and one more . character, but if it's important to you for clarity, those costs may be worth paying.
Also, I may not have the syntax exactly right, but I believe you can also do something like this:
function getFirstChild(argObj:{element:XML, tagName:AnyString}):XML { return element.(name() == tagName)[0]; }
var papa:XML; // ... var firstborn:XML = getFirstChild({element: papa, tagName: "baby"});
(Actually, in this case, the e4x syntax is so succinct that the function is kind of pointless, but you get the idea -- use a single argument, of an ad hoc record type, to clarify argument roles.)
Mr. Eich: Thank you for your suggestion. Unless I've been spending time with Mescalito, to implement
getFirstChildOfElement(Papa).withTagName('baby'),
since it has no idea which child is targeted, the first function would have to return an array of all children. Thus .withTagName() would have to be added as a method of Array. Correct? I'm not sure it would be wise to add the overhead as my example is but one of a great many such functions I would wish to create. BTW, I already use your suggested technique to emulate such things as getComputedStyle(Element,'').getPropertyValue(propName) in my IE6 DOM compatibility library.
Mr. Keesey: Thank you for your response. I can't begin to fathom what you have written <grin>.
And Mr. Hall: Thank you for your thoughts. I wish there was a clean way to add my pet functionality as I've always felt restricted by traditional function syntax but as you have pointed out, doing so would create problems.
Excuse the noise from the hinterland. I'm out of my depth. You folks carry on with your good work. I'm sure I'll be happy with Es4 and I will use it.
Before I sign out, may I ask if any of you have experience with Snobol? It is, in my opinion, a superior string processing language the core concepts of which would make an excellent addition to regular expressions.
, Mark Filipak
On Feb 1, 2008, at 5:28 PM, Mark Filipak wrote:
Mr. Eich: Thank you for your suggestion. Unless I've been spending
time with Mescalito, to implementgetFirstChildOfElement(Papa).withTagName('baby'),
since it has no idea which child is targeted, the first function
would have to return an array of all children. Thus .withTagName()
would have to be added as a method of Array. Correct?
No, you could do something that avoids copying. The return value of
getFirstChildOfElement does not need to be of type Array.
Before I sign out, may I ask if any of you have experience with
Snobol? It is, in my opinion, a superior string processing language
the core concepts of which would make an excellent addition to
regular expressions.
Snobol and its successor, Icon (are there other offspring?) are neat
languages. Food for thought in a future edition, since full Unicode
and Perl 5 regexp syntax are at odds, and we have not pulled the Perl
6 lever (incompatible, more concise regexp syntax).
On Jan 31, 2008 5:08 PM, Mark Filipak <markfilipak.ecmascript4 at gmail.com> wrote:
What I have to write now: getFirstChildOfElementWithTagName(Papa, 'baby');
If you're not writing something like $("baby:first", Papa), you're missing out on what the language already has to offer.
Hello,
May I make a pitch for an expanded function syntax? This example will say it all (I hope).
What I have to write now:
getFirstChildOfElementWithTagName(Papa, 'baby');
What I'd like to be able to write:
getFirstChildOfElement(myPapa)withTagName('baby');
function getFirstChildOfElement(Papa)withTagName(babyTagName) { alert(Papa.tagName); // Show Papa's tag name alert(babyTagName); // Show baby's desired tag name }
I don't think such an expanded capability would have any backward compatibility issues. It would make function calls more readable and easier to remember.
Thanks, and ciao -- Mark Filipak