« first day (239 days earlier)      last day (4707 days later) » 

11:00 PM
well
what is the contract for the method?
 
@FredOverflow but how do you know if it is?
 
when I look up the documentation for poll, what will it say that it does?
 
@wilhelmtell You design the class.
 
@FredOverflow the base class can't make much assumptions about derviatives
 
@wilhelmtell Of course it can. It also makes the assumption that poll takes no arguments and returns a bool.
 
11:01 PM
@wilhelmtell: On the contrary, the base class makes all the assumptions about the derived class
 
What exactly is poll supposed to do?
 
Xeo
You know, virtual functions aren't called the "interface" for nothing. You decide what the derived classes are allowed to do.
 
the base class says what goes and what doesn't
 
@DeadMG the contract is that it returns true or false for a given object.
as simple as that.
 
Xeo
@wilhelmtell Perfect, make it const.
 
11:01 PM
right
 
Xeo
If a derived class has a cache or sth, they should make it mutable
 
so it's a reading operation
 
@wilhelmtell I can see that from the signature. But what does the documentation say about the meaning of the function? What should it do?
 
and should be const
 
but if some base class depend on a state that can change during the evaluation then it can't be const
@FredOverflow it's a base class for an interpreter hierarchy
 
11:02 PM
If I say x.poll(); x.poll(); x.poll(); x.poll(); x.poll(); do you expect the same result each time? Or could it change?
 
Xeo
Know what? Why not simply overload it for const and non-const?
 
each derivative needs to decide at call to poll() if it's time for a refill. refill being some operation...
 
Or even better: Should it be legal to call poll on a const object?
 
@wilhelmtell: You're being much, much too generic here
poll what?
 
@wilhelmtell Sounds like it should not be const then.
 
Xeo
11:04 PM
@wilhelmtell Just deciding something implies logical constness for me
 
a base class serves a purpose, if you know what the purpose is, then you should know whether the functions should be const
 
right
 
if you can't decide, then maybe your base class serves too vague of a purpose
 
ok, base class is refill_basis. one derivative is day. another is week. another is month.
 
Xeo
And if you have it const, a derived class can always cheat you with mutable.
 
11:05 PM
and what does poll do in that context?
 
let me rephrase the signature
 
Please answer this question:
2 mins ago, by FredOverflow
If I say x.poll(); x.poll(); x.poll(); x.poll(); x.poll(); do you expect the same result each time? Or could it change?
 
virtual bool poll(const boost::gregorian::date& start) with possible const
@FredOverflow depending on the derivative. that's my problem. i don't know if i should make such a restriction on derivatives.
it's the choice between two features. one freeddom for derivatives the other freedom for callers.
 
then you don't know the contract for poll
 
Xeo
@wilhelmtell That reminds me of another reason why i like const on the right side of a type - it doesn't make my eyes ping-pong between the const and the & reference to see if it is indeed const for long typenames (including namespaces).
 
11:07 PM
Can you show us an example program that calls poll? I still don't know what it is supposed to do.
 
I mean, logically, what action does poll actually perform?
 
exactly
 
Xeo
okay @wilhelmtell - does poll decide something or does it perform an active action and returns success / failure?
@wilhelmtell The derivates still have all the freedom they want: mutable, again.
 
I want to see the documentation that explains to a client what poll does -- why he would want to call poll.
 
@Xeo i can't rely on that. you don't use mutable to go around const, that's not what it's for.
 
Xeo
11:11 PM
@wilhelmtell Uhm, it is.
That's pretty much the only use.
 
i don't if you guys got my last three messages .. .:-S
about week
 
Xeo
Uhm, nope
 
@wilhelmtell: No, it's you that didn't respond to us
 
Xeo
I atleast not
 
@DeadMG ok, poll() should say yes if it's time to refill. there's a start date when refill originally started. that's what i pass. then each object says "ok refill now". logically many would work in const fine. for example, day refills every day.
week might take today from boost::gregorian::day_clock::universal_day() and say yes if today is the same day as the start day.
it need not change the object.
but then, maybe i'd later want to create an object that changes. like, refill every third poll to refill.
 
11:11 PM
@Xeo By that logic, you can mark every functions as const and every member as mutable. That would be pretty pointless.
 
Xeo
> poll() should say yes if it's time to refill.
Sound like a const function to me
 
@wilhelmtell: Then make it const
if your object wants to refill every third poll or something, then that is what mutable is for
 
Xeo
@FredOverflow Well, sure it's pointless. But you can do that.
You just show all assumptions about logical constness a user can make the middle finger
 
You should mark member functions as const if they leave the logical state of an object intact. That implies that it does not matter how often you call a const member function, it always returns the same result and, more importantly, it does not influence the state of the observable world.
 
@FredOverflow that's not entirely true. most classes will probably depend on the clock. so they won't return the same result every time.
 
11:16 PM
Right, implicit dependencies. I have been spoiled by Monads :)
 
Xeo
@wilhelmtell Extend that to "That implies that it does not matter how often you call a const member function at the same time"
 
lol
in a single universe? i think you can only do that once.
 
what I would more think about is in terms of the refill operation that you mentioned
 
Does poll call refill itself?
 
11:18 PM
no
these classes are going to be simple. hopefully. they're predicates really.
 
well
I could thoroughly argue that if you refill every three polls, then it's not actually fulfilling the original contract of refilling based on the clock
 
that's not on the contract
it was an example
 
then you're getting confused
 
the contract is to answer the question "should i refill"
 
do I refill arbitrarily? Or do I refill based on the clock?
 
11:20 PM
void foo(const Base& x)
{
    if (x.poll())   // <-- SHOULD THIS COMPILE?
    {
        // ...
    }
}
 
Xeo
@wilhelmtell Yes, and how does that fit with "refill every three polls" (i.e., internally call refill)?
 
if I refill arbitrarily, then why does poll contain an argument?
 
i don't tell the derivatives how to decide. just decide.
you're right.
i should pass it to the consturctor.
@Xeo i never said that. it's just a yes-no question, no operation involved.
 
Xeo
@wilhelmtell Cool, yes-no questions are const.
If you have a counter in the derived class for that "refill every three polls", make it mutable.
 
@FredOverflow that's a nice feature I'd love, hence my dilemma.
but i think i'm going to go const. fuck it's c++ lemme enjoy myself.
 
11:22 PM
the derived class can use mutable for that kind of thing
 
Xeo
40
Q: C++ 'mutable' keyword

RobA while ago I came across some code that marked a member variable of a class with the 'mutable' keyword. As far as I can see it simply allows you to modify a variable in a 'const' method: class Foo { private: mutable bool done_; public: void doSomething() const { ...; done_ = ...

Relevant and stuff.
 
on a related note: base classes are hard.
 
Have you ever written an interface in Java or C#? :)
 
Of course I have. But they're not nearly as const-solid as C++. So they don't make writing base classes hard, they instead encourage you to write bad code and be happy with it.
 
Because there is no such thing as a const member function in Java or C#.
 
11:27 PM
I disapprove of const anyway
 
mm?
(note to self: he might be trolling. hold on)
 
no, I'm not trolling
const has useful cases, like non-mutable access to the members of a std::set
 
lol :)
 
but in my opinion, variables exist to be written to
and I have little use for non-variable variables
the only reason I've ever had to use const is because a bunch of STL functions want const member functions
 
@DeadMG yes yes, of course, but life is infinitely easier when you know that a given state will never ever ever ever change.
 
11:29 PM
Try F# for some time and come back ;)
 
things become so simple then.
so it's a tool. sometimes it's very useful.
 
yeah, but I find that overall, the usefulness is not proportional
like, my example with`std::set`- it's great that const exists for this use
 
I find marking member functions as const the most useful aspect of const correctness, even if you don't use a single const object or const reference.
 
but in order to actually use const this way, then everything has to be const-correct, a distinctly non-trivial undertaking
 
Right, not everybody knows how to use const. That's why const_cast exists.
 
11:31 PM
@DeadMG the compiler does all the work for you, apart from you typing "const". you just program as usual, and if something's wrong the compiler will jump. i think that's as trivial as it can get.
i think that on the contrary, not using const-correctness is a none-trivial way to program.
 
sure, but you have to type const every-bloody-where
 
so, in a way, programming correctly in Java (God forbid) and C# is much much harder than programming in C++.
 
although, gotta admit, now that rvalue references exist and perfect forwarding reduce this problem significantly
 
@wilhelmtell Because the signatures don't tell you whether the functions modify the object? I agree.
 
The price I pay for thinking about const is a price that I pay even when programming without const
 
11:37 PM
@LucDanton Right. In a language that does not support const, you have to write "this function does not modify the object" as documentation, which takes a lot more key presses.
And it won't be checked by the compiler.
 
Every body with the slghtest experience in the industry knows that documentation doesn't work, and that the compiler does.
 
Well, documentation tends to get outdated and is generally full of silly little errors. But apart from that... :-)
Speaking of missing compiler support and documentation: I absolutely hate all those variations on "this parameter cannot be null" documentation in Java.
It just clutters up the code.
 

« first day (239 days earlier)      last day (4707 days later) »