Page 1 of 1

How to check existence of an object

Posted: 4 Apr 2021 9:48
by jarnold30
Hi, this is probably a stupid noobie question, but here goes...

In ordinary prolog, I can declare that something exists using a statement like

fred("jim").

And then I can use fred("jim") in a predicate to check that this object exists.

But if I create an object

Jim = fred::new("jim"),

how do I later check for the existence of Jim?

Re: How to check existence of an object

Posted: 4 Apr 2021 15:28
by Harrison Pratt
One way would be to create a class database in your base class and store the object identifier information you need in that database when you create each object, something like that below. You will need to manage removing personObj/2 facts when objects are destroyed and you also should remember that class facts persist as long as your application is active.

Code: Select all

implement person     open core class facts     personObj : (string Name, person ObjID). clauses     new(Name) :-         % ... do various things         assert(personObj(Name, This)),         nothing(This).   clauses % declare in person.cl to access object information     tryGetObject(Name) = PersonObject :-         personObj(Name, PersonObject),         !. end implement person

Re: How to check existence of an object

Posted: 5 Apr 2021 8:58
by jarnold30
Thanks.

But what does the nothing(This) line do?

Re: How to check existence of an object

Posted: 5 Apr 2021 10:29
by Harrison Pratt
nothing(...) does nothing. It's a way of making the IDE capture an unused variable so you can see it when running the debugger. Unused variables do not consistently display in the Variables window when debugging without making them appear as being used to the compiler.

It has no place in production code. I should have removed it before posting. :oops:

Re: How to check existence of an object

Posted: 5 Apr 2021 11:21
by Thomas Linder Puls

Re: How to check existence of an object

Posted: 5 Apr 2021 13:28
by jarnold30
I learn something every day :-)