Thread overview
const(type) vs. const type
Jul 20, 2010
Mike Linford
Jul 20, 2010
Jonathan M Davis
Jul 20, 2010
div0
Jul 20, 2010
bearophile
Jul 20, 2010
Jonathan M Davis
Jul 20, 2010
Trass3r
Jul 21, 2010
torhu
Jul 21, 2010
Mike Linford
July 20, 2010
I'm playing with QtD, and I tried to override a QWidget's sizeHint() function, which is declared as const QSize sizeHint(). I tried to override it by declaring my function as override const(QSize) sizeHint () . I got a compiler error that it was "not covariant" with const QSize, and when I changed it to that it worked fine. I've skimmed through the documentation but don't understand what the difference is. Any help?



-- 
Mike Linford
July 20, 2010
On Tuesday, July 20, 2010 15:57:41 Mike Linford wrote:
> I'm playing with QtD, and I tried to override a QWidget's sizeHint() function, which is declared as const QSize sizeHint(). I tried to override it by declaring my function as override const(QSize) sizeHint () . I got a compiler error that it was "not covariant" with const QSize, and when I changed it to that it worked fine. I've skimmed through the documentation but don't understand what the difference is. Any help?

IIRC,

const QSize sizeHint()


is synonymous with

QSize sizeHint() const


which means that the function is const. However,

const(QSize)  sizeHint()


means that the QSize returned is const rather than the function.

- Jonathan M Davis
July 20, 2010
On 20/07/2010 23:57, Mike Linford wrote:
> I'm playing with QtD, and I tried to override a QWidget's sizeHint()
> function, which is declared as const QSize sizeHint(). I tried to
> override it by declaring my function as override const(QSize) sizeHint
> () . I got a compiler error that it was "not covariant" with const QSize,
> and when I changed it to that it worked fine. I've skimmed through the
> documentation but don't understand what the difference is. Any help?
>
>
>

const QSize sizeHint() means the object on which you are calling the function has to be const and the returned QSize is mutable

(I think, it might also be const as well, I don't use that format of method signature...),

whilst const(QSize) sizeHint() means a mutable object which returns a const object.

Const is allowed before & after the method signature which is confusing and annoying.

In c++ it would be: (assuming QSize is a D class)

QSize& sizeHint() const { return _sh; }

vs

const QSize& sizeHint() { return _sh; }

-- 
My enormous talent is exceeded only by my outrageous laziness.
http://www.ssTk.co.uk
July 20, 2010
> I'm playing with QtD, and I tried to override a QWidget's sizeHint()
> function, which is declared as const QSize sizeHint(). I tried to
> override it by declaring my function as override const(QSize) sizeHint
> () . I got a compiler error that it was "not covariant" with const QSize,

http://digitalmars.com/d/2.0/const3.html
In that case it is a const member function which means it is not allowed to change any part of the object through the member function's this reference.
July 20, 2010
div0:
> Const is allowed before & after the method signature which is confusing and annoying.

We have asked Walter to improve this situation, and he has refused, saying that keeping all D "attributes" syntax uniform is better.
See also:
http://d.puremagic.com/issues/show_bug.cgi?id=4040

Bye,
bearophile
July 20, 2010
On Tuesday, July 20, 2010 16:23:23 div0 wrote:
> Const is allowed before & after the method signature which is confusing and annoying.

TDPL had an explanation for that that IIRC made fair sense, but I'd have to look it up again since I don't remember exactly what it was. Certainly, at first glance, it's a poor design decision. I'll obviously have to look it up again to see why it wasn't as bad a decision as it seems at first glance.

- Jonathan M Davis
July 21, 2010
On 21.07.2010 00:57, Mike Linford wrote:
> I'm playing with QtD, and I tried to override a QWidget's sizeHint()
> function, which is declared as const QSize sizeHint(). I tried to
> override it by declaring my function as override const(QSize) sizeHint
> () . I got a compiler error that it was "not covariant" with const QSize,
> and when I changed it to that it worked fine. I've skimmed through the
> documentation but don't understand what the difference is. Any help?

In the first case, it's the function itself that ends up being const, not its return value.  It's like putting 'const' after the parameter list in C++.    In the second case, only the return value is const.
July 21, 2010
Thanks for the responses guys, I appreciate it.

-- 
Mike Linford