Computer Science Canada Is it possible to take in a parent class as a paramter to a procedure, and then identify what the child class is? |
Author: | smool [ 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:
This is just a skeleton example, I am well aware this is definitely not the proper way to assign ranges to weapons ![]() |
Author: | mirhagk [ 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? | ||
Does this not work? |
Author: | Dreadnought [ 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:
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. |
Author: | Raknarg [ 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
Simple solution, though perhaps not as applicable |
Author: | mirhagk [ 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. |
Author: | Raknarg [ 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? |
Author: | TerranceN [ 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:
Or in a language like scala with pattern matching:
If anyone is interested in learning scala, there's a coursera course taught by the designer of the language that's pretty good. |
Author: | mirhagk [ 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:
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. |