Is this possible to get the name of an object as a variable? I’m trying to make a database where each object represents each person. I’ve got objects with [name/1, surname/1], but when I ask e.g.
X::name(john).
it gives me an error. Ofc there is no problem to get the atom by using this method:
object_id::name(X).
The ::/2
message sending control construct indeed requires a bound first argument at call time. But you can enumerate existing objects using the current_object/1
built-in predicate:
| ?- current_object(Person), Person::name(john).
...
However, this solution may also result in errors as we will be enumerating all objects by backtracking and not all of them will understand a name/1
message. A better solution is thus to only enumerate objects that understand the name/1
message. Assuming that all objects representing a person implement (directly or through inheritance) a person_protocol
, we can use the conforms_to_protocol/2
built-in predicate:
| ?- conforms_to_protocol(Person, person_protocol),
Person::name(john).
...
See https://logtalk.org/manuals/refman/predicates/conforms_to_protocol_2_3.html for details.