Hello, folks--

As part of a rather ambitious project that I'm not quite ready to
discuss[*], I have created a class called 'vobject', meaning 'validated
object'. Basically, it is a tuple that wraps an object whose value is
constrained by an arbitrary predicate; whenever you create an instance
of the class or try to set the value, the new value is checked against
the predicate. Here's what the code looks like:

MACRO: or-null-predicate ( quot -- )
    [ { [ null = ] } swap suffix '[ _ || ] ] ;

TUPLE: vobject predicate value ;
: <vobject> ( predicate value -- vobject )
    swap [ call t assert= ] 2keep
    vobject new
    swap >>predicate swap >>value ; inline

TUPLE: nullable-vobject < vobject ;
: <nullable-vobject> ( predicate value -- vobject )
    swap or-null-predicate [ call t assert= ] 2keep
    nullable-vobject new
    swap >>predicate swap >>value ; inline

TUPLE: free-vobject < vobject ;
: <free-vobject> ( value -- vobject )
    free-vobject new [ drop t ] >>predicate swap >>value ;

GENERIC: ::> ( vobject -- value )
GENERIC: >:: ( vobject newval -- vobject )

M: vobject ::>
    value>> ;
M: vobject >::
    over predicate>> dupd call( val -- ? ) t assert= >>value ;
M: free-vobject >::
    >>value ;

[The purpose of having the free-vobject class is to be able to easily
mix constrained and unconstrained values, and be able to access them in
a consistent way]

For the most part, this all seems to work as expected. However, I find
that the setter word, >::, raises an error but I don't understand why.
Here's what happens:

( scratchpad ) ! Here's a silly example
( scratchpad ) [ [ string? ] [ length 3 = ] bi and ] "abc" <vobject>

--- Data stack:
T{ vobject f ~quotation~ "abc" }
( scratchpad ) ! First, I'll manually perform the steps to validate and
set the value:

--- Data stack:
T{ vobject f ~quotation~ "abc" }
( scratchpad ) "xyz"

--- Data stack:
T{ vobject f ~quotation~ "abc" }
"xyz"
( scratchpad ) over predicate>> dupd call( val -- ? ) t assert= >>value 

--- Data stack:
T{ vobject f ~quotation~ "xyz" }
( scratchpad ) ! Yes, that's what's supposed to happen. Now I'll use the
>:: word:

--- Data stack:
T{ vobject f ~quotation~ "xyz" }
( scratchpad ) "mmm" >::

At this point I get an error popup saying:

    Generic word >:: does not define a method for the string class.
    Dispatching on object: "mmm"

Why is it dispatching on the object "mmm"? I would appreciate your
insights into this puzzle.


  [*] because I think what I have in mind may be way over my head, but I
  need to
      mess around with the idea a bit before deciding whether I really
      want to do it
-- 
Matt Gushee
m...@gushee.net

------------------------------------------------------------------------------
The Planet: dedicated and managed hosting, cloud storage, colocation
Stay online with enterprise data centers and the best network in the business
Choose flexible plans and management services without long-term contracts
Personal 24x7 support from experience hosting pros just a phone call away.
http://p.sf.net/sfu/theplanet-com
_______________________________________________
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk

Reply via email to