Eiffel: change the signature of a inherited feature - eiffel

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.

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

Eiffel: Creator instruction applies to target of a deferred type

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

How to compare two objects by value

I need to compare two objects of the same class by value to see whether all their values match or not.
For background this is so I can compare before and after values of a wpf datagrid row
Say the objects are both House class with properties of name, street, town (all strings).
So the class would be
public class House
public property name as string
public property street as string
public property town as string
end class
Should I
1) override equals in the House class and in it check name=name, street=street, town=town
2) make the House class implement IComparable and create a compare function that implements it, checking each property as 1
3) there's a better way you know that I dont!
I'd appreciate an example based on this scenario if possible.
Many thanks
You should be using Option 1 : Overriding the Equals method.
Why ?
Because the Equals() method is supposed to be used when you want to compare if two objects are the same.
So what's the use of IComparabe ?
IComparable interface has a different purpose. Its goal is to check if an object is supposed to go before or after another object. Therefore this is used by sorting methods.
You can implement the IComparable interface and check if two object's CompareTo() method return 0. However it only means that they are supposed to get the same ranking, not that they are equals...
Is there another approach ?
There are plenty differents ways of doing what you want to do. But since there is a simple and elegant method that is here, let's use that one. The main difficulty in programming an application is to find the tools that are already here to do what you want...
So how to Override the Equals() method ?
This link to MSDN explains how to override the Equals method
In short (I'm just copy/pasting from MSDN and removing error checking for clarity here) :
Public Class Point
Protected x As Integer
Protected y As Integer
Public Sub New (xValue As Integer, yValue As Integer)
Me.x = xValue
Me.y = yValue
End Sub
Public Overrides Overloads Function Equals(obj As Object) As Boolean
Dim p As Point = CType(obj, Point)
Return Me.x = p.x And Me.y = p.y
End Function
End Class
Do not use this straight ahead and read article first, as you must do some error checking in the Equals, because it could throw some exception when converting Object to Point...

WPF Generics:How to define a list of objects as parameter of a function

I want create a generic (general) method which accepts the return types of LINQ 2 SQL query (a list of objects- IEnumerable) against different table as the parameter of the function. How can I define my parameter for the function ? I will use this function to bind the data to a grid.
I got the answer here
Public Sub FormatGrid(Of T)(ByVal varList As IEnumerable(Of T))
End Sub

Resources