
-----------------------------------
smool
Tue Oct 23, 2012 4:33 pm

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:



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 :)

-----------------------------------
mirhagk
Tue Oct 23, 2012 7:31 pm

RE:Is it possible to take in a parent class as a paramter to a procedure, and then identify what the child class is?
-----------------------------------
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?

-----------------------------------
Dreadnought
Tue Oct 23, 2012 8:58 pm

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:
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.

-----------------------------------
Raknarg
Fri Oct 26, 2012 8:57 am

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


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

-----------------------------------
mirhagk
Fri Oct 26, 2012 9:12 am

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.

-----------------------------------
Raknarg
Mon Oct 29, 2012 3:49 pm

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?

-----------------------------------
TerranceN
Mon Oct 29, 2012 4:38 pm

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:


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:

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

If anyone is interested in learning scala, there's a [url=https://www.coursera.org/course/progfun]coursera course taught by the designer of the language that's pretty good.

-----------------------------------
mirhagk
Tue Oct 30, 2012 10:12 am

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:


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.
