Thread overview | |||||
---|---|---|---|---|---|
|
November 14, 2008 Function default parameters | ||||
---|---|---|---|---|
| ||||
I want to use the return value of another member function as the default for a parameter. I could just overload it but I would prefer to do it like: module test; class Thing { char[] getString(uint len = this.strLen()) { char[] a = new char[len]; return a; } uint strLen() { return 6; } } int main() { Thing t = new Thing; char[] abc = t.getString; return 0; } test.d(6): Error: 'this' is only defined in non-static member functions, not Thing |
November 14, 2008 Re: Function default parameters | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tim M | On Thu, Nov 13, 2008 at 8:34 PM, Tim M <a@b.com> wrote:
> I want to use the return value of another member function as the default for a parameter. I could just overload it but I would prefer to do it like:
You can't, sorry. You'll just have to overload it.
|
November 14, 2008 Re: Function default parameters | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tim M | Reply to tim,
> I want to use the return value of another member function as the
> default for a parameter. I could just overload it but I would prefer
> to do it like:
>
> module test;
>
> class Thing
> {
> char[] getString(uint len = this.strLen())
> {
> char[] a = new char[len];
> return a;
> }
> uint strLen()
> {
> return 6;
> }
> }
> int main()
> {
> Thing t = new Thing;
> char[] abc = t.getString;
> return 0;
> }
> test.d(6): Error: 'this' is only defined in non-static member
> functions, not Thing
>
Sorry. No sutch luck. The overload is what your stuck with.
|
Copyright © 1999-2021 by the D Language Foundation