On Mar 22, 5:21 pm, Scott Sauyet <[email protected]> wrote:
> I think "reflects" (not "reflect" -- don't you love English where
> adding an "s" makes nouns plural and verbs singular?!) has a similar
> sense as "ActsAs".
>
> I was suggesting something different, namely that the inverted sense
> might be nicer to use in coding.  I still don't have a better name
> than "mismatches", which is fairly ugly.
>
> But it could be used like this:
>
>     if (mismatches(spec, obj) {
>         return;
>     }
>     // proceed knowing that the object matches the spec
>
> or like this:
>
>     var mismatch = mismatches(spec, obj)
>     if (mismatch) {
>         throw new Error(mismatch);
>     }
>     // proceed knowing that the object matches the spec
>
> I'm just a bit uncomfortable with the negative sense of this.
>
> What I didn't like about npup's original is that this was handled with
> two separate calls:
>
>     if (Acts.As(spec, obj) {
>         // proceed knowing that the object matches the spec
>     } else {
>         throw new Error(Acts.As.info());
>     }
>
> This wouldn't allow, for instance, the following:
>
>     if (Acts.As(spec1, obj1) {
>         if (Acts.As(spec2, obj2) {
>             // proceed knowing that all objects matches their specs
>         } else {
>             throw new Error(Acts.As.info());
>         }
>     } else {
>         throw new Error(Acts.As.info()); // fails to report accurately
>     }
>
> You could of course work something out, but it strikes me as
> difficult, which is why I suggested reversing the sense.
>
>     var mismatch1 = mismatches(spec1, obj1);
>     if (mismatches1) {
>         throw new Error(mismatch1);
>     }
>     var mismatch2 = mismatches(spec2, obj2);
>     if (mismatch2) {
>         throw new Error(mismatch2);
>     }
>     // proceed knowing that all objects matches their specs
>
> I'm not at all convinced by this, but I think it's worth considering.
>
>   -- Scott

I don't see the point of reversing the wording here? Why not simply
do:

if (!Acts.As(spec, obj)) {
  throw Error(Acts.As.info());
}
// proceed knowing...

And the nested stuff works, as far as I can see? I can't run this
(example below) in any console where I am right now, so haven't
checked 100%. Kick me in the global objects if I misunderstood you ;)

var spec1 = {
  name: "type-string"
  , age: "type-number"
}
, spec2 = {
  x: "type-number"
  , y: "type-number"
}
, obj1 = {
  name: "ape"
  , age: 22
}
, obj2 = {
  K: function (v) {return v;}
};
// let's go..
try {
  if (Acts.As(spec1, obj1)) {
    console.log("obj1 conformed with spec1");
    if (Acts.As(spec2, obj2)) {
      console.log("obj2 conformed with spec2");
    }
    else {
      throw Error(Acts.As.info());} // proper mismatch msg thrown
(obj2 failure)
  }
  else {
    throw Error(Acts.As.info()); // proper mismatch msg thrown here
too (obj1 failure)
  }
}
catch(err) {
  console.log("ERR: %s", err);
}

That said, I am one of those who prefer throwing immediately instead
of if-else-ing too much and thereby having the real work being done in
some unnecessarily indented part of the code :)

I've been thinking of more ways to get around the "shared" info
message quirk, but haven't found any super elegant solution yet. Had
it been C programming I imagine there would be no hesitation to just
stuff it in some supplied container object and return the boolean. But
of course we want to do better :)

Thanks again for useful input!
/p

-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/[email protected]/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/[email protected]/

To unsubscribe from this group, send email to
[email protected]

Reply via email to