Discussions related to Visual Prolog
jarnold30
Posts: 19
Joined: 9 Aug 2020 15:30

How to check existence of an object

Unread post 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?
Harrison Pratt
VIP Member
Posts: 439
Joined: 5 Nov 2000 0:01

Re: How to check existence of an object

Unread post 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
jarnold30
Posts: 19
Joined: 9 Aug 2020 15:30

Re: How to check existence of an object

Unread post by jarnold30 »

Thanks.

But what does the nothing(This) line do?
Harrison Pratt
VIP Member
Posts: 439
Joined: 5 Nov 2000 0:01

Re: How to check existence of an object

Unread post 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:
User avatar
Thomas Linder Puls
VIP Member
Posts: 1398
Joined: 28 Feb 2000 0:01

Re: How to check existence of an object

Unread post by Thomas Linder Puls »

Regards Thomas Linder Puls
PDC
jarnold30
Posts: 19
Joined: 9 Aug 2020 15:30

Re: How to check existence of an object

Unread post by jarnold30 »

I learn something every day :-)
Post Reply