Wish: Polymorphism or likewise.

Status
Not open for further replies.

Kimmowich

Member
Licensed User
Longtime User
Hey

Since we cant use TYPE as object and therefore cant get the benefit of Polymorphism, are there any plans in that direction?

Would it at some point be possible to either use SUBs as parameter in SUB-calls or even as an object/Sub in TYPE?


Thx in advance

Kim
 

Kimmowich

Member
Licensed User
Longtime User
Thx for the quick response..

I did look at CallSub.. but. it dosnt do the trick. Perhaps im missing something here.

Since TYPE cant hold anything else than Data, I need a way to parse various SUBs (but only parse 1 for every call) with different calculation so I dont have to make all the code inside one SUB, and the called SUB can have multible function with the same data.

Hope this make sense.. English is not really my language :)

Thx

Kim
 

Kimmowich

Member
Licensed User
Longtime User
I made this quick example to show what i mean..

B4X:
Sub CalcOne(v As Int) As Int
   Return v * 2
End Sub

Sub CalcTwo(v As Int) As Int
   Return v * 4
End Sub

Sub PutValue(v As Int) As Int
   rList.add(v)
   Return v
End Sub

Sub GetValue(v As Int) As Int
   Return rList.Get(v)
End Sub

Sub MinValue(a As Int, b As Int) As Int
   If a < b Then Return a Else Return b
End Sub

Sub MaxValue(a As Int, b As Int) As Int
   If a > b Then Return a Else Return b
End Sub

Sub RunData(GoValue As (e.g) Sub, Statement As Sub)
   '      
   '   Here i run the Data.   
   '
End Sub

Sub Init
   RunData(CalcOne, MinValue)
   RunData(PutValue, MaxValue)
End Sub

Thx
 

thedesolatesoul

Expert
Licensed User
Longtime User
A couple of ways around this.

If you want to use TYPEs then, in the type:

B4X:
Type MyType ( _
       Name As String,   _
       Items(10) As Int, _
       Init as String, _
       GetValue as String, _
       ...
       )

Then assign Init = "Init" (the name of the sub)
and wherever you need to call it just use CallSub(Main,MyType1.Init)
etc...

Another better way is just to use a Code module as an object definition.
But in that case you can only have 1 instance of the object, not multiple.
They way around that would be to create an object definition by a Type and pass that over to the code module's subs.
 

agraham

Expert
Licensed User
Longtime User
You can always try reflection :)
B4X:
Sub Test1S(msg As String)
   Msgbox(msg,"Test1")
End Sub

Sub Test2S(msg As String)
   Msgbox(msg,"Test2")
End Sub

Sub CallString(subname As String, param As String)
   Dim Obj1 As Reflector
   subname = "_" & subname.ToLowerCase 
   Obj1.RunStaticMethod("your.package.name.main", subname, Array As Object(param), Array As String("java.lang.String"))
End Sub

Sub Btn1_Click
   CallString("Test1S", "Calling")
   CallString("Test2S", "Calling")
End Sub
This can be extended to any Sub with any mix of parameter types
 

Kimmowich

Member
Licensed User
Longtime User
Thx for the great response ;)

I will have a go on both solutions.. just in case ;)

That said.

I did post this as a Wish.

It would be nice to either get TYPE inherited from other TYPE with subs-override or perhaps get less lucky and have the possibility of using
sub calls as descriebed in above posts (the easy way. hehe) :)

My question: Can we expect something like this in future updates?

thx
 

Troberg

Well-Known Member
Licensed User
Longtime User
Actually you can treat Types as Objects.
You can also pass sub names as strings and then use CallSub to dynamically call the sub.

Actually, he is on to something here. It would be rather neat if I could specify a variable/argument as "Method" or "Call" or something like that. then, when I use it, internally, but hidden from me, of course CallSub or Reflection is used.

The code would be clearer, and it would require less "under the hood-knowledge".
 
Status
Not open for further replies.
Top