Did you know ... | Search Documentation: |
Pack logtalk -- logtalk-3.86.0/examples/super_calls/SCRIPT.txt |
This file is part of Logtalk https://logtalk.org/ SPDX-FileCopyrightText: 1998-2023 Paulo Moura <pmoura@logtalk.org> SPDX-License-Identifier: Apache-2.0
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
% start by loading the example:
| ?- logtalk_load(super_calls(loader))
.
...
% the predicate get_local/1 calls the local/1 predicate in "self", % i.e. in the object that receives the get_local/1 message:
| ?- parent::get_local(Local)
.
Local = parent yes
| ?- prototype::get_local(Local)
.
Local = prototype yes
% the prototype::correct/1 predicate makes a "super" call, which preserves % "self", to the inherited get_local/1 predicate:
| ?- prototype::correct(Local)
.
Local = prototype yes
% the prototype::wrong/1 predicate sends a message to the parent, which % (necessarily) resets "self" to the message receiver:
| ?- prototype::wrong(Local)
.
Local = parent yes
% "super" calls force dynamic binding when the called predicate is dynamic:
| ?- bottom::value(Value)
.
Value = parent yes
| ?- middle::assertz(d(middle))
.
yes
| ?- bottom::value(Value)
.
Value = middle yes
| ?- middle::retractall(d(_))
.
yes
| ?- bottom::value(Value)
.
Value = parent yes