Is it possible to take in a parent class as a paramter to a procedure, and then identify what the child class is?
Author |
Message |
smool
![](http://compsci.ca/v3/uploads/user_avatars/19584221104f5399411b2b3.jpg)
|
Posted: Tue Oct 23, 2012 4:33 pm Post subject: Is it possible to take in a parent class as a paramter to a procedure, and then identify what the child class is? |
|
|
What is it you are trying to achieve?
Say I have a procedure that takes in a parent class as parameter C. Then inside the procedure, I would to do thing A if the parameter is child class C1, and do thing B if the parameter is child class C2.
example:
Turing: |
class Weapon
export SetRange
var range : real
proc SetRange (r : real)
reange := r
end SetRange
end class Weapon
class Sword
inherit Weapon
end Sword
class Bow
inherit Weapon
end Bow
proc SetRange (w : ^Weapon)
if w is Sword then % This is where I would like to be able to distinguish between different child classes
w -> SetRange (0)
elsif w is Bow then
w -> SetRange (900)
end if
end SetRange
|
This is just a skeleton example, I am well aware this is definitely not the proper way to assign ranges to weapons ![Smile Smile](http://compsci.ca/v3/images/smiles/icon_smile.gif) |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
mirhagk
|
Posted: Tue Oct 23, 2012 7:31 pm Post subject: RE:Is it possible to take in a parent class as a paramter to a procedure, and then identify what the child class is? |
|
|
Turing: | class Weapon
export SetRange
var range : real
proc SetRange (r : real)
range := r
end SetRange
end Weapon
class Sword
inherit Weapon
end Sword
class Bow
inherit Weapon
end Bow
proc SetRange (w : ^Bow)
w -> SetRange (900)
end SetRange
proc SetRange (w: ^Sword)
w -> SetRange (0)
end SetRange
|
Does this not work? |
|
|
|
|
![](images/spacer.gif) |
Dreadnought
|
Posted: Tue Oct 23, 2012 8:58 pm Post subject: Re: Is it possible to take in a parent class as a paramter to a procedure, and then identify what the child class is? |
|
|
What you want is polymorphism. I believe there is a tutorial in The Turing Walkthrough on the subject.
Here's an example:
Turing: | class Parent
export className, virtual
var className : string := "Parent"
deferred proc virtual () % the body of a deferred routine may be defined later (if we do not call the function we don't even need a body)
end Parent
class Child1
inherit Parent
className := "Child1" % We can change the initial values of member variables
body proc virtual () % Here we give a body to the virtual function
put "Hi!"
end virtual
end Child1
class Child2
inherit Parent
className := "Child2"
body proc virtual () % And here a different body
put "Hello"
end virtual
end Child2
proc Greeting (ptr : ^Parent)
put ptr -> className, " says: " ..
ptr -> virtual
end Greeting
var objects : array 1 .. 2 of ^Parent
new Child1, objects (1)
new Child2, objects (2)
for i : 1 .. 5
Greeting (objects (Rand.Int (1, 2)))
end for |
As you can see, the real power lies in the virtual function which could potentially be completely different from one child to the next.
@mirhagk unfortunately you cannot overload functions in Turing, so that will not compile. |
|
|
|
|
![](images/spacer.gif) |
Raknarg
![](http://compsci.ca/v3/uploads/user_avatars/3745510004d8be6689b92f.jpg)
|
Posted: Fri Oct 26, 2012 8:57 am Post subject: RE:Is it possible to take in a parent class as a paramter to a procedure, and then identify what the child class is? |
|
|
Probably a cheap way of doing so, but what about keeping track of a variable that tells you what class it is, like a string with "sword" or "bow"? then you could just do
Turing: |
proc SetRange (w : ^Weapon)
if w -> wType = "Sword" then
w -> SetRange (0)
elsif w -> wType = "Bow" then
w -> SetRange (900)
end if
end SetRange
|
Simple solution, though perhaps not as applicable |
|
|
|
|
![](images/spacer.gif) |
mirhagk
|
Posted: Fri Oct 26, 2012 9:12 am Post subject: RE:Is it possible to take in a parent class as a paramter to a procedure, and then identify what the child class is? |
|
|
@Dreadnought, I forgot that turing had it's OOP thrown haphazardly together. @OP if you want clean OOP code then you can't really use Turing, Dreadnought's way is the best way OOP can be used (and it's still pretty ugly). Racknarg's way is traditional turing style, which ignores the ability of OOP. |
|
|
|
|
![](images/spacer.gif) |
Raknarg
![](http://compsci.ca/v3/uploads/user_avatars/3745510004d8be6689b92f.jpg)
|
Posted: Mon Oct 29, 2012 3:49 pm Post subject: RE:Is it possible to take in a parent class as a paramter to a procedure, and then identify what the child class is? |
|
|
So can other languages do basically what OP said? |
|
|
|
|
![](images/spacer.gif) |
TerranceN
|
Posted: Mon Oct 29, 2012 4:38 pm Post subject: Re: Is it possible to take in a parent class as a paramter to a procedure, and then identify what the child class is? |
|
|
Yep. In a language like python you can just check the type directly using the type function:
Python: |
class A(object):
pass
class B(object):
pass
def printClass(obj):
if type(obj) == A:
print "This is an A object"
elif type(obj) == B:
print "This is a B object"
else:
print "This object type is unknown"
someA = A()
someB = B()
printClass(someA)
printClass(someB)
printClass(5)
|
Or in a language like scala with pattern matching:
scala: |
class Parent() {}
class ChildA() extends Parent {}
class ChildB() extends Parent {}
object Test {
def printClass(x: Parent) = x match {
case _: ChildA => println("This is an A object")
case _: ChildB => println("This is a B object")
case _: Parent => println("This is at least a Parent object")
}
def main(args: Array[String]) = {
val someA = new ChildA()
val someB = new ChildB()
printClass(someA)
printClass(someB)
}
}
|
If anyone is interested in learning scala, there's a coursera course taught by the designer of the language that's pretty good. |
|
|
|
|
![](images/spacer.gif) |
mirhagk
|
Posted: Tue Oct 30, 2012 10:12 am Post subject: RE:Is it possible to take in a parent class as a paramter to a procedure, and then identify what the child class is? |
|
|
and most languages support things like operator overloading, and even creating entire classes where a type is a parameter (templates in C++, generics in C# (and java?)).
the lack of operator overloading, duck typing or type checking mechanism is a serious drawback to Turing. You could mimic it somewhat by doing something like the following:
Turing: |
class Object
export className, virtual
var className : string := "Parent"
deferred proc GetType()
end Parent
class Child1
inherit Object
className := "Child1" % We can change the initial values of member variables
body proc GetType ()
return className
end GetType
end Child1
|
Similar to what dreadnought has. This way you could define a global Object class that every class you make should inherit from, and implement some key methods (similar to many OOP languages). In C# the methods every object support are:
Equals(Object obj)
GetType()
ToString()
GetHashCode()
The first 3 at least could be very useful to have in every class in Turing. For the most part those 3 are consistently across many OOP languages. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
|
|