Parametric polymorphism: Difference between revisions

Line 456:
END MODULE SORTSEARCH </lang>
 
There would be a function (with a unique name) for each of the contemplated variations in parameter types, and when the compiler reached an invocation of FIND(...) it would select by matching amongst the combinations that had been defined in the routines named in the INTERFACE statement. The various actual functions could have different code, and in this case, those for searching CHARACTER arrays differ, becauseonly the character<code>INTEGER*4 comparison operations differ from those for numbers. ThusTHIS,NUMB(1:*)</code> function FIND would appear toneed be a polymorphic functionchanged, butsay itto is<code>REAL*4 notTHIS,NUMB(1:*)</code> andfor indeedFINDF4, therewhich is actuallywhy noboth functionvariables calledare FIND anywherenamed in the compiledone codestatement. That saidHowever, somefor systemssearching hadCHARACTER polymorphic variablesarrays, such asbecause the B6700character wherebycomparison integersoperations werediffer representedfrom asthose floating-pointfor numbers (and, sono exactlythree-way theIF-test sameeither), functionadditional couldchanges beare presentedrequired. withThus, anfunction integerFIND orwould aappear floating-pointto variablebe (provideda thepolymorphic compilerfunction didn'tthat checkaccepts forand parameterreturns typea matchingvariety -of types, but thisit wasis routine)not, and itindeed, wouldthere workis - so long asactually no divisionsfunction werecalled involvedFIND sinceanywhere addition, subtraction, and multiplication arein the samecompiled forcode. both, but integer division discards any remainders.
 
That said, some systems had polymorphic variables, such as the B6700 whereby integers were represented as floating-point numbers and so exactly the same function could be presented with an integer or a floating-point variable (provided the compiler didn't check for parameter type matching - but this was routine) and it would work - so long as no divisions were involved since addition, subtraction, and multiplication are the same for both, but integer division discards any remainders.
More generally, using the same code for different types of variable can be problematical. A scheme that works in single precision may not work in double precision (or ''vice-versa'') or may not give corresponding levels of accuracy, or not converge at all, ''etc.'' While F90 also standardised special functions that give information about the precision of variables and the like, and in principle, a method could be coded that, guided by such information, would work for different precisions, this sort of scheme is beset by all manner of difficulties in problems more complex than the simple examples in text books. Polymorphism just exacerbates the difficulties, but sometimes it is not so troublesome, as in [[Pathological_floating_point_problems#The_Chaotic_Bank_Society]] whereby the special EPSILON(x) function that reports on the precision of a nominated variable of type ''x'' is used to determine the point beyond which further calculation (in that precision, for that formula) will make no difference.
 
More generally, using the same code for different types of variable can be problematical. A scheme that works in single precision may not work in double precision (or ''vice-versa'') or may not give corresponding levels of accuracy, or not converge at all, ''etc.'' While F90 also standardised special functions that give information about the precision of variables and the like, and in principle, a method could be coded that, guided by such information, would work for different precisions, this sort of scheme is beset by all manner of difficulties in problems more complex than the simple examples inof text books. Polymorphism just exacerbates the difficulties, but sometimes it is not so troublesome, as in [[Pathological_floating_point_problems#The_Chaotic_Bank_Society]] whereby the special EPSILON(x) function that reports on the precision of a nominated variable of type ''x'' is used to determine the point beyond which further calculation (in that precision, for that formula) will make no difference.
 
=={{header|Go}}==
1,220

edits