Returning ITERABLE type in Eiffel - iterable

I am trying to return Result type being ITERABLE[K].
All I know is that Iterable inherits from ITERATION_CURSOR, so that I made following unworking code but it doesn't compile.
obtainKey (v: V): ITERABLE[G]
local
myCollection: ITERABLE [G]
myCursor:ITERATION_CURSOR[G]
do
create {ITERABLE[G]} myCursor
Result := myCursor
My guess is that I have to do something like following, if it was c++ or Java,
ITERATION_CURSOR myCursor = new ITERABLE;
I don't know. My assumption could be wrong.
How can I do this kind of thing in Eiffel and make above code work?

The ITERABLE class is a deferred class (abstract in java) and a deferred class cannot be created. You have to use a class that is not deferred and that inherit from the ITERABLE class. Note that the ITERATION_CURSOR class is also deferred. What to use can change depending of what you need in your implementation. Here is an example using a LINKED_LIST:
obtain_key (v:V): ITERABLE[G]
local
my_list:LINKED_LIST[G]
do
create my_list.make
Result := my_list
end

Related

Calling private function within package body

According to Oracle documentation one can make items private in packages by declaring them in the body but not in the specification.
I have a procedure in this package that needs to call a function that should not be accessed outside of this package. Oracle SQL Developer returns PLS-00313 'ADD_STUDENT' not declared in this scope
Declaration:
PACKAGE SCHOOL AS
PROCEDURE ADD_PEOPLE(...);
END SCHOOL;
Body:
PACKAGE BODY SCHOOL AS
PROCEDURE ADD_PEOPLE(...)
...
ADD_STUDENT();
END ADD_PEOPLE;
FUNCTION ADD_STUDENT(...)
...
END ADD_STUDENT;
END SCHOOL;
I can't find an example of calling internal functions/procedures and whether the package declaration is needed ex:SCHOOL.ADD_STUDENT()
The issue you have (assuming that you are calling the correctly named procedure/function in the correct manner) is that you are trying to invoke a call to a function that hasn't yet been declared. There are two ways around this, assuming you want to keep the function private:
Declare the ADD_STUDENT function before any procedures/functions that invoke it.
Use forward declaration to declare the function before it is invoked.
So, for option 1, your example code would look like:
PACKAGE BODY SCHOOL AS
FUNCTION ADD_STUDENT(...)
...
END ADD_STUDENT;
PROCEDURE ADD_PEOPLE(...)
...
some_var := ADD_STUDENT();
END ADD_PEOPLE;
END SCHOOL;
/
And for option 2 your code would look like:
PACKAGE BODY SCHOOL AS
-- forward declared function
FUNCTION ADD_STUDENT(...);
PROCEDURE ADD_PEOPLE(...)
...
some_var := ADD_STUDENT();
END ADD_PEOPLE;
FUNCTION ADD_STUDENT(...)
...
END ADD_STUDENT;
END SCHOOL;
/
Personally, I favour option 1, as it means there's less stuff cluttering up the package body, but option 2 might be necessary if you have two modules that reference each other.
You need to define the private function before it is referenced in the body.
Simply define the function and then the procedure.
The package declaration is not needed - just call it with the name in the body as that name is already in scope.
In Package Specification your are defining function as ADD_STUDENT
While you are calling ADD_STUDENTS();
Please remove 'S' and compile it again.

Kotlin Instantiate Immutable List

I've started using Kotlin as a substitute for java and quite like it. However, I've been unable to find a solution to this without jumping back into java-land:
I have an Iterable<SomeObject> and need to convert it to a list so I can iterate through it more than once. This is an obvious application of an immutable list, as all I need to do is read it several times. How do I actually put that data in the list at the beginning though? (I know it's an interface, but I've been unable to find an implementation of it in documentation)
Possible (if unsatisfactory) solutions:
val valueList = arrayListOf(values)
// iterate through valuelist
or
fun copyIterableToList(values: Iterable<SomeObject>) : List<SomeObject> {
var outList = ArrayList<SomeObject>()
for (value in values) {
outList.add(value)
}
return outList
}
Unless I'm misunderstanding, these end up with MutableLists, which works but feels like a workaround. Is there a similar immutableListOf(Iterable<SomeObject>) method that will instantiate an immutable list object?
In Kotlin, List<T> is a read-only list interface, it has no functions for changing the content, unlike MutableList<T>.
In general, List<T> implementation may be a mutable list (e.g. ArrayList<T>), but if you pass it as a List<T>, no mutating functions will be exposed without casting. Such a list reference is called read-only, stating that the list is not meant to be changed. This is immutability through interfaces which was chosen as the approach to immutability for Kotlin stdlib.
Closer to the question, toList() extension function for Iterable<T> in stdlib will fit: it returns read-only List<T>.
Example:
val iterable: Iterable<Int> = listOf(1, 2, 3)
val list: List<Int> = iterable.toList()

Pascal passing object of inheritance class to procedure

I have types declared as:
TPlayer = class(TObject)
TBullet = class(TObject)
TEnemy = class(TObject)
and objects:
Player: TPlayer;
PlayerBullets: Array[1..20] of TBullet;
Enemies: Array[1..20] of TEnemy;
EnemyBullets: Array[1..20] of TBullet;
Now I want to create TBullet constructor, which can process informations from both Player and Enemies. In short, I want this constructor to handle both TPlayer and TEnemy objects.
My idea is:
constructor TBullet.Create(const Source: TObject);
Sadly it does not work. How to do this?
EDIT:
My exact problem is: when I pass TPlayer or TEnemy object to this constructor it doesn't see atributes of those objects. For example: TPlayer has attr xPos. If I use Bullet.Create(Player) and in TBullet.Create I use Source.xPos I get an error.
I can think of 3 ways to achieve that.
Have TPlayer and TEnemy both derive from the same base class that have all the information TBullet's constructor need, and have the constructor parameter of that type.
Define an interface contains all the information needed by TBullet and have TPlayer and TEnemy implement that interface
Leave everything "as is" and manage the different class in a "hard coded" manner in TBullet's constructor.
By that I mean:
constructor TBullet.Create(const Source: TObject);
var
vPlayer : TPlayer;
vEnemy : TEnemy;
begin
if Source is TPlayer then
begin
vPlayer := TPlayer(Source);
[Do whatever with vPlayer]
end else if Source is TEnemy then
begin
vEnemy := TEnemy(Source);
[Do Whatever with vEnemy]
end;
end;
Which solution is the best? That could be a debate in itself and largely dependant on your specific situation. Based solely on the name of your class, I'd guess option 1 could be valid. A "TCharacter" class could be created and use as a base calss for both TCharacter and TEnemy. But this is mere speculation at this point.
In windows there is little difference in inheriting from a parent with the common methods (could be abstract in the parent ) or implementing an interface (when again the behavior could be customized ).
If you have an enumeration commonParticipant( cpPlayer, cpEnemy) then Windows allows access to the ultimate parent IUnknown and then down again to a child interface that identifies the methods peculiar to that child, i.e. you can pass an ICommonParticipant including the commonParticipant and then work with either a iPlayer or IEnemy interface

Why use extra procedure to initialize values instead of doing it in constructor?

So i've been going trough types at my new work and they pretty much all look like this
create or replace TYPE BODY T_Some_Type AS
CONSTRUCTOR FUNCTION T_Some_Type
RETURN SELF AS RESULT AS
BEGIN
INTIALIZE();
RETURN;
END T_Some_Type;
MEMBER PROCEDURE INTIALIZE AS
BEGIN
var1 := 0;
var2 := 0;
var3 := 0;
END INTIALIZE;
END;
Being skilled in OOP but new to pl/sql, i keep wondering why use extra procedure to initialize variables, when it can be done directly in constructor making objects interface simpler and lighter on memory.
This is how i would normally do it :
create or replace TYPE BODY T_Some_Type AS
CONSTRUCTOR FUNCTION T_Some_Type
RETURN SELF AS RESULT AS
BEGIN
var1 := 0;
var2 := 0;
var3 := 0;
RETURN;
END T_Some_Type;
END;
Is there any advantage or this is recommended for some reason?
Please advise.
Please bear in mind that the object features in Oracle have evolved slowly since version 8.0, and yet are still pretty limited in some respects. This is because Oracle is a relational database with a structured and procedural programming paradigm: object-orientation is not a good fit.
So. In a language like Java we can override a method in a sub-type but execute the code in the parent's implementation with a super() call.
Before 11g Oracle had no similar mechanism for extending member functions. If we wanted to override a super-type's method we had to duplicate the code in the sub-type, which was pretty naff. There was a workaround which is not much less naff: extract the common code into a separate method in the super-type, and call that method in the super- type and its dependents.
In 11g Oracle introduced "Generalized Invocation". This allows us to call super-type code on a sub-type. Find out more. There is one major limitation with Generalized Invocation, and that is that we cannot use it with Constructor methods. Therefore, in its sub-types' constructors too our only choice is to put that code in a method like the initialize() in your question.

c# generic orderby

In my base-repository class
i wrote this function to make possible to retrive a sorted data collection from the DB.
T is a generic defined at Class level
public abstract class RepositoryBase<T>
where T : class
The code is this:
public IList<T> GetAll<TKey>(Expression<Func<T, bool>> whereCondition, Expression<Func<T, TKey>> sortCondition, bool sortDesc = false)
{
if (sortDesc)
return this.ObjectSet.Where(whereCondition).OrderByDescending(sortCondition).ToList<T>();
return this.ObjectSet.Where(whereCondition).OrderBy(sortCondition).ToList<T>() ;
}
My goal was to introduce a generic sort parameter so that i could call the function in this way:
repo.GetAll (model=>model.field>0, model=>model.sortableField, true)
i mean that i could specify the sorting field directly via anonymous function and so using Intellisense...
Unfortunately this function doesn't work as the last code line generate errors at compile time.
I tried also to call:
repo.GetAll<Model> (model=>model.field>0, model=>model.sortableField, true)
but this don't work.
How should i write the function to meet my goal?
i'm working with EF 5, c#, .NET 4.5
You're using ObjectSet which implements IQueryable<T>. That is extended by methods on System.Linq.Queryable, which accept Expression<Func< parameters. It is correct to use those Expression parameters, as you intend for execution to occur in the database, not locally.
A Func is an anonymous delegate, a .net method.
An Expression is a tree, which may be compiled into a Func, or may be translated into Sql or something else.
You showed us a really abstract use of the method, but not an actual use of the method, or the compiler error. I suspect the error you may be making is confusing the two type parameters.
You said:
repo.GetAll<Model> (model=>model.field>0, model=>model.sortableField, true)
But this generic parameter for this method represents the type of sortableField. If sortableField isn't a Model - this is wrong.
Instead, you should be doing something like this:
Repository<Person> myRepo = new Repository<Person>();
myRepo.GetAll<DateTime>(p => p.Friends.Count() > 3, p => p.DateOfBirth, true);
If specifying the sort type breaks your intended pattern of usage, consider hiding that key by using an IOrderer: Store multi-type OrderBy expression as a property

Resources