[m-users.] Typeclass error resolving a specific function.

Sean Charles (emacstheviking) objitsu at gmail.com
Wed Oct 11 08:32:19 AEDT 2023


I've modified a hit detection system to change the status of objects instead of collecting ids, but when I try to compile now, for this type class definition:

:- typeclass hittable(T) where
[
   func id(T)        = int,
   func as_point(T)  = pointf,
   func as_circle(T) = vector3f,
   func as_rect(T)   = rectangle,
   func is_hit(T)    = bool,
   pred set_hit(T::in, T::out) is det
].

    % Falling object: something to be avoided!
    %
:- instance hittable(fobject) where
[
  id(T)        = T ^fid,
  as_point(T)  = fp_float_pt(T ^xy),
  as_circle(T) = fp_float_v3(T ^xy, 1.0),
  func(as_rect/1) is fo_bounds_rect,
  is_hit(T)    = T ^hit,
  (set_hit(!T) :- !T ^hit := yes)
].

    % Missile: a point like object.
    %
:- instance hittable(missile) where
[
  id(T)        = T ^missile_id,
  as_point(T)  = fp_float_pt(T ^mxy),
  as_circle(T) = fp_float_v3(T ^mxy, 1.0),
  func(as_rect/1) is missile_bounds_rect,
  is_hit(T)    = T ^hit,
  (set_hit(!T) :- !T ^hit := yes)
].

    % Player. The players rocket ship.
    %
:- instance hittable(player) where
[
  id(_)        = -1,
  as_circle(T) = v2_circle(T ^xy, 1.0),
  as_point(T)  = v2_pointf(T ^xy),
  is_hit(_)    = no, %% SAFE?
  func(as_rect/1) is player_bounds_rect,
  (set_hit(!T) :- !T ^hit := yes)   <===== COMPILER ERROR
].

I get this error:

level_ufo.m:767: In clause for type class method implementation:
level_ufo.m:767:   type error in unification of variable `STATE_VARIABLE_T'
level_ufo.m:767:   and functor `hit :='/2.
level_ufo.m:767:   variable `STATE_VARIABLE_T' has type `player.player',
level_ufo.m:767:   functor `hit :='/2 has overloaded type {
level_ufo.m:767:     'hit :='(ufo.ufo, bool.bool): ufo.ufo,
level_ufo.m:767:     'hit :='(player.missile, bool.bool): player.missile,
level_ufo.m:767:     'hit :='(level_ufo.fobject, bool.bool): level_ufo.fobject
level_ufo.m:767:   }.
level_ufo.m:767:   The partial type assignment was:
level_ufo.m:767:     STATE_VARIABLE_T_0_6: player.player
level_ufo.m:767:     STATE_VARIABLE_T_7: player.player
level_ufo.m:767:     V_9: bool.bool


I just can't see why it doesn't like this one but the previous two would appear to be fine, the error message confirms the type T as player.player which is correct but i don't understand why the error is present. Is it because the flags are all called hit in the different discriminated union types ?

Thanks.
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mercurylang.org/archives/users/attachments/20231010/7a6d0c56/attachment.html>


More information about the users mailing list