Thread overview | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
December 17, 2006 Structs and Variadic Functions | ||||
---|---|---|---|---|
| ||||
I think combining structs with variadic functions can introduce some new interesting possibilities. struct SS{ int a, b, c; } static SS obj = {10, 20, 30}; // Full initalization static SS obj = {a : 15, c : 35}; // Partial initalization Apply this to a variadic function: void func(SS arg ...){ writefln(arg.a); writefln(arg.b); writefln(arg.c); } int main(){ func(15, 30, 45); // Full initalization func(b : 45, c : 30); // Partial initalization } In response to my last two suggestions: Using aliases for constants - I greatly misunderstood how 'const' works in D. As long as you initalize the variable, D forces the expression to be constant. static int a; const int* ptr = &a; // OK const int* ptr = new int; // Error: non-constant expression new int Virtual Import - I didn't think this was a great idea anyways. I was just trying to suggest a way to define aliases for modules. |
December 17, 2006 Re: Structs and Variadic Functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Xinok | "Xinok" <xknnet@gmail.com> wrote in message news:em4ctn$hhu$1@digitaldaemon.com... >I think combining structs with variadic functions can introduce some new > interesting possibilities. > > struct SS{ > int a, b, c; > } > static SS obj = {10, 20, 30}; // Full initalization > static SS obj = {a : 15, c : 35}; // Partial initalization > > > Apply this to a variadic function: > void func(SS arg ...){ > writefln(arg.a); > writefln(arg.b); > writefln(arg.c); > } > > int main(){ > func(15, 30, 45); // Full initalization > func(b : 45, c : 30); // Partial initalization > } I really like that. It also wouldn't impose any extra overhead, since the size of the structure on the stack would be the same size as if you were to pass those parameters, and accessing those struct members in the function would also be virtually the same as accessing parameters. |
December 19, 2006 Re: Structs and Variadic Functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Xinok | I would also like to see this. One of my favourite things about js, is that I can send it an assoc array of whatever I want... functions, floats, strings, integers, and the ones I don't send are null. having that for functions would be AMAZING... I think it's not that hard to implement either. I suppose one could push on the stack a list of pointers in the order they appear in the struct... the null ones being uninitialized. then, inside of the function, the values will have to be loaded, grabbing the value of a pointer. Technically, you can already do something similar though, by setting default values for all params to the function. 1. but, you can't call them unordered, referencing only the names. 2. if you know you want the first and third argument initialized, then it's weird to skip the second. hmmmmm. this is a really good idea. |
December 20, 2006 Re: Structs and Variadic Functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kenny | The main use I saw for this feature was passing properties to a function. Take for example, a file class: class File{ private struct FProp{ string path; // Path to the file to open bool read, write; // Open file to read or write int cache; // File cache bool temp; // Temporary file? bool binary; // Open file in binary mode? } this(FProp prop ...); // Constructor } int main(){ File ifile(path : "C:\\in.dat", read : true, cache : 4096); File ofile(path : "C:\\out.dat", write : true, cache : 0, temp : true); } You only set the properties you need, and in any order you want. |
December 21, 2006 Re: Structs and Variadic Functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Xinok | Xinok wrote:
> I think combining structs with variadic functions can introduce some new
> interesting possibilities.
>
> struct SS{
> int a, b, c;
> }
> static SS obj = {10, 20, 30}; // Full initalization
> static SS obj = {a : 15, c : 35}; // Partial initalization
>
>
> Apply this to a variadic function:
> void func(SS arg ...){
> writefln(arg.a);
> writefln(arg.b);
> writefln(arg.c);
> }
>
> int main(){
> func(15, 30, 45); // Full initalization
> func(b : 45, c : 30); // Partial initalization
> }
>
Unless I misunderstand you, it seems you are suggesting named parameters. Some other languages have this already (for example, OCaml), but it doesn't require structs to do this. You can pick up the names of parameters from the prototype, and we already have support for default parameters. All you need now is support for naming parameters at the call-site, for which your syntax seems perfectly suitable. Then, you could avoid variadic functions and structs and just write:
FilePtr openFile(char[] filename, bool read=true, bool write=true, bool append=false, bool anotherParam=false) {...}
int main(){
openFile(filename : "asdf.txt", append : true);
// Equivalent to openFile("asdf.txt", true, true, true, false);
}
Which is certainly a nice syntax feature to have.
Cheers,
Reiner
|
December 21, 2006 Re: Structs and Variadic Functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Reiner Pope | On Thu, 21 Dec 2006 07:46:14 +0200, Reiner Pope <xxxxxx@xxx.xx> wrote:
[snip]
> Unless I misunderstand you, it seems you are suggesting named parameters. Some other languages have this already (for example, OCaml), but it doesn't require structs to do this. You can pick up the names of parameters from the prototype, and we already have support for default parameters. All you need now is support for naming parameters at the call-site, for which your syntax seems perfectly suitable. Then, you could avoid variadic functions and structs and just write:
>
> FilePtr openFile(char[] filename, bool read=true, bool write=true, bool append=false, bool anotherParam=false) {...}
>
> int main(){
> openFile(filename : "asdf.txt", append : true);
> // Equivalent to openFile("asdf.txt", true, true, true, false);
> }
>
> Which is certainly a nice syntax feature to have.
>
> Cheers,
>
> Reiner
Yes, indeed. I guess named parameters have been suggested before (in this NG)? Does Walter see it as a potential 2.0+ feature?
|
December 21, 2006 Re: Structs and Variadic Functions (Keyword arguments) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Reiner Pope | Reiner Pope wrote: > Xinok wrote: >> I think combining structs with variadic functions can introduce some new >> interesting possibilities. >> >> struct SS{ >> int a, b, c; >> } >> static SS obj = {10, 20, 30}; // Full initalization >> static SS obj = {a : 15, c : 35}; // Partial initalization >> >> >> Apply this to a variadic function: >> void func(SS arg ...){ >> writefln(arg.a); >> writefln(arg.b); >> writefln(arg.c); >> } >> >> int main(){ >> func(15, 30, 45); // Full initalization >> func(b : 45, c : 30); // Partial initalization >> } >> > Unless I misunderstand you, it seems you are suggesting named parameters. Some other languages have this already (for example, OCaml), but it doesn't require structs to do this. You can pick up the names of parameters from the prototype, and we already have support for default parameters. All you need now is support for naming parameters at the call-site, for which your syntax seems perfectly suitable. Then, you could avoid variadic functions and structs and just write: > > FilePtr openFile(char[] filename, bool read=true, bool write=true, bool append=false, bool anotherParam=false) {...} > > int main(){ > openFile(filename : "asdf.txt", append : true); > // Equivalent to openFile("asdf.txt", true, true, true, false); > } > > Which is certainly a nice syntax feature to have. This has been suggested several times. It may be possible. But I recall Walter was uncomfortable with parameter names in prototypes becoming significant all the sudden when they aren't in C/C++. Prototypes currently don't require a parameter to have a name at all. Then there's the added difficulty of figuring out overloading. If you have two functions with a parameter called 'foo' which one do you call for func(foo:23). Some new rules for what's the best match are probably needed. I don't think the issues are insurmountable, but they do require some work. And a willingness on everyone's part to accept that the _name_ of a parameter is now part of the public interface and thus changing a parameter name can break code. I don't recall about OCaml, but Python at least has keyword arguments but doesn't have overloading, so it doesn't have to deal with the overload/keyword interaction. Also I'd say that if regular functions get call-by keyword capability, then I would hope that templates get it too. So that may have some implications on the syntax used. If "keyword : value" is used for functions, then probably something different would have to be used for templates, since : already means specialization there. It would be nice if the same syntax could be used for both. Here are some links -- Thread started by Chris Sauls Jun 2005: http://www.digitalmars.com/d/archives/digitalmars/D/25835.html Thread started by me May 2005: http://www.digitalmars.com/d/archives/digitalmars/D/23172.html Thread started by Norbert Nemec, June 2004 http://www.digitalmars.com/d/archives/digitalmars/D/4543.html Thread started by Eric Shumard, May 2004 http://www.digitalmars.com/d/archives/digitalmars/D/752.html And this one's not about keyword args, but about default parameters and function pointers in general. Started by Don Clugston. http://www.digitalmars.com/d/archives/digitalmars/D/27291.html --bb |
January 09, 2007 Re: Structs and Variadic Functions (Keyword arguments) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bill Baxter | Bill Baxter wrote:
> And this one's not about keyword args, but about default parameters and function pointers in general. Started by Don Clugston.
> http://www.digitalmars.com/d/archives/digitalmars/D/27291.html
Wow! That was my third ever D post!
|
January 09, 2007 Re: Structs and Variadic Functions (Keyword arguments) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Don Clugston | Don Clugston Wrote:
> Bill Baxter wrote:
> > And this one's not about keyword args, but about default parameters and function pointers in general. Started by Don Clugston. http://www.digitalmars.com/d/archives/digitalmars/D/27291.html
>
> Wow! That was my third ever D post!
While this post is still here, I'd like to make this suggestion: We have the 'default' keyword, let's use it!
void func(int a = 15, int b = 30, int c = 45);
func(45, default, 60);
|
Copyright © 1999-2021 by the D Language Foundation