View mode: basic / threaded / horizontal-split · Log in · Help
September 11, 2007
Re: Const sucks
On Mon, 10 Sep 2007 15:22:23 -0700, Walter Bright wrote:

> Derek Parnell wrote:
>> Remind me again what the difference between 'const' and 'invariant'
>> is.
> 
> 'const' means a I can't change it (but someone else can, such as someone
> in a different thread). 'invariant' means nuttin' can change it, not
> no-how, not no-way.

Thank you. Because the terms in English are synonyms, its a bit hard,
without constant usage, to remember this stuff. Of course, once it is
permanently in D and I'm using it all the time, no doubt it'll stick ...
pity about the newbies though.

>> What do these below mean ...
>> 
>> const int (* p)
> 
> syntax error
> 
>> const int *(p)
> 
> syntax error
> 
>> const int (*) p
> 
> syntax error
> 
> Imagine if const were a template called Const, and we used that template
> to form a new type that was const of the old type. We'd invoke it like:
> 	Const!(T) t;
> Now, just s/Const!/const/, and the syntax should make sense.

Thanks. I thought as much.

>>> o  tail const of a struct would have to be done by making the
>>> struct a template:
>>> 
>>> struct S(T) { T member; } S!(int)   // tail mutable S!(const(int))
>>> // tail const
>> 
>> But most structs contain multiple members, and usually of different
>> types.
> 
> That's up to the struct designer.

What does that answer mean?!?!?!

Ok, let's back up a second. You say "struct S(T) { T member; } S!(int)   //
tail mutable S!(const(int)) >>> // tail const" to which I assumed you meant
that you can ONLY have tail const structs if every member of the struct is
of a type mentioned in the template argument.

In other words, if I have a struct with three members, each of a different
type, I need to code ...

struct S3(T, U, V)
{
  T member1;
  U member2;
  V member3;
}

S3!(const(int), const(float), const(bool));

and so on for 4, 5, 6, .... 23 member structs.

I'm sure I'm misunderstanding you, because this is really silly.

-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
11/09/2007 10:52:30 AM
September 11, 2007
Re: Const sucks
Walter Bright wrote:
> Bruno Medeiros wrote:
>> As Regan asked before, and how would that work for classes? This is a 
>> most fundamental issue that has not been mentioned. Without a way to 
>> declare tail const/invariant, this design, no matter how simpler or 
>> more understandable, is fundamentally *broken*.
> 
> What, exactly, is the use case that needs a solution?

Before hand, let me just say that by broken, I didn't mean subvertable, 
but rather that using const/invariant with classes would be impractical 
and unmanageable.

Simple example: Let's say you have a class Foo that is comparable. How 
do you sort an array of const(Foo)'s ?

  const(Foo)[] sort(const(Foo)[] arr) {
     ... ?
The sorting algorithm doesn't matter, what matters is: how would one 
swap two elements in the array?


-- 
Bruno Medeiros - MSc in CS/E student
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D
September 11, 2007
Re: Const sucks
Derek Parnell wrote:
>  
>> o  const and invariant now mean "fully const" and "fully invariant", 
>> where fully means "head and tail combined":
> 
> Remind me again what the difference between 'const' and 'invariant' is.
> 

'invariant' = constant
'const' = read-only

It's simple I think.

-- 
Bruno Medeiros - MSc in CS/E student
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D
September 11, 2007
Re: Const sucks
Miles wrote:
> Sometimes people give use-cases to exemplify (STL's need for a
> difference between iterator<> and const_iterator<> is the first thing
> that comes to my mind), but then you ask what a language could provide
> to fix this, they don't know.
>
> This is where you should aim first you laser.
> 

Wait, what?
I presume this is a typo, but I can't for the life of me figure out what
it's supposed to be.
No offense :)
--downs
September 11, 2007
Re: Const sucks
Walter Bright wrote:
> Bruno Medeiros wrote:
>> As Regan asked before, and how would that work for classes? This is a 
>> most fundamental issue that has not been mentioned. Without a way to 
>> declare tail const/invariant, this design, no matter how simpler or 
>> more understandable, is fundamentally *broken*.
> 
> What, exactly, is the use case that needs a solution?

Here's another use case, which is significantly different than the other 
(there should be lots more to be found). Say you have a Person class, 
which has a name, and for whatever reasons you want the name be a String 
class instead of const(char)[]. Then you have:

  class Person {
    const(String) name;
    ...

but now how do you change the name of the Person?...



-- 
Bruno Medeiros - MSc in CS/E student
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D
September 11, 2007
Re: Const sucks
Downs wrote:
> I presume this is a typo, but I can't for the life of me figure out what
> it's supposed to be.

Yes, it was... :-P read: This is where you should first aim your laser.

I tried to make a joke... you usually kill bugs with insecticide or
slippers (or a little programming), but D's const was like using a huge
laser beam to exterminate a small misfeature of C++... Since you are
using a laser to kill bugs (i.e. a major redesign), aim it first on
those bugs that are really annoying us way back from C++.
September 11, 2007
Re: Const sucks
"Walter Bright" <newshound1@digitalmars.com> wrote in message 
news:fc4mu5$2j7u$3@digitalmars.com...
> Jb wrote:
>> Am I the only one who though it odd that the current documentation spends 
>> twice as much time talking about C++ than it does about D?
>
> Half of D programmers come from C++, and the easiest way to learn 
> something new is to relate it to something already known.

Yeah thats fair enough up to a point, but imo the D section was sorely 
lacking in examples and detailed explanations. Hence all the "What does 
const xyz mean?" questions in the newgroup.
September 11, 2007
Re: Const sucks
"Jb" <jb@nowhere.com> wrote in message news:fc4stj$2snr$1@digitalmars.com...
>
> "Walter Bright" <newshound1@digitalmars.com> wrote in message 
> news:fc4mu5$2j7u$3@digitalmars.com...
>> Jb wrote:
>>> Am I the only one who though it odd that the current documentation 
>>> spends twice as much time talking about C++ than it does about D?
>>
>> Half of D programmers come from C++, and the easiest way to learn 
>> something new is to relate it to something already known.
>
> Yeah thats fair enough up to a point, but imo the D section was sorely 
> lacking in examples and detailed explanations. Hence all the "What does 
> const xyz mean?" questions in the newgroup.

Sorry to clarify, i meant the the D section was too light, not the C++ 
section too heavy.
September 11, 2007
Re: Const sucks
Walter Bright wrote:
> o  tail const of a struct would have to be done by making the struct a 
> template:
> 
>   struct S(T) { T member; }
>   S!(int)   // tail mutable
>   S!(const(int)) // tail const
> 

Walter, how are references handled and can you create a tail const of a 
reference?

Which of the following are correct syntax and how is the "constness" 
defined:

class SomeClass {
  some constructors
  some variables
}

const SomeClass x = new SomeClass ();
SomeClass const(x) = new SomeClass ();
SomeClass x = const new SomeClass ();
SomeClass x = new const SomeClass ();

(anything I missed out?)

How can I create a tail const such that the reference is immutable but 
the public instance variables and public methods are mutable?

What about arrays?

Regards,

Myron.
September 11, 2007
Re: Const sucks
Bruno Medeiros wrote:
> Walter Bright wrote:
>> What, exactly, is the use case that needs a solution?
> 
> Before hand, let me just say that by broken, I didn't mean subvertable, 
> but rather that using const/invariant with classes would be impractical 
> and unmanageable.
> 
> Simple example: Let's say you have a class Foo that is comparable. How 
> do you sort an array of const(Foo)'s ?
> 
>   const(Foo)[] sort(const(Foo)[] arr) {
>      ... ?
> The sorting algorithm doesn't matter, what matters is: how would one 
> swap two elements in the array?

You can't sort an (array of)(const T). You can sort an (array 
of)(something of)(const T).
2 3 4 5 6 7 8 9 10
Top | Discussion index | About this forum | D home