Jump to page: 1 2
Thread overview
Class variables in optional parameters?
Jul 16, 2005
AJG
Jul 16, 2005
Derek Parnell
Jul 17, 2005
AJG
Jul 16, 2005
Ben Hinkle
Jul 17, 2005
AJG
Jul 17, 2005
Ben Hinkle
Jul 17, 2005
Andrew Fedoniouk
Jul 17, 2005
Regan Heath
Jul 17, 2005
Andrew Fedoniouk
Jul 17, 2005
Regan Heath
Jul 17, 2005
Andrew Fedoniouk
Jul 17, 2005
Regan Heath
Jul 17, 2005
Andrew Fedoniouk
Jul 17, 2005
Regan Heath
Jul 17, 2005
Andrew Fedoniouk
Jul 17, 2005
AJG
Jul 17, 2005
Deewiant
July 16, 2005
Hi,

I'm not sure if this is a bug or not, but it seems like the following behaviour should be allowed:

# class Foo {
#     bool Bar = true;
#     public      this(bool bar = Bar) {} // In constructors?
#     public void func(bool bar = Bar) {} // In regular funcs?
# }

Actually, neither of those works. I'm getting:
# need 'this' to access member Bar

If I change it to this.Bar, then I get:
# 'this' is only allowed in non-static member functions

As a matter of fact, neither the constructor nor the func is static. What's the deal? Is this a bug or per-design? I hope the former...

Thanks,
--AJG.


July 16, 2005
On Sat, 16 Jul 2005 23:07:21 +0000 (UTC), AJG wrote:

> Hi,
> 
> I'm not sure if this is a bug or not, but it seems like the following behaviour should be allowed:
> 
> # class Foo {
> #     bool Bar = true;
> #     public      this(bool bar = Bar) {} // In constructors?
> #     public void func(bool bar = Bar) {} // In regular funcs?
> # }
> 
> Actually, neither of those works. I'm getting:
> # need 'this' to access member Bar
> 
> If I change it to this.Bar, then I get:
> # 'this' is only allowed in non-static member functions
> 
> As a matter of fact, neither the constructor nor the func is static. What's the deal? Is this a bug or per-design? I hope the former...

This has come up before but I didn't notice a definitive reason why it works like it does.

It appears that if you supply a default value to a parameter, that default value must be known, or gettable, at compile time.

If you change it to "static bool Bar = true;" it compiles but that may not be what you want either.

-- 
Anonymous Bosch
--
The view will be the same no matter which path you take to the mountain top.
July 16, 2005
> # class Foo {
> #     bool Bar = true;
> #     public      this(bool bar = Bar) {} // In constructors?
> #     public void func(bool bar = Bar) {} // In regular funcs?
> # }

Note default values must be known at compile-time. So I assume you meant something like

 class Foo {
     bool Bar = true;
     public      this(bool bar = Bar.init) {} // In constructors?
     public void func(bool bar = Bar.init) {} // In regular funcs?
 }


July 17, 2005
Hi,

>This has come up before but I didn't notice a definitive reason why it works like it does.

Hm... interesting.

>It appears that if you supply a default value to a parameter, that default value must be known, or gettable, at compile time.

This is unfortunate. Is this at least considered a technical limitation rather than a feature? I mean, semantically, I don't see why it shouldn't be allowed.

>If you change it to "static bool Bar = true;" it compiles but that may not be what you want either.

Indeed, it would be something different. I'll post an example in my reply to Ben if you are interested.

Thanks,
--AJG.


July 17, 2005
Hi,

>Note default values must be known at compile-time. So I assume you meant something like
>
> class Foo {
>     bool Bar = true;
>     public      this(bool bar = Bar.init) {} // In constructors?
>     public void func(bool bar = Bar.init) {} // In regular funcs?
> }

Well, yes and no. For the constructor, yes. For the regular function, no. In my class, Bar is a setting. So, I want to specify the _default_ value for this setting in the initilizer. Ideally, I also want to specify this default only once.

# bool Bar = true; // This is what it should be without user intervention.

Then, in the constructor, I want to offer the possibility of overriding the default. In _this_ case, .init would work, because Bar couldn't have been altered.

# public this(bool bar = Bar.init) {} // This is fine.

However, I have another function where it makes sense to also allow for this default to be overridden. Now .init wouldn't work, because the user could have set Bar to a non-default via the constructor. So then:

# public void func(bool bar = Bar.init) {} // This is wrong.

Because it forces the user to either re-specify the setting or be stuck with the default. If this is the case, then it conflicts with this setting in the constructor. Ideally, he can ommit the parameter so it means "use what I already specified."

Is there a way to fix this?

Thanks,
--AJG.


July 17, 2005
"AJG" <AJG_member@pathlink.com> wrote in message news:dbc88m$2es1$1@digitaldaemon.com...
> Hi,
>
>>Note default values must be known at compile-time. So I assume you meant something like
>>
>> class Foo {
>>     bool Bar = true;
>>     public      this(bool bar = Bar.init) {} // In constructors?
>>     public void func(bool bar = Bar.init) {} // In regular funcs?
>> }
>
> Well, yes and no. For the constructor, yes. For the regular function, no.
> In my
> class, Bar is a setting. So, I want to specify the _default_ value for
> this
> setting in the initilizer. Ideally, I also want to specify this default
> only
> once.

Without having more context I'd say you should use overloading
 class Foo {
     bool defaultbar = true;
     public this(bool bar = defaultbar.init) {defaultbar = bar;}
     public void func() {func(defaultbar);}
     public void func(bool bar) {...}
 }


July 17, 2005
Consider this:

class Foo {
    bool Bar = true;
    public void func(Foo f, bool bar = Bar)
    {
         f.func( f );  // what is supposed to be used here:
         // this.bar or f.bar ?
    }
}



July 17, 2005
On Sat, 16 Jul 2005 19:25:14 -0700, Andrew Fedoniouk <news@terrainformatica.com> wrote:
> Consider this:
>
> class Foo {
>     bool Bar = true;
>     public void func(Foo f, bool bar = Bar)
>     {
>          f.func( f );  // what is supposed to be used here:
>          // this.bar or f.bar ?
>     }
> }

So why not:

public void func(Foo f, bool bar = this.Bar)

or

public void func(Foo f, bool bar = f.Bar)

?

Regan
July 17, 2005
"Regan Heath" <regan@netwin.co.nz> wrote in message news:opst1c65bk23k2f5@nrage.netwin.co.nz...
> On Sat, 16 Jul 2005 19:25:14 -0700, Andrew Fedoniouk <news@terrainformatica.com> wrote:
>> Consider this:
>>
>> class Foo {
>>     bool Bar = true;
>>     public void func(Foo f, bool bar = Bar)
>>     {
>>          f.func( f );  // what is supposed to be used here:
>>          // this.bar or f.bar ?
>>     }
>> }
>
> So why not:
>
> public void func(Foo f, bool bar = this.Bar)
>

Parameters are in separate namespace from function body
- no 'this' there.

> or
>
> public void func(Foo f, bool bar = f.Bar)
>

Better, but too complicated for compilation and
codegeneration.
Is not worth doing as it always could be replaced by

public void func(Foo f, bool bar);
....
func(f, f.Bar);



July 17, 2005
On Sat, 16 Jul 2005 20:08:14 -0700, Andrew Fedoniouk <news@terrainformatica.com> wrote:
> "Regan Heath" <regan@netwin.co.nz> wrote in message
> news:opst1c65bk23k2f5@nrage.netwin.co.nz...
>> On Sat, 16 Jul 2005 19:25:14 -0700, Andrew Fedoniouk
>> <news@terrainformatica.com> wrote:
>>> Consider this:
>>>
>>> class Foo {
>>>     bool Bar = true;
>>>     public void func(Foo f, bool bar = Bar)
>>>     {
>>>          f.func( f );  // what is supposed to be used here:
>>>          // this.bar or f.bar ?
>>>     }
>>> }
>>
>> So why not:
>>
>> public void func(Foo f, bool bar = this.Bar)
>>
>
> Parameters are in separate namespace from function body
> - no 'this' there.

So? Change it.

>> or
>>
>> public void func(Foo f, bool bar = f.Bar)
>>
>
> Better, but too complicated for compilation and
> codegeneration.

Who are you to make this assertion?

> Is not worth doing as it always could be replaced by
>
> public void func(Foo f, bool bar);
> ....
> func(f, f.Bar);

IYNSHO.

Regan
« First   ‹ Prev
1 2