Feature request: B3D's Str function
BlitzMax Forums/BlitzMax Programming/Feature request: B3D's Str function
| ||
Hi, In B3D you could convert a type-instance to a string, using the Str function: ' B3D code: Type test Field SomeNum% Field SomeText$ End Type testa.test = New test testa\SomeNum = 5 testa\SomeText = "Hello" Print Str testa WaitKey() This would print: "[5,"Hello"]". This could be used to compare objects and remove objects that have identical values inside their fields. ' B3D code: Type test Field SomeNum% Field SomeText$ End Type testa.test = New test testb.test = New test testa\SomeNum = 5 testa\SomeText = "Hello" testb\SomeNum = 6 testb\SomeText = "Hello" Print Str testa Print Compare(testa, testb) WaitKey() Function Compare(o1.test, o2.test) o3$ = Str o1 o4$ = Str o2 If o3 = o4 Then Return True Else Return False End Function Right now we have to write our own Compare method and change it when there's a new field added to a type. |
| ||
Did you test the "ToString ()" method described under "language - objects"? |
| ||
Dreamora: The ToString method, by default, prints the the memory address of the object. PowerPC: If you want the rough equivalent of this, you're going to have to do it yourself. This means that you'll have to override the ToString method and have it return a string similar to what Str would have returned with previous Blitz versions. I can't believe I'm telling people to 'do it yourself' now -- sad, sad times. |
| ||
I already did this by overriding the Compare method, so I could compare two different objects (they may be of the same type, but they don't have to). If they're not the same type, then the method returned False. If it was the same type (or a derived type), it did compare the fields and returned True if every field had the same values in them. But when a field is added, I would have to change the Compare method too. The B3D Str function was perfect, as it did it automatically. It even could handle fields that were pointers to other objects. But I suppose there is no way to 'loop' through all fields of an object, is there? Then it would be possible to create a Str method, identical to B3D's Str function. |
| ||
i think Ginormous was indicating writing a new compare function that you can use in conjunction with overridden ToString methods... something like:Type test1 Field x:Int=0 Method ToString:String() Return "["+x+"]"; EndMethod EndType Type test2 Field x:Int=0 Field y:Int=1 Method ToString:String() Return "["+x+","+y+"]"; EndMethod EndType tester1:test1=New test1 tester2:test2=New test2 tester3:test1=New test1 DebugLog(tester1.ToString()) DebugLog(tester2.ToString()) DebugLog(tester3.ToString()) DebugLog(ObjStrComp(tester1,tester2)) DebugLog(ObjStrComp(tester1,tester3)) Function ObjStrComp:Int(obj1:Object,obj2:Object) Return (obj1.ToString()=obj2.ToString()) EndFunction |