tooltiperror
Super Moderator
- Reaction score
- 232
Just wondering, why should we use interfaces? I don't get their value.
library testLib
//method interface example
interface Comparable
method operator > takes Comparable c returns boolean defaults false
endinterface
struct String extends Comparable
string which
method operator > takes Comparable c returns boolean
return StringLength(this.which) > StringLength(String(c).which) //can't quite remember how to check if c is of type String
endmethod
endstruct
struct Character extends Comparable
method operator > takes Comparable c returns boolean
return LoadInteger(someHashtable,someKey_Character,StringHash(this.which)) > LoadInteger(someHashtable,someKey_Character,StringHash(Character(c).which))
endmethod
endstruct
//function interface example - imagine you don't have the operators being overloaded, they are simply all extending a common object.
function interface Comparator takes Comparable a, Comparable b returns boolean
function compareGreaterS2C takes Comparable str, Comparable char returns boolean
return LoadInteger(someHashtable,someKey_Character,StringHash(SubString(str.which,0,1))) > LoadInteger(someHashtable,someKey_Character,StringHash(Character(char).which))
endfunction
endlibrary
library anotherTestLib uses testLib
//test function interface
function needCompare takes Comparable a, Comparable b, Comparator comp returns nothing
if comp.evaluate(a,b) then
call BJDebugMsg("a is greater than b")
elseif comp.evaluate(b,a) then // some sort of typecasting should be done first
call BJDebugMsg("a is less than b")
else
call BJDebugMsg("a is equal to b")
endif
endfunction
call needCompare(String.create("Not Dewey Decimal"),Character.create("F"),Comparator.compareGreaterS2C)
// test method interface
function compare takes Comparable a, Comparable b returns nothing
if a>b then
call BJDebugMsg("a is greater than b")
elseif a<b then
call BJDebugMsg("a is less than b")
else
call BJDebugMsg("a is equal to b")
endif
endfunction
call compare(String.create("Also Not"),String.create("Dewey Decimal, as this is just an example"))
endlibrary