Object name as variable in Logtalk

Matek Statek

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).

Paulo Moura

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Integer as object_identifier in Logtalk

From Dev

New object with name in variable

From Dev

Dynamic variable name in object

From Dev

Deserialize into object with variable name

From Dev

Getting the object variable name in JavaScript

From Dev

How to use variable name in object?

From Dev

Javascript object property with variable name

From Javascript

Property name on object from variable

From Javascript

Javascript use variable as object name

From Dev

R : name of an object stored in a variable

From Dev

How to concatenate variable with an object name?

From Dev

Initializing object with name vs variable

From Dev

how to use the variable as the name of an object?

From Dev

Python, use variable as object "name"

From Dev

reference object name with variable - javascript

From Dev

Using variable *name* as property name in object literal

From Dev

Assign object variable name to a variable javascript or typescript

From Dev

Reference object variable name within object itself

From Dev

Angular template and passed in object variable name

From Dev

check if string variable name is a javascript fuction of an object

From Java

Get value of java object variable name

From

How to convert variable (object) name into String

From Dev

Create a variable with custom name and assigning it an object DataFrame

From Python

Get variable name from NameError object

From Dev

add new properties to object with variable name in mongoose

From Dev

JavaScript set anonymous Object key to variable name

From Javascript

Add a property to a JavaScript object using a variable as the name?

From Dev

Need to concatenate object name with counter variable properly

From Javascript

JavaScript object: access variable property by name as string

Related Related

  1. 1

    Integer as object_identifier in Logtalk

  2. 2

    New object with name in variable

  3. 3

    Dynamic variable name in object

  4. 4

    Deserialize into object with variable name

  5. 5

    Getting the object variable name in JavaScript

  6. 6

    How to use variable name in object?

  7. 7

    Javascript object property with variable name

  8. 8

    Property name on object from variable

  9. 9

    Javascript use variable as object name

  10. 10

    R : name of an object stored in a variable

  11. 11

    How to concatenate variable with an object name?

  12. 12

    Initializing object with name vs variable

  13. 13

    how to use the variable as the name of an object?

  14. 14

    Python, use variable as object "name"

  15. 15

    reference object name with variable - javascript

  16. 16

    Using variable *name* as property name in object literal

  17. 17

    Assign object variable name to a variable javascript or typescript

  18. 18

    Reference object variable name within object itself

  19. 19

    Angular template and passed in object variable name

  20. 20

    check if string variable name is a javascript fuction of an object

  21. 21

    Get value of java object variable name

  22. 22

    How to convert variable (object) name into String

  23. 23

    Create a variable with custom name and assigning it an object DataFrame

  24. 24

    Get variable name from NameError object

  25. 25

    add new properties to object with variable name in mongoose

  26. 26

    JavaScript set anonymous Object key to variable name

  27. 27

    Add a property to a JavaScript object using a variable as the name?

  28. 28

    Need to concatenate object name with counter variable properly

  29. 29

    JavaScript object: access variable property by name as string

HotTag

Archive