On Mar 23, 2:35 am, Scott Sauyet <[email protected]> wrote:
> There are still of course techniques
> that are more difficult with this style, but they are not as bad as
> I'd thought.
>
> Am I right that this style would be more difficult?:
>
> var results1 = mismatch(spec1, obj1),
> results2 = mismatch(spec2, obj2);
> if (results1 || results2) {
> throw new Error(results1 || results2);
> }
> // proceed knowing that the objects match the spec
>
> Will this work?:
>
> var results1 = Acts.As(spec1, obj1),
> error1 = results1 ? null : Acts.As.info(),
> results2 = Acts.As(spec2, obj2),
> error2 = results2 ? null : Acts.as.info();
> if (error1 || error2) {
> throw new Error(error1 || error2);
> }
> // proceed knowing that the objects match the spec
>
I see what you mean. Multiple calls wrecks that pesky info variable,
so that's how it would have to be done i suppose. It clutters up the
caller's code more than I would want. I aimed for simple usage,
ideally oneliner-assertions.
> > 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 tend to be the same way, at least in JS. But I wouldn't like this
> sort of API to dictate my program structure.
Ack that.
> Or of course we could also simply return an object containing both a
> boolean status and, if the status is false, another object with
> details about the mismatch:
>
> var match = Acts.As(spec, obj);
> if (!match.results) {
> throw new Error(match.info);
> }
> // proceed knowing that the object matches the spec
>
> But this adds unnecessary clutter to the calling code.
>
> If we could make our object false-y, we'd get the best of all worlds.
> But, we can't as demonstrated by
>
> var obj1 = {valueOf: function() {return 5}}; console.log(7 +
> obj1); // 12
> var obj2 = {valueOf: function() {return false}}; console.log(!
> obj2); // false
>
Yes, this is what was in version 0.01 :) The returned object had to be
coerced (forcing the valueOf to kick in)
Oh and thanks for making me remember I should put a version number
in..
> In any case, a very interesting discussion. Thank you,
>
> -- Scott
Thank you too!
/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]