Cyclic Generics

Community Forums/Monkey2 Talk/Cyclic Generics

Samah(Posted 2015) [#1]
Mark,
Is it possible to allow for cyclic generics? This would really help with chaining method calls with a factory/builder pattern.

Class Entity<T>
	Method One:T()
		Print "One"
		Return Self
	End
End

Class Enemy Extends Entity<Enemy>
	' Method One:Enemy() is inherited
	Method Two:Enemy()
		Print "Two"
		Return Self
	End
End

Class Player Extends Entity<Player>
	' Method One:Player() is inherited
	Method Three:Player()
		Print "Three"
		Return Self
	End
End

Then we could do something like:
New Enemy().One().Two()
New Player().One().Three()

Rather than the current ugly way:
Enemy(New Enemy().One()).Two()
Player(New Player().One()).Three()

Usually, One would have to return the base class, which means we would need a cast to continue using the methods in the subclasses.


Skn3(Posted 2015) [#2]
Yes please!


ImmutableOctet(SKNG)(Posted 2015) [#3]
There are usually better ways around having to do this, but it would be nice to have this for those corner cases. This would solve the problem I was having in this old thread, where it only really failed because the compiler was being paranoid.


Samah(Posted 2015) [#4]
I realise I have asked for this before, but I believe Mark said he has reimplemented generics in a different way for Monkey 2. Maybe it's possible now.


marksibly(Posted 2015) [#5]
Fixed in monkey2!


Samah(Posted 2015) [#6]
@marksibly: Fixed in monkey2!

I love you! <3


marksibly(Posted 2015) [#7]
Eek!