Eiffel: Creator instruction applies to target of a deferred type - eiffel

Class A
deferred Class A
feature --
item: X -- X is deferred
set_item_for_some_reason (param: N)
do
create item.make_from_param (param)
end
end -- class
Class B
Class B inherit
A
feature --
item: Y -- Y not deferred inherits from X
end -- class
I'd like to create in the same class an attribute which will be defined in the descendent and get a Creator instruction applies to target of a deferred type Error which makes sens in a way of reduced context, but not in the context I intend it to do it.
For me it does make sense to be able to create an object in current deferred class, I wont have to implement in all the descendants! which will be a wrong design... something like this:
deferred Class A
feature --
item: X -- X is deferred
set_item_for_some_reason (param: N)
do
set_item_from_param (param)
end
set_item_from_param (param: N)
deferred
end
end -- class
Class B inherit
A
feature --
item: Y -- Y not deferred
set_item_from_param(param: N)
do
create item.make_from_param (param)
end
end -- class
Am I wrong with my design or is it a restriction which is still discussed about Eiffel compiler as I understood? and if yes, what would be the best practice workaround?

A possible solution is to use generic classes. In the class A, the formal generic parameter has a creation constraint that the corresponding actual generic parameter should have a particular creation procedure:
class A [G -> X create make_from_param end] feature
item: detachable G
set_item_for_some_reason (param: N)
do
create item.make_from_param (param)
end
end
A descendant class can specify an actual generic that has this creation procedure:
class B inherit
A [Y]
end
To make sure we are on the same page, here is the code of classes X and Y:
deferred class X feature
make_from_param (param: N)
deferred
end
end
class Y inherit
X
create
make_from_param
feature
make_from_param (param: N)
do
end
end
You can add as many such descendants as you wish. The main restriction is that whenever class A is used, its actual generic parameter should have the specified feature as a creation one. For example, it's OK to declare a type A [Y]. But A [X] would trigger an error.
If in a descendant, the type of item should not be fixed yet, it's possible to propagate it, repeating the constraint on the formal generic:
class C [G -> X create make_from_param end] inherit
A [G]
end

Related

Is it possible to make a create which returns a boolean

hi i dont know if i asked the question clearly.
i have a deferred class "animal" which contains two features : "bite" (this returns a boolean -> bite:BOOLEAN) and "speak"(speak(word:BOOLEAN)).
now i made a class named "dog" what inherit from "animal". i redefined the two features without a compiler error. now i want to make a create, which contains the function bite (create bite:BOOLEAN).
this gaves me a compiler error, if i try it with the other feature it works fine.
my error code : VGCP(2) creators part lists improper identifier.
thank u for ur help
My Application.e:
note
description : "root class of the application"
date : "$Date$"
revision : "$Revision$"
class
APPLICATION
inherit
ARGUMENTS_32
create
make
feature
d1:DOG
feature {NONE} -- Initialization
make
-- Run application.
do
print(d1.bite)
print ("Hello Eiffel World!%N")
end
end
my animal class:
deferred class
ANIMAL
feature
bite_:BOOLEAN
deferred
end
feature
speak(word:BOOLEAN)
deferred
end
end
my dog class:
class
DOG
inherit
ANIMAL
redefine
bite_,speak_
end
create
bite_
feature
bite_:BOOLEAN
do
Result:=5<3
end
feature
speak(word:BOOLEAN)
do
print("yes")
end
I'm not sure what is the intended semantics of feature speak, so I'll focus solely on feature bite, or, to express the supposed intention better can_bite. Class ANIMAL is similar to what you have:
deferred class
ANIMAL
feature -- Status report
can_bite: BOOLEAN
-- Can this animal bite?
deferred
end
end
With class DOG, two variations are possible: when every dog bites (possibly, depending on some internal state), when the ability to bite is set at object creation time.
In the first scenario, can_bite is a function and the class looks like
class
DOG
inherit
ANIMAL
feature -- Status report
can_bite: BOOLEAN
-- Can this animal bite?
do
Result := True -- It could also be some expression.
end
end
In the second scenario, can_bite is an attribute initialized by a creation procedure, and the class looks like
class
DOG
inherit
ANIMAL
create
make
feature {NONE} -- Creation
make (is_biting: BOOLEAN)
-- Initialize with ability `is_biting`.
do
can_bite := is_biting
end
feature -- Status report
can_bite: BOOLEAN
-- Can this animal bite?
end
Note that in both cases the client has to create the dog object before making any calls on it. In the first case, the default creation procedure is used, and the client code looks like
create d1
print (d1.can_bite)
In the second case, the specific creation procedure is used (it initializes can_bite), and the client code looks like
create d1.make (True)
print (d1.can_bite)

What does the "self" keyword references in this Ruby case?

I would like some help understanding what does self refers to in this case. I understand that self refers to the class, module, etc that "owns" the code currently executing. In my example, my_each is an instance method for the Enumerable module. I'd like to know how does the self keyword works so that when I pass it to my example array it references it.
module Enumerable
def my_each
i = 0
while i < self.size
yield(self[i])
i += 1
end
self
end
end
[2,4,5].my_each { |i|
puts i
}
=> 2
=> 4
=> 5
What self refers to depends on the context. In your case, in an instance method, self refers to the object receiver of your instance method, so in your case the array [2, 4, 5].
But self can refer to other objects too. For example,
class Foo
puts self
end
prints Foo because in that context self refers to the class object. And that's why the two following definitions are the same thing
class Foo
def Foo.m
end
end
class Foo
def self.m
end
end
In Ruby it's all about self and every method is always executed against a particular self.
In your example instance method my_each self will refer to an instance which is using the method.
As you said self can also refer to a class, module..
It is a very powerfull ruby keyword since it can be used to create metaclasses.
If you are interested in understanding more I suggest you read chapter 5 in The Well Grounded Rubyist
self is a special variable that points to the object that "owns" the currently executing code. Ruby uses self everywhere:
For instance variables: #myvar
For method and constant lookup
When defining methods, classes and modules.
Inside of an instance method
In the code below, reflect is an instance method. It belongs to the object we created via Event.new. So self points to that object.
class Event
def reflect
self
end
end
g = Event.new
g.reflect == g # => true
Inside of a class method
For this example, reflect is a class method of Ghost. With class methods, the class itself "owns" the method. self points to the class.
class Event
def self.reflect
self
end
end
Event.reflect == Event # => true
It works the same with "class" methods inside of modules. For example:
module Event
def self.reflect
self
end
end
Event.reflect == Event # => true
Remember, classes and modules are treated as objects in Ruby. So this behavior isn't that different from the instance method behavior we saw in the first example.
Inside of a class or module definition
One feature of Ruby that makes it such a good fit for frameworks like Rails is that you can execute arbitrary code inside class and module definitions. When you put code inside of a class/module definition, it runs just like any other Ruby code. The only real difference is the value of self.
As you can see below, self points to the class or module that's in the process of being defined.
class Event
self == Event # => true
end
module Join
self == Join # => true
end
Inside mixin methods
Mixed-in methods behave just like "normal" instance or class methods when it comes to self. This makes sense. Otherwise the mixin wouldn't be able to interact with the class you mixed it into.
Instance methods
Even though the reflect method as defined in the module, its self is the instance of the class it was mixed into.
module Reflection
def reflect
self
end
end
class Event
include Reflection
end
g = Event.new
g.reflect == g # => true
Class methods
When we extend a class to mix in class methods, self behaves exactly like it does in normal class methods.
module Reflection
def reflect
self
end
end
class Event
extend Reflection
end
Event.reflect == Event # => true
Inside the metaclass
Chances are you've seen this popular shortcut for defining lots of class methods at once.
class Event
class << self
def method1
end
def method2
end
end
end
The class << foo syntax is actually pretty interesting. It lets you access an object's metaclass - which is also called the "singleton class" or "eigenclass." I plan on covering metaclasses more deeply in a future post. But for now, you just need to know that the metaclass is where Ruby stores methods that are unique to a specific object.
If you access self from inside the class << foo block, you get the metaclass.
class << "test"
puts self.inspect
end
# => #<Class:#<String:0x007f8de283bd88>
Outside of any class
If you're running code outside of any class, Ruby still provides self. It points to "main", which is an instance of Object:
puts self.inspect # => main

Check OWL Class

Suppose the range of an object property is defined through a class expression. I want to know whether a particular instance of the class can be used in the range of the object property. Is it possible to use the OWL API and check if a particular class is subsumed by this class expression ?
OWLAPI cannot provide a complete answer for this - an OWLReasoner implementation is necessary for complete results.
Given an implementation of OWLReasoner, to check entailment you can either list all the subclasses of a class expression and check if the class you're interested in appears in the response, or ask the reasoner if your class of interest is a subclass, i.e.,
OWLDataFactory df = ...
OWLClassExpression ce = ...
OWLClass c = ...
OWLReasoner r = ...
OWLAxiom ax = df.getOWLObjectSubClassOf(c, ce);
boolean cIsSubclassOfCE = r.isEntailed(ax);

Eiffel: change the signature of a inherited feature

How is the correct way to change the signature of a feature in Eiffel if any? if there is no option as I understood, how would be the correct way to define in a parent class that there will be a feature to be able to select but still don't know arguments number and types (types are resolvable with polymorphism...)
Is the only available playing with polymorphism having an argument into class a to select of type ANY?
class SELECTABLE
class SELECTABLE
select
deferred
end
end -- class
class DB_SERVICE
class DB_SERVICE
inherit
SELECTABLE
(...)
feature -- Status setting
select (a_db_connection: DB_CONNECTION)
local
l_qry: STRING
do
item := first_item_from_qry (l_qry)
end
end -- class
Having the following in class SELECTABLE is indeed a solution:
select (a: ANY)
deferred
end
Another solution is to use TUPLE:
select (a: TUPLE)
deferred
end
This allows you to have more than one argument in descendant classes:
select (a: TUPLE [db_connection: DB_CONNECTION])
do
a.db_connection.do_something
end
which can be called:
a_db_server.select (a_db_connection)
or:
select (a: TUPLE [db_connection: DB_CONNECTION; db_parameters: DB_PARAMETERS])
do
a.db_connection.do_something (a.db_parameters)
end
which can be called:
a_db_server.select (a_db_connection, a_dp_parameters)
Note that in that case, the need for the explicit tuple notation [...] in the argument of select is optional.
And of course, select is a keyword in Eiffel. You will have to use another name for your feature.

Ndb Model validation with interdependent properties?

I want to validate an NDB Model. My model is something like :
class Sample(ndb.Model):
x = ndb.IntegerField()
y = ndb.IntegerField()
I want to ensure that x < y at any point of time.
Approaches I have tried :
Write Validator function and call it from overridden constructor. But the field values may be changed later. And it should validate everytime it saves
Add a _pre_put_hook - but that seems to be overkill. Plus - this won't throw error until an entity is actually saved in datastore
Ideally what I want is that whenever a or b is changed - A function should be triggered which will validate if the entity is valid else throw error.
Note : Currently I am using _pre_put_hook.
This should be done outside of the ndb.Model logic.
Just create a function inside the model like the following:
class Sample(ndb.Model):
x = ndb.IntegerField()
y = ndb.IntegerField()
#classmethod
def new(cls, x, y):
if x >= y:
raise Exception
return cls(x=x, y=y)
Then replace the generic Exception I have used with a custom one and catch it with a try block.

Resources