94.23.238.222: Comment provided by Ssp - via ArticleComments extension - 2013-10-21 16:47:34

Comment provided by Ssp - via ArticleComments extension

←Older revision Revision as of 16:47, 21 October 2013
Line 255: Line 255:
--[[User:JaroslavTulach|JaroslavTulach]] 15:09, 5 December 2012 (UTC)
--[[User:JaroslavTulach|JaroslavTulach]] 15:09, 5 December 2012 (UTC)
 +
</div>
 +
== Ssp said ... ==
 +
 +
<div class='commentBlock'>
 +
Hi, The Community BBQ will take place on Torridge Way between 4.00 6.00pm on Saturday 5th November. We won't be hanivg fireworks, but have timed the BBQ so that people can go on down to the Hoe later or watch that display from various excellent viewing points around Efford. I'll send you an email with the flier about this event and add you to our regular circulation about events. Thanks for your interest and hope you will join us on Saturday, do feel free to bring others along. Jacqi, Community Coordinator
 +
 +
--Ssp 18:47, 21 October 2013 (CEST)
</div>
</div>

JaroslavTulach at 15:09, 5 December 2012 - 2012-12-05 15:09:36

←Older revision Revision as of 15:09, 5 December 2012
Line 250: Line 250:
--bob 07:20, 5 December 2012 (CET)
--bob 07:20, 5 December 2012 (CET)
 +
 +
Thanks. Fixed in [http://source.apidesign.org/hg/apidesign/rev/87bcc647df63 87bcc647df63].
 +
 +
--[[User:JaroslavTulach|JaroslavTulach]] 15:09, 5 December 2012 (UTC)
 +
</div>
</div>

76.126.212.226: Comment provided by bob - via ArticleComments extension - 2012-12-05 06:20:44

Comment provided by bob - via ArticleComments extension

←Older revision Revision as of 06:20, 5 December 2012
Line 237: Line 237:
--jtulach 10:49, 18 September 2012 (CEST)
--jtulach 10:49, 18 September 2012 (CEST)
 +
</div>
 +
== bob said ... ==
 +
 +
<div class='commentBlock'>
 +
trait Listable[T <: Listable[T]]
 +
 +
should be changed to
 +
 +
trait Listable[T]
 +
 +
The additional bound does not provide any benefit
 +
 +
--bob 07:20, 5 December 2012 (CET)
</div>
</div>

213.180.34.38 at 08:51, 18 September 2012 - 2012-09-18 08:51:04

←Older revision Revision as of 08:51, 18 September 2012
Line 219: Line 219:
With a small modification you can keep next and prev private but using ll_item template is better approach.
With a small modification you can keep next and prev private but using ll_item template is better approach.
 +
<source lang="cpp">
class person_item {
class person_item {
private:
private:
Line 232: Line 233:
person_item (int age, const char* name) { this->age=age; this->name=name; }
person_item (int age, const char* name) { this->age=age; this->name=name; }
};
};
-
 
+
</source>
--jtulach 10:49, 18 September 2012 (CEST)
--jtulach 10:49, 18 September 2012 (CEST)
</div>
</div>

213.180.34.38: Comment provided by jtulach - via ArticleComments extension - 2012-09-18 08:49:47

Comment provided by jtulach - via ArticleComments extension

←Older revision Revision as of 08:49, 18 September 2012
Line 214: Line 214:
--[[User:JaroslavTulach|JaroslavTulach]] 08:34, 18 September 2012 (UTC)
--[[User:JaroslavTulach|JaroslavTulach]] 08:34, 18 September 2012 (UTC)
 +
== jtulach said ... ==
 +
 +
<div class='commentBlock'>
 +
With a small modification you can keep next and prev private but using ll_item template is better approach.
 +
 +
class person_item {
 +
private:
 +
friend llist <person_item>;
 +
person_item* next;
 +
person_item* prev;
 +
 +
protected:
 +
int age;
 +
const char* name;
 +
 +
public:
 +
person_item (int age, const char* name) { this->age=age; this->name=name; }
 +
};
 +
 +
 +
 +
--jtulach 10:49, 18 September 2012 (CEST)
 +
</div>

JaroslavTulach at 08:34, 18 September 2012 - 2012-09-18 08:34:43

←Older revision Revision as of 08:34, 18 September 2012
Line 203: Line 203:
}
}
</source>
</source>
-
 
--jtulach 14:04, 17 September 2012 (CEST)
--jtulach 14:04, 17 September 2012 (CEST)
</div>
</div>
 +
 +
Right. This is it.
 +
 +
I didn't know about the option used when defining ''person_item'' - e.g. to just expose the ''next'' and ''prev'' publicly. Not that I'd prefer that solution (as it breaks the encapsulation), but shows the power of [[C++]] ''always recompiled'' templates. [[Java]]'s generics are compiled only once and need to use the ''class animal_item : public ll_item <animal_item>'' approach.
 +
 +
Thanks. I'll publish the code somehow, sometimes on the [[blogs]].
 +
 +
--[[User:JaroslavTulach|JaroslavTulach]] 08:34, 18 September 2012 (UTC)

213.180.34.38 at 15:09, 17 September 2012 - 2012-09-17 15:09:47

←Older revision Revision as of 15:09, 17 September 2012
Line 114: Line 114:
<div class='commentBlock'>
<div class='commentBlock'>
 +
 +
<source lang="cpp">
template<class ITEM>
template<class ITEM>
class llist {
class llist {
Line 200: Line 202:
animals.add(z);
animals.add(z);
}
}
-
 
+
</source>
--jtulach 14:04, 17 September 2012 (CEST)
--jtulach 14:04, 17 September 2012 (CEST)
</div>
</div>

213.180.34.38: Comment provided by jtulach - via ArticleComments extension - 2012-09-17 12:04:37

Comment provided by jtulach - via ArticleComments extension

←Older revision Revision as of 12:04, 17 September 2012
Line 111: Line 111:
--[[User:JaroslavTulach|JaroslavTulach]] 07:42, 17 September 2012 (UTC)
--[[User:JaroslavTulach|JaroslavTulach]] 07:42, 17 September 2012 (UTC)
 +
== jtulach said ... ==
 +
 +
<div class='commentBlock'>
 +
template<class ITEM>
 +
class llist {
 +
protected:
 +
ITEM *first;
 +
public:
 +
void add(ITEM& p)
 +
{
 +
p.prev=NULL;
 +
p.next=first;
 +
first=&p;
 +
}
 +
void remove(ITEM& p)
 +
{
 +
ITEM *pr=p.prev;
 +
ITEM *nx=p.next;
 +
if(pr) pr->next=nx;
 +
else first=nx;
 +
if(nx) nx->prev=pr;
 +
}
 +
 +
ITEM *get(int idx)
 +
{
 +
ITEM *i=first;
 +
while((i)&&(idx-->0))
 +
i=i->next;
 +
 +
return i;
 +
}
 +
 +
llist(void) { first=NULL; };
 +
~llist() { };
 +
};
 +
 +
// not inherited, requires to define next and prev properties
 +
class person_item {
 +
protected:
 +
int age;
 +
const char* name;
 +
 +
public:
 +
person_item* next;
 +
person_item* prev;
 +
 +
person_item (int age, const char* name) { this->age=age; this->name=name; }
 +
};
 +
 +
 +
template<class ITEM>
 +
class ll_item {
 +
friend llist <ITEM>;
 +
private:
 +
ITEM* next;
 +
ITEM* prev;
 +
};
 +
 +
// inherited from ll_item template which defines next and prev for me
 +
class animal_item : public ll_item <animal_item>{
 +
protected:
 +
const char* name;
 +
 +
public:
 +
 +
animal_item(const char* name) : ll_item <animal_item>() { this->name=name; }
 +
};
 +
 +
int main(int argc, char* argv[])
 +
{
 +
person_item a(10, "Ben");
 +
person_item b(20, "Nora");
 +
person_item c(30, "John");
 +
 +
animal_item x("Fifi");
 +
animal_item y("Bobika");
 +
animal_item z("Bill");
 +
 +
llist <person_item> persons;
 +
persons.add(b);
 +
persons.add(c);
 +
persons.add(a);
 +
// persons.add(x); - compilation fails
 +
 +
llist <animal_item> animals;
 +
animals.add(x);
 +
animals.add(y);
 +
animals.add(z);
 +
}
 +
 +
 +
 +
--jtulach 14:04, 17 September 2012 (CEST)
 +
</div>

JaroslavTulach at 07:42, 17 September 2012 - 2012-09-17 07:42:47

←Older revision Revision as of 07:42, 17 September 2012
Line 93: Line 93:
I don't know why do you think this is not type safe? You cannot add anything which is not inherited from ll_item. If you have enabled RTTI, you can dynamic cast items from list.
I don't know why do you think this is not type safe? You cannot add anything which is not inherited from ll_item. If you have enabled RTTI, you can dynamic cast items from list.
 +
<source lang="cpp">
class persons_list : public llist {
class persons_list : public llist {
public:
public:
Line 100: Line 101:
}
}
};
};
-
 
+
</source>
--jtulach 10:58, 13 September 2012 (CEST)
--jtulach 10:58, 13 September 2012 (CEST)
</div>
</div>
 +
 +
The question ''Why this is not type-safe?'' may reveal the progress [[OOP]] made during last twenty years. In the middle of nineties [[Java]] was claimed a safe language as it could not cause ''segmentation faults''. The [[C++]]'s '''dynamic_cast''' is a safety of similar kind. It is a runtime check. It is not bad thing, but as I am coding in [[Java]] most of the time, I was not calling for something as basic.
 +
 +
When I mentioned type safety, I meant compile type safety. The above dynamic cast does not guarantee that I cannot add ''animal_item'' into the ''llist'' and try to obtain ''person_item''. To achieve such kind of safety one needs a form of ''algebraic types'' (nicely illustrated in the [[OOP#Revolutionary_view|revolutionary essay]]). Both [[Scala]] and [[Java]] (since version 1.5) provide a way to marry ''algebraic types'' with [[OOP]] inheritance and thus provide compile time safety (as code snippets for the [[Trait|direct linked list]] show).
 +
 +
--[[User:JaroslavTulach|JaroslavTulach]] 07:42, 17 September 2012 (UTC)

213.180.34.38: Comment provided by jtulach - via ArticleComments extension - 2012-09-13 08:58:49

Comment provided by jtulach - via ArticleComments extension

←Older revision Revision as of 08:58, 13 September 2012
Line 88: Line 88:
--[[User:JaroslavTulach|JaroslavTulach]] 08:26, 12 September 2012 (UTC)
--[[User:JaroslavTulach|JaroslavTulach]] 08:26, 12 September 2012 (UTC)
 +
== jtulach said ... ==
 +
 +
<div class='commentBlock'>
 +
I don't know why do you think this is not type safe? You cannot add anything which is not inherited from ll_item. If you have enabled RTTI, you can dynamic cast items from list.
 +
 +
class persons_list : public llist {
 +
public:
 +
person_item *get(int idx)
 +
{
 +
return dynamic_cast<person_item *>(llist::get(idx));
 +
}
 +
};
 +
 +
 +
--jtulach 10:58, 13 September 2012 (CEST)
 +
</div>