In adding has_type/2 clauses to my code there is a need to also check the structure; currently using subsumes_term/2
e.g.
error:has_type(boolean_object,Object) :-
must_be(ground,Object),
(
subsumes_term(object(boolean,_),Object)
;
throw(error(type_error(boolean_object, Object), _))
),
Object = object(_,Value),
must_be(oneof([true,false]),Value).
:- begin_tests(boolean_tests).
test(101,[error(type_error(boolean_object,object(bool,true)),_)]) :-
must_be(boolean_object,object(bool,true)).
test(102,[error(type_error(oneof([true,false]),1),_)]) :-
must_be(boolean_object,object(boolean,1)).
test(103,[error(type_error(boolean_object,obj(boolean,true)),_)]) :-
must_be(boolean_object,obj(boolean,true)).
:- end_tests(boolean_tests).
While I decided to use type_error from the list of ISO error types, I don’t know if that is the correct one; should a different one be used or a new error type created?
I think of this a structure error and not as type error, but is it common to think of the functor being invalid as a type error? If I think of object(boolean,true)
instead like (object,boolean,true)
as they did with Prolog of old, then it does make sense as a type error.
Any other useful feedback is welcome.
Related predicates of interest: Analysing and Constructing Terms