Forums

alternate syntax for functions

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

alternate syntax for functions

Postby RIGHT_THEN » Tue Apr 19, 2011 12:48 pm

Gentlemen

curently all the languages that i know support function syntax like
FunctionName then arguments f1(ar1,ar2) etc..

but would it not be interesting if function syntax could work like fill in the blanks too
for example
object Reset( object object_to_set, object value_to_set )
{
object_to_set = value_to_set;
return object_to_set;
}


now calling becomes

bool was_here = false ;
Reset(was_here,true);

but had it been like this
Reset_(was_here)_to_(true); //nice isnt it


so defination can now become
object Reset_(object object_to_set)_to_(object value_to_set)
{
object_to_set = value_to_set;
return object_to_set;

}

wont this make such a readable beautiful code.

i wish this feature be implemented in cobra with syntax being if user wants such functions
than "_" be used to join "(" as done above if you find it confusing to do like
object Reset(object object_to_set)to(object value_to_set)


their is nothing extraordinary about its design at the 
back end cobra can still produce it like this by joining names
[SpecialFunctionDefinition(Reset_(object object_to_set)_to_(object value_to_set)]
object Reset_to(object object_to_set,object value_to_set)
{
object_to_set = value_to_set;
return object_to_set;

}


think about other possibilities
one can finally write english sentences to formulate code.

if_(array)_has_multiple_arrays_inside_it_return_(out that_array)_else_return_null ;

-----------------------its def can be-----------------


object if_(Array array)_has_multiple_arrays_inside_it_return_(out Array that_array)_else_return_null
{
that_array = null ;
var x = array.GetEnumerator();
if ( x.MoveNext )
{
var current_elmt = x.Current ;
if ( current_elmt is Array)
that_array = current_elmt ;
}
return that_array ;
}
Backend:-
[SpecialFunctionDefinition(if_(Array array)_has_multiple_arrays_inside_it_return_(out Array that_array)_else_return_null
)]
object if_has_multiple_arrays_inside_it_return_else_return_null(Array array,out Array that_array){
;
}


or another atribute could be introduced as to what name user would like its function to have at the backend

[SpecialFunctionDefinition(if_(Array array)_has_multiple_arrays_inside_it_return_(out Array that_array)_else_return_null
)]
[FunctionNameVisibleToBackendAs( if_Array_has_multiple_arrays_inside_it_return_Array_else_return_null )]
object if_Array_has_multiple_arrays_inside_it_return_Array_else_return_null(Array array, out Array that_array)
{
;
}


function names can clearly explain their intent with no need for extra comments


Thanking_You
RIGHT_THEN
RIGHT_THEN
 
Posts: 99

Re: alternate syntax for functions

Postby Charles » Fri Apr 22, 2011 6:55 pm

What you're asking for already exists in Smalltalk and derivatives such as Objective-C. They use colons (:) for separation and they are considered part of the method name. Also, Python has this to some extent with keyword arguments.
Code: Select all
# Smalltalk
stuff at: 1 put: 10

# Objective-C
[stuff insertObject:@10 atIndex:1]

# Python
stuff.insert(10, at=1)


Btw the real Python call is "stuff.insert(1, 10)". I just made up the above to show a keyword arg using the same kind of method in the other examples.

Regarding Cobra, I think we're more likely to take the Python keyword approach than anything else because it fits in with the rest of the syntax.

HTH.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: alternate syntax for functions

Postby RIGHT_THEN » Sat Apr 23, 2011 9:05 am

Hey Charles

Seems you are back from vacations.
all the languages i know all = utterly few in my case.

so thanks for introducing smalltalk`s feature.
good if smalltalk has such attributes to it.but can it be that
expressive as i intended it to be?

it seems you are wanting it to keep near python.
well its your baby. but it would have been great...

Thanking_You
RIGHT_THEN
RIGHT_THEN
 
Posts: 99

Re: alternate syntax for functions

Postby Charles » Sun Apr 24, 2011 10:11 am

One thing that has influenced me is the large number of methods that require 0 - 1 arguments, as well as the large number of methods for which the 2 arguments are easy to remember including their order. I even went so far as to write a parser for Objective-C header files to compute the % of methods that took 0, 1, 2 or more arguments. That was many years ago and I no longer have the numbers handy, but methods with 0 - 1 arguments dominate.

For methods that require many arguments, you can use a "parameter object".
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: alternate syntax for functions

Postby RIGHT_THEN » Mon Apr 25, 2011 9:29 pm

hey Charles,

sure params can be used to pass multiple objects.
but that is not the point.
Intension was to write code as English sentences.

which could have been expanded to other aspects of the language too

Code: Select all
forEachItem_In_(List<int> Collection)_DivideItemBy_(int num)_And_Return_(out List<int> result)
                  |
                  |
                  |
forEachItem_In_(numbers)_DivideItemBy_(2)_And_Return_(result)

this is still a function not the foreach loop. we are providing parameters
where we intened to and not all the way in the end. i find it very readable.
althou i am not pressing you for it.

but if at all this feature was there in the language it does not mean that the old way
to write functions should be deprecated both can coexist together.

this can come in handy when where at times one feels that i am unable to
present my intent clearly where i want to and instead i am writing
multiple line of code justifing my intent. but instead it is becoming
a cluter.

and i know what you might say about gaining experience with coding.
but still....

Thanking_You
RIGHT_THEN
RIGHT_THEN
 
Posts: 99

Re: alternate syntax for functions

Postby Charles » Thu Apr 28, 2011 12:14 am

When you said "Intension was to write code as English sentences"--if you really meant that, then there is a lot more work to be done to accomplish that. You would need a whole new parser and consider how all the elements work together both technically and qualitatively. We have a Simple English Parser Sample if you are looking for something to start with. You would have to extend it and then figure out how to interpret it or translate, what kinds of errors to check for, etc.

Regarding methods like:
forEachItem_In_(List<int> Collection)_DivideItemBy_(int num)_And_Return_(out List<int> result)

...I think that will not work in the long run for multiple reasons. One is that every time you change the implementation of a method, you would have to rename it. Another is that the method name would be too long for larger methods. Also, it reveals the details of the method when, in fact, a method is typically encapsulates its implementation which may change in details over time.

Nevertheless you may be interested to know that back in the 1990's I was thinking about the Smalltalk message passing style such as:
Code: Select all
2 raisedTo: 16
album repeatTracksFrom: 1 to: 10

And was wondering how the syntax could be set up so that messages could have spaces in them. I came up with:
Code: Select all
2 raised to (16)
album repeat tracks from (1) to (10)

# or if variables can also have spaces, you need a comma to segregate the receiver:
2, raised to (16)
album, repeat tracks from (1) to (10)

This is reminiscent of your suggestion. But I don't think this is the right syntax for Cobra. Also, I never implemented the idea and it's hard to say if it would work out in practice without trying it.

You might be interested in 4GL languages. Sadly, the Wikipedia page is light on examples.

Interesting stuff.

Regarding making the intent of a method clear, I highly recommend the form "verbDirectObject" whenever possible and keeping methods reasonably small and focused in purpose. HTH.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: alternate syntax for functions

Postby RIGHT_THEN » Fri Apr 29, 2011 8:52 am

i can see you wont be convinced.
oh well..

Thanking_You
RIGHT_THEN
RIGHT_THEN
 
Posts: 99


Return to Discussion

Who is online

Users browsing this forum: No registered users and 45 guests