Optional parameters?

2018-04-01 Thread Steven Schveighoffer via Digitalmars-d-learn
I currently have a situation where I want to have a function that accepts a parameter optionally. I thought maybe Nullable!int might work: void foo(Nullable!int) {} void main() { foo(1); // error int x; foo(x); // error } Apparently, I have to manually wrap an int to get it to pass.

Re: Optional parameters?

2018-04-01 Thread Jacob Carlborg via Digitalmars-d-learn
On 2018-04-01 17:54, Steven Schveighoffer wrote: I currently have a situation where I want to have a function that accepts a parameter optionally. I thought maybe Nullable!int might work: void foo(Nullable!int) {} void main() {    foo(1); // error    int x;    foo(x); // error } Apparentl

Re: Optional parameters?

2018-04-01 Thread Alex via Digitalmars-d-learn
On Sunday, 1 April 2018 at 15:54:16 UTC, Steven Schveighoffer wrote: void main() { foo(1); // error int x; foo(x); // error } For the first line, I had the same problem a while ago... https://issues.dlang.org/show_bug.cgi?id=15792

Re: Optional parameters?

2018-04-01 Thread Jonathan M Davis via Digitalmars-d-learn
On Sunday, April 01, 2018 11:54:16 Steven Schveighoffer via Digitalmars-d- learn wrote: > I currently have a situation where I want to have a function that > accepts a parameter optionally. > > I thought maybe Nullable!int might work: > > void foo(Nullable!int) {} > > void main() > { > foo(1);

Re: Optional parameters?

2018-04-01 Thread Boris-Barboris via Digitalmars-d-learn
On Sunday, 1 April 2018 at 15:54:16 UTC, Steven Schveighoffer wrote: I currently have a situation where I want to have a function that accepts a parameter optionally. I would simply use a pointer for this. Fighting D grammar seems too much of a hassle for such simple task.

Re: Optional parameters?

2018-04-01 Thread Jonathan M Davis via Digitalmars-d-learn
On Sunday, April 01, 2018 22:06:57 Boris-Barboris via Digitalmars-d-learn wrote: > On Sunday, 1 April 2018 at 15:54:16 UTC, Steven Schveighoffer > > wrote: > > I currently have a situation where I want to have a function > > that accepts a parameter optionally. > > I would simply use a pointer for

Re: Optional parameters?

2018-04-01 Thread Seb via Digitalmars-d-learn
On Sunday, 1 April 2018 at 15:54:16 UTC, Steven Schveighoffer wrote: I currently have a situation where I want to have a function that accepts a parameter optionally. I thought maybe Nullable!int might work: void foo(Nullable!int) {} void main() { foo(1); // error int x; foo(x); // e

Re: Optional parameters?

2018-04-01 Thread Boris-Barboris via Digitalmars-d-learn
On Sunday, 1 April 2018 at 22:25:45 UTC, Jonathan M Davis wrote: How would a pointer help? Instead of doing foo(nullable(42)) he'd have to do foo(new int(42)) which is just one character shorter and ends up allocating on the heap, unlike with Nullable. - Jonathan M Davis foo(&x);

Re: Optional parameters?

2018-04-01 Thread Jonathan M Davis via Digitalmars-d-learn
On Sunday, April 01, 2018 22:37:17 Boris-Barboris via Digitalmars-d-learn wrote: > On Sunday, 1 April 2018 at 22:25:45 UTC, Jonathan M Davis wrote: > > How would a pointer help? Instead of doing > > > > foo(nullable(42)) > > > > he'd have to do > > > > foo(new int(42)) > > > > which is just one ch

Re: Optional parameters?

2018-04-01 Thread Jonathan M Davis via Digitalmars-d-learn
On Sunday, April 01, 2018 22:34:16 Seb via Digitalmars-d-learn wrote: > On Sunday, 1 April 2018 at 15:54:16 UTC, Steven Schveighoffer > > wrote: > > I currently have a situation where I want to have a function > > that accepts a parameter optionally. > > > > I thought maybe Nullable!int might work:

Re: Optional parameters?

2018-04-01 Thread Boris-Barboris via Digitalmars-d-learn
On Sunday, 1 April 2018 at 22:44:45 UTC, Jonathan M Davis wrote: Which doesn't work in @safe code and doesn't work when you have an rvalue as you would when passing 42. Ultimately, using pointers ultimately either requires explicitly allocating stuff on the heap to be able to pass rvalues, or i

Re: Optional parameters?

2018-04-01 Thread Ali via Digitalmars-d-learn
On Sunday, 1 April 2018 at 15:54:16 UTC, Steven Schveighoffer wrote: I currently have a situation where I want to have a function that accepts a parameter optionally. why not simply use function overloading?

Re: Optional parameters?

2018-04-01 Thread Norm via Digitalmars-d-learn
On Sunday, 1 April 2018 at 15:54:16 UTC, Steven Schveighoffer wrote: I currently have a situation where I want to have a function that accepts a parameter optionally. I thought maybe Nullable!int might work: void foo(Nullable!int) {} void main() { foo(1); // error int x; foo(x); // e

Re: Optional parameters?

2018-04-02 Thread Timoses via Digitalmars-d-learn
On Sunday, 1 April 2018 at 15:54:16 UTC, Steven Schveighoffer wrote: I currently have a situation where I want to have a function that accepts a parameter optionally. I thought maybe Nullable!int might work: void foo(Nullable!int) {} void main() { foo(1); // error int x; foo(x); // e

Re: Optional parameters?

2018-04-02 Thread Cym13 via Digitalmars-d-learn
On Monday, 2 April 2018 at 09:31:35 UTC, Timoses wrote: On Sunday, 1 April 2018 at 15:54:16 UTC, Steven Schveighoffer wrote: I currently have a situation where I want to have a function that accepts a parameter optionally. I thought maybe Nullable!int might work: void foo(Nullable!int) {} vo

Re: Optional parameters?

2018-04-02 Thread Steven Schveighoffer via Digitalmars-d-learn
On 4/1/18 12:00 PM, Jacob Carlborg wrote: Yeah, D doesn't allow user defined implicit conversions, which I think is required for this. I would make function overloading even more complex than it is today. Although it would be really handy for cases like this. Not necessarily implicit con

Re: Optional parameters?

2018-04-02 Thread Steven Schveighoffer via Digitalmars-d-learn
On 4/2/18 5:31 AM, Timoses wrote: On Sunday, 1 April 2018 at 15:54:16 UTC, Steven Schveighoffer wrote: I currently have a situation where I want to have a function that accepts a parameter optionally. I thought maybe Nullable!int might work: void foo(Nullable!int) {} void main() {    foo(1);

Re: Optional parameters?

2018-04-02 Thread Steven Schveighoffer via Digitalmars-d-learn
On 4/1/18 6:01 PM, Jonathan M Davis wrote: On Sunday, April 01, 2018 11:54:16 Steven Schveighoffer via Digitalmars-d- learn wrote: I currently have a situation where I want to have a function that accepts a parameter optionally. I thought maybe Nullable!int might work: void foo(Nullable!int) {

Re: Optional parameters?

2018-04-04 Thread Dejan Lekic via Digitalmars-d-learn
On Sunday, 1 April 2018 at 15:54:16 UTC, Steven Schveighoffer wrote: I currently have a situation where I want to have a function that accepts a parameter optionally. This is what function overloading and/or default values are for, right?

Re: Optional parameters?

2018-04-04 Thread Timoses via Digitalmars-d-learn
On Wednesday, 4 April 2018 at 08:08:40 UTC, Dejan Lekic wrote: On Sunday, 1 April 2018 at 15:54:16 UTC, Steven Schveighoffer wrote: I currently have a situation where I want to have a function that accepts a parameter optionally. This is what function overloading and/or default values are for

Re: Optional parameters?

2018-04-04 Thread Dukc via Digitalmars-d-learn
On Sunday, 1 April 2018 at 15:54:16 UTC, Steven Schveighoffer wrote: But I'd rather avoid such things if possible. Is there a way around this? Seems rather limiting that I can do: Is this what you're looking for? void foo(Nullable!int x = Nullable!int.init) { if (!x.isNull) x.get.writeln;

Re: Optional parameters?

2018-04-04 Thread Steven Schveighoffer via Digitalmars-d-learn
On 4/4/18 8:59 AM, Dukc wrote: Is this what you're looking for? See my original post: I know I can do things like this: void foo(int x) { return foo(nullable(x)); } -Steve

Re: Optional parameters?

2018-04-04 Thread Dukc via Digitalmars-d-learn
On Wednesday, 4 April 2018 at 15:44:34 UTC, Steven Schveighoffer wrote: See my original post: I know I can do things like this: void foo(int x) { return foo(nullable(x)); } -Steve Oops, I read only the body of that function and thought it's a main function constructiong nullable when call

delegate with optional parameters

2017-04-02 Thread Inquie via Digitalmars-d-learn
is it possible to create a delegate that takes an optional number of parameters and/or return type? T delegate(S...)(S) special_delegate; I guess this is impossible?

Re: delegate with optional parameters

2017-04-02 Thread Basile B. via Digitalmars-d-learn
On Sunday, 2 April 2017 at 19:24:14 UTC, Inquie wrote: is it possible to create a delegate that takes an optional number of parameters and/or return type? T delegate(S...)(S) special_delegate; I guess this is impossible? alias Dg(Return, Params...) = Return delegate(Params); Dg!(int,float,

Re: delegate with optional parameters

2017-04-02 Thread Inquie via Digitalmars-d-learn
On Sunday, 2 April 2017 at 20:02:56 UTC, Basile B. wrote: On Sunday, 2 April 2017 at 19:24:14 UTC, Inquie wrote: is it possible to create a delegate that takes an optional number of parameters and/or return type? T delegate(S...)(S) special_delegate; I guess this is impossible? alias Dg(Ret

Re: delegate with optional parameters

2017-04-02 Thread Basile B. via Digitalmars-d-learn
On Sunday, 2 April 2017 at 20:48:09 UTC, Inquie wrote: On Sunday, 2 April 2017 at 20:02:56 UTC, Basile B. wrote: On Sunday, 2 April 2017 at 19:24:14 UTC, Inquie wrote: is it possible to create a delegate that takes an optional number of parameters and/or return type? T delegate(S...)(S) speci

Re: delegate with optional parameters

2017-04-02 Thread Inquie via Digitalmars-d-learn
On Sunday, 2 April 2017 at 21:47:55 UTC, Basile B. wrote: On Sunday, 2 April 2017 at 20:48:09 UTC, Inquie wrote: On Sunday, 2 April 2017 at 20:02:56 UTC, Basile B. wrote: On Sunday, 2 April 2017 at 19:24:14 UTC, Inquie wrote: is it possible to create a delegate that takes an optional number of

Re: delegate with optional parameters

2017-04-02 Thread Ali Çehreli via Digitalmars-d-learn
On 04/02/2017 03:24 PM, Inquie wrote: >> Show a usage, someone certainly propose a pattern that does the job. > > int delegate() f; > void delegate(int) f; That won't work because both of those are variables and variables don't have overloading. > These are effectively overload methods, but m

Re: delegate with optional parameters

2017-04-02 Thread Inquie via Digitalmars-d-learn
On Monday, 3 April 2017 at 03:08:22 UTC, Ali Çehreli wrote: On 04/02/2017 03:24 PM, Inquie wrote: >> Show a usage, someone certainly propose a pattern that does the job. > > int delegate() f; > void delegate(int) f; That won't work because both of those are variables and variables don't have o

Re: delegate with optional parameters

2017-04-03 Thread Rene Zwanenburg via Digitalmars-d-learn
On Monday, 3 April 2017 at 05:00:15 UTC, Inquie wrote: Yes, but they are really not any different. They only look different. A field can be a function just like a method because they look exactly the same except on is in a vtable and the other is in the fields memory. But both point functions.