| Thread overview | ||||||
|---|---|---|---|---|---|---|
|
November 22, 2008 Functions as Struct Properties | ||||
|---|---|---|---|---|
| ||||
Hi, We already have the notation: int[] array; void foo(int[] a, int x); foo(array, 3); array.foo(3); // means the same thing so why not extend this to struct first arguments? --Sam | ||||
November 22, 2008 Re: Functions as Struct Properties | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Sam S E | "Sam S E" <eisenstat.aa@sympatico.ca> wrote in message news:gga2ua$15v7$1@digitalmars.com... > Hi, > > We already have the notation: > > int[] array; > void foo(int[] a, int x); > > foo(array, 3); > array.foo(3); // means the same thing > > so why not extend this to struct first arguments? > --Sam A lot of people want that to be extended to all types. It just doesn't seem to be happening for some reason. Another popular approach (my preference) is something closer to C#'s way of doing it: int x; void foo(extension int a, int b); void bar(int a, int b); foo(x, 3); // Ok x.foo(3); // Ok bar(x, 3); // Ok x.bar(3); // Error | |||
November 23, 2008 Re: Functions as Struct Properties | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Nick Sabalausky | Nick Sabalausky Wrote:
> "Sam S E" <eisenstat.aa@sympatico.ca> wrote in message news:gga2ua$15v7$1@digitalmars.com...
> > Hi,
> >
> > We already have the notation:
> >
> > int[] array;
> > void foo(int[] a, int x);
> >
> > foo(array, 3);
> > array.foo(3); // means the same thing
> >
> > so why not extend this to struct first arguments?
> > --Sam
>
> A lot of people want that to be extended to all types. It just doesn't seem to be happening for some reason. Another popular approach (my preference) is something closer to C#'s way of doing it:
>
> int x;
> void foo(extension int a, int b);
> void bar(int a, int b);
>
> foo(x, 3); // Ok
> x.foo(3); // Ok
>
> bar(x, 3); // Ok
> x.bar(3); // Error
>
>
That's a good idea, but I would put the keyword on the second case, since you would rarely want to restrict the syntax, causing less typing. I also realized that this could enable a more flexible op overloading syntax:
static Foo opAdd(invariant Foo x, int y)
{
...
}
Using current syntax, you can't make the Foo invariant. This has been causing problems with porting a C++ program to D, since C++ supports:
class Foo
{
...
Foo opAdd(int) const
{
...
}
}
--Sam
| |||
November 23, 2008 Re: Functions as Struct Properties | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Sam S E | On Sat, 22 Nov 2008 18:00:26 -0500, Sam S E <eisenstat.aa@sympatico.ca> wrote: > Hi, > > We already have the notation: > > int[] array; > void foo(int[] a, int x); > > foo(array, 3); > array.foo(3); // means the same thing > > so why not extend this to struct first arguments? > --Sam This was part of the "Future of D" talk at the D conference (see Uniform function types, page 9 http://s3.amazonaws.com/dconf2007/WalterAndrei.pdf ) | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply