# New Ticket Created by Mark Glines # Please include the string: [perl #53066] # in the subject line of all future correspondence about this issue. # <URL: http://rt.perl.org/rt3/Ticket/Display.html?id=53066 >
Hi, I've run into a design issue that I'm hoping for some discussion on. Every time you declare a function parameter as "const", that means you need to declare it for any other functions you pass it to. Sure, you can remove the constness with a cast, but isn't that cheating? (If it's going to be modified, you shouldn't be declaring it as const.) So every time I see a const PMC *pmc casted with (PMC*), it makes me think there's either too much const (in the parent function), or too little (and we should add it to the child). So one place where I think we could use some additional consting is in PMC methods. Ticket #52874 contained (among other things) a cast to remove the constness from a PMC* pointer passed to VTABLE_isa(). Now, isa() looks to me like a great candidate for constness... it should be pure, free of side effects, and I can't imagine any sane circumstances where this would not be so. So I consted it (svn r27028). But, oh, the object.pmc implementation of isa() calls VTABLE_get_class. No problem, that looks similarly pure, const it too (svn r27029). When data is declared as const, every other function that gets called with that data should be declared as const, too. This results in a chain reaction. "isa" likes to call "get_class"; "get_class" likes to call "type" and "name" (consted in the attached patch), "name" (and a few other things) like to call "get_string". This is where I ran into problems. Is get_string constable? It seems possible that someone somewhere would want to cache a stringification that was computationally intensive, for later reuse. So now (as bernhard kindly pointed out to me), this is starting to sound like a design issue. Am I going down the wrong path here? If I can't constify the vtable methods, the solution is to revert r27028 and r27029, and to *remove* constness from any functions which pass consted arguments to vtable methods. Mark
Index: src/vtable.tbl =================================================================== --- src/vtable.tbl (revision 27029) +++ src/vtable.tbl (working copy) @@ -22,12 +22,12 @@ void delprop(STRING* key) PMC* getprops() -INTVAL type() +INTVAL type() :const INTVAL type_keyed(PMC* key) INTVAL type_keyed_int(INTVAL key) INTVAL type_keyed_str(STRING* key) -STRING* name() +STRING* name() :const PMC* clone() PMC* clone_pmc(PMC* args) Index: src/pmc/class.pmc =================================================================== --- src/pmc/class.pmc (revision 27028) +++ src/pmc/class.pmc (working copy) @@ -1315,7 +1315,7 @@ */ - VTABLE INTVAL type() { + VTABLE INTVAL type() :const { Parrot_Class * const _class = PARROT_CLASS(SELF); return _class->id; } Index: src/pmc/object.pmc =================================================================== --- src/pmc/object.pmc (revision 27029) +++ src/pmc/object.pmc (working copy) @@ -148,7 +148,7 @@ */ - VTABLE STRING *name() { + VTABLE STRING *name() :const { PMC * const _class = VTABLE_get_class(interp, SELF); STRING * const class_name = VTABLE_get_string(interp, _class); @@ -540,7 +540,7 @@ */ - VTABLE INTVAL type() { + VTABLE INTVAL type() :const { PMC *_class = VTABLE_get_class(interp, SELF); return VTABLE_type(interp, _class); } Index: src/pmc/delegate.pmc =================================================================== --- src/pmc/delegate.pmc (revision 27028) +++ src/pmc/delegate.pmc (working copy) @@ -117,7 +117,7 @@ SUPER(method_name, sub_pmc); } - VTABLE STRING *name() { + VTABLE STRING *name() :const { return SELF->vtable->whoami; } @@ -125,7 +125,7 @@ return SELF->vtable->_namespace; } - VTABLE INTVAL type() { + VTABLE INTVAL type() :const { return SUPER(); } Index: src/pmc/ref.pmc =================================================================== --- src/pmc/ref.pmc (revision 27028) +++ src/pmc/ref.pmc (working copy) @@ -102,7 +102,7 @@ */ - VTABLE INTVAL type() { + VTABLE INTVAL type() :const { return VTABLE_type(interp, PMC_pmc_val(SELF)); } @@ -136,7 +136,7 @@ */ - VTABLE STRING *name() { + VTABLE STRING *name() :const { return SUPER(); } Index: src/pmc/deleg_pmc.pmc =================================================================== --- src/pmc/deleg_pmc.pmc (revision 27028) +++ src/pmc/deleg_pmc.pmc (working copy) @@ -64,11 +64,11 @@ return res; } - VTABLE STRING *name() { + VTABLE STRING *name() :const { return SELF->vtable->whoami; } - VTABLE INTVAL type() { + VTABLE INTVAL type() :const { return SELF->vtable->base_type; } Index: src/pmc/stmref.pmc =================================================================== --- src/pmc/stmref.pmc (revision 27028) +++ src/pmc/stmref.pmc (working copy) @@ -99,7 +99,7 @@ */ - VTABLE STRING *name() { + VTABLE STRING *name() :const { return SUPER(); } Index: src/pmc/pmcproxy.pmc =================================================================== --- src/pmc/pmcproxy.pmc (revision 27028) +++ src/pmc/pmcproxy.pmc (working copy) @@ -321,7 +321,7 @@ */ - VTABLE INTVAL type() { + VTABLE INTVAL type() :const { const Parrot_Class * const _class = PARROT_CLASS(SELF); return _class->id; } Index: src/pmc/default.pmc =================================================================== --- src/pmc/default.pmc (revision 27029) +++ src/pmc/default.pmc (working copy) @@ -433,7 +433,7 @@ */ - VTABLE INTVAL type() { + VTABLE INTVAL type() :const { return SELF->vtable->base_type; } @@ -463,7 +463,7 @@ */ - VTABLE STRING *name() { + VTABLE STRING *name() :const { return SELF->vtable->whoami; }