How to extend any class
Posted
#1
(In Topic #674)
Regular

After trying several methods to extend some classes like 'String' or 'File' I found a simple way to do it.
I'm going to do the example adding the function 'Words' to the class 'String'.
The first step is to create a class called 'String' in the current project, that is String.class, then inside this file:
Regards
Posted
Guru

Might as well name the class StringF.class or something, not much difference between typing String.Words or StringF.Words and will avoid conflicts.
PS. here's your function shortened to 2 lines.
Posted
Regular

I was thinking the same thing, but so far there are no conflicts, I guess because I don't overwrite existing methods.BruceSteers said
I do not think it is a good idea to name classes with the same name as already existing internal classes.
It's an experiment to tune some functions that I would like to add to the real String, maybe someday.
Great, very good code and more efficient. ThanksPS. here's your function shortened to 2 lines.:roll:
<IMG src="https://imgur.com/yndQmyi.png">
</IMG>Regards
Posted
Guru

tincho said
I was thinking the same thing, but so far there are no conflicts, I guess because I don't overwrite existing methods.BruceSteers said
I do not think it is a good idea to name classes with the same name as already existing internal classes.
It's an experiment to tune some functions that I would like to add to the real String, maybe someday.
Great, very good code and more efficient. ThanksPS. here's your function shortened to 2 lines.:roll:
<IMG src="https://imgur.com/yndQmyi.png"></IMG>
Regards
oops i forgot to Trim() may not be as fast now.
I guess if you have no problems then go for it
Posted
Regular

Still faster that the original function: 13.617 vs 7.294oops i forgot to Trim() may not be as fast now.
Hehe, that's true, although problems usually come without you calling them. Problem solving is what brought us to where we are, (I mean humanity), but it can be interpolated for programming as well.I guess if you have no problems then go for it
Posted
Guru

tincho said
Still faster that the original function: 13.617 vs 7.294oops i forgot to Trim() may not be as fast now.
Hehe, that's true, although problems usually come without you calling them. Problem solving is what brought us to where we are, (I mean humanity), but it can be interpolated for programming as well.I guess if you have no problems then go for it
Yep lol , I think humanity needs much more bugfix than antivirus to be honest.
If you compile gambas from source (using git to pull) you can create your own branch and then add your code to String.class in gb.util
Hmm actually it looks like gb.util just does the same as you do by making a String.class so i guess your way will be sound unless you use gb.util
Posted
Regular

Its correct, indeed my two targets are File.class & String.Class both inside gb.utilBruceSteers said
Hmm actually it looks like gb.util just does the same as you do by making a String.class so i guess your way will be sound unless you use gb.util
But i test in a project (tradukisto) at the same time gb.util and my classes File.class & File.class everything works perfectly.
1 guest and 0 members have just viewed this.


