Search code examples
objectintegeridentifierlogtalk

Integer as object_identifier in Logtalk


I have a protocol students:

:- protocol(student).
    :- public([
        name/1,
        surname/1,
        studies/1,
        marks/1
    ]).
:- end_protocol.

Now I want to make an object which name is an ID (Integer), but when I'm trying to do this with create_object(18342, [implements(student)], [], [name(john), surname(smith), studies(it), marks(ok)]).

swilgt gives mi the error:

ERROR: Type error: 'object_identifier' expected, found '18342' (an integer)

Ofc I could use quotation marks, but I don't want to. Is there an option to use integer as a name, or have I use string and add id/1 into the protocol?


Solution

  • Indeed is (currently) not possible to use an integer as an object identifier. One alternative is indeed to use an atom, e.g. '133' instead of 123. Don't use a string, e.g. "123" as the actual meaning of double quoted text depends on the double_quotes standard Prolog flag, whose only portable value is codes(i.e. "123" is parsed as [49,50,51].

    A portable way to convert between an integer and an atom is to use the standard predicates number_codes/2 and atom_codes/2 (or number_chars/2 and atom_chars/2). Some supported backend Prolog systems also provide proprietary built-in predicates to directly convert between numbers and atoms.