Declaration syntax

deed none at none.none
Wed Jan 8 11:31:20 PST 2014


> Why have a function declaration take a different form than an 
> expression?
>
> h = sqrt(x*x+y*y)
> s = sin(theta)
>
> There's thousands of years of math behind that, we are taught 
> that form
> before we ever get near programming a computer.
>
> result = do_somthing_with(parameters)


Your example seems to show how the functions are used, not how 
they are declared. I'm only considering declarations and 
prototyping. After point 2 they would be declared like this, 
assuming double as parameter and return type for sqrt and sin:

sqrt double(x double) {}
sin  double(x double) {}
foo  const(int)[] (all const(int)[], newNum int, sum int) {}

And after point 3, the declaration would be:

sqrt(x)               double(double) {}
sin(x)                double(double) {}
foo(all, newNum, sum) const(int)[] (const(int)[], int, int) {}

Currently we have:

double sqrt(double x)
double sin(double x)
const(int)[] foo(const(int)[] all, int newNum, int sum) {}

With 20 of these methods inside a struct or class, heavily 
annotated, and the code folded to get an overview, one have to 
search in the middle of all the declarations to identify them. 
And you need to identify said methods, otherwise the information 
value is quite limited.

Consider

struct {
     private Data[]
     @property const nothrow bool
     @property Data
     void
}

struct {
     _data
     empty()
     front()
     popFront()
}

I believe the last part provides more overall information by 
itself than the first. Also, the information in the first part is 
of limited value alone; we want to know that empty is the method 
that can be used as a property, returns bool, won't change our 
struct and doesn't throw more than that the struct contains that 
annotation or that that annotation happens to be named empty. And 
if the last part is changed to

struct {
     _veryImportantData
     isDiskFull()
     lastAdded()
     deleteAll()
}

more overall information is provided than

struct {
     private VeryImportantData[]
     @property const nothrow bool
     @property VeryImportantData
     void
}

Therefore it feels natural to compose this as

struct {
     _data VeryImportantData[] private
     isDiskFull() bool() @property const nothrow
     lastChanged() VeryImportantData() @property
     deleteAll() void()
}

rather than

struct {
     private VeryImportantData[] _data
     @property bool isDiskFull() const nothrow
     @property VeryImportantData lastChanged()
     void() deleteAll
}

or instead of

struct {
     pure double  sin(double x)  nothrow
     immutable double  pi
     double  sqrt(double x)  nothrow @safe
     const(int)[]  foo(const(int)[] all, int newNum, int sum)
}

have

struct {
     sqrt(x)  double(double)  nothrow
     pi  immutable double
     sin(x)  double(double)
     foo(all, newNum, sum)  const(int)[] (const(int)[], int, int)
}


Anyway, I'm not trying to push this, I was just curious about the 
two questions in my initial post.


More information about the Digitalmars-d mailing list