Knowledge

Ad hoc polymorphism

Source 📝

229:, to work with any kind of list. Using overloading, it is possible to have a function perform two completely different things based on the type of input passed to it; this is not possible with parametric polymorphism (but would have to be achieved with switching on the type inside the generic function). Another way to look at overloading is that a routine is uniquely identified not by its name, but by the combination of its name and the number, order and types of its parameters. 298:
polymorphism. Since Smalltalk has a late bound execution model, and since it provides objects the ability to handle messages that are not understood, it is possible to implement functionality using polymorphism without explicitly overloading a particular message. This may not be generally recommended
302:
Also, while in general terms common class method and constructor overloading is not considered polymorphism, there are more uniform languages in which classes are regular objects. In Smalltalk, for instance, classes are regular objects. In turn, this means messages sent to classes can be overloaded,
255:
An advantage that is sometimes gained from overloading is the appearance of specialization, e.g., a function with the same name can be implemented in multiple different ways, each optimized for the particular data types that it operates on. This can provide a convenient interface for code that needs
283:, the overloading is done at run time, as the methods ("function implementation") for each overloaded message ("overloaded function") are resolved when they are about to be executed. This happens at run time, after the program is compiled. Therefore, polymorphism is given by 155:
in which polymorphic functions can be applied to arguments of different types, because a polymorphic function can denote a number of distinct and potentially heterogeneous implementations depending on the type of argument(s) to which it is applied. When applied to
196:
mechanism: control moving through one named function is dispatched to various other functions without having to specify the exact function being called. Overloading allows multiple functions taking different types to be defined with the same name; the
303:
and it is also possible to create objects that behave like classes without their classes inheriting from the hierarchy of classes. These are effective techniques that can be used to take advantage of Smalltalk's powerful
180:, in which polymorphic functions are written without mention of any specific type, and can thus apply a single abstract implementation to any number of types in a transparent way. This classification was introduced by 256:
to be specialized to multiple situations for performance reasons. The downside is that the type system cannot guarantee the consistency of the different implementations.
225:
function would be called based on the type of lists being appended. This differs from parametric polymorphism, in which the function would need to be written
172:
in this context is not intended to be pejorative; it refers simply to the fact that this type of polymorphism is not a fundamental feature of the
362:
To handle these six function calls, four different pieces of code are needed (or three, if strings are considered to be lists of characters):
244:). Some languages that are not dynamically typed and lack ad hoc polymorphism (including type classes) have longer function names such as 505: 130: 441:. Overloading can therefore provide different meaning, or semantics, for an operation, as well as differing implementations. 252:, etc. This can be seen as advantage (more descriptive) or a disadvantage (overly verbose) depending on one's point of view. 510: 460: 312: 152: 22: 304: 486: 308: 233: 367: 123: 412: 210: 466: 202: 177: 52: 489:. Lecture notes for International Summer School in Computer Programming, Copenhagen, August 1967 205:
automatically ensures that the right function is called. This way, functions appending lists of
284: 264: 237: 144: 116: 515: 450: 418: 241: 181: 165: 161: 43: 38: 8: 299:
practice for everyday programming, but it can be quite useful when implementing proxies.
66: 410:
actually refers to three or four completely different functions. This is an example of
294:
A closer look will also reveal that Smalltalk provides a slightly different variety of
104: 99: 389: 193: 89: 84: 61: 157: 94: 399: 378: 374: 499: 392: 382: 279:
polymorphism can work out. Consider for example the Smalltalk language. In
260: 307:
capabilities. Similar arrangements are also possible in languages such as
214: 173: 455: 425:
Note the ambiguity in the string types used in the last case. Consider
259:
Since overloading is done at compile time, it is not a substitute for
280: 75: 275:
The previous section notwithstanding, there are other ways in which
287:
as in other languages, and it is also extended in functionality by
198: 206: 169: 16:
Applying polymorphic functions to arguments of different types
433:
assume addition rather than concatenation. They may expect
240:
to be overloaded in a manner similar to functions (see
497: 217:, and so on could be written, and all be called 487:Fundamental concepts in programming languages 124: 160:or procedural concepts, it is also known as 131: 117: 327:that may be used in the following ways: 232:This type of polymorphism is common in 498: 13: 14: 527: 187: 506:Polymorphism (computer science) 461:Polymorphism (computer science) 388:In the fourth and fifth cases, 377:addition must be invoked (with 373:In the second and third cases, 270: 236:languages, many of which allow 479: 429:in which the programmer might 402:concatenation must be invoked. 1: 472: 463:(other kinds of polymorphism) 7: 511:Programming language topics 444: 234:object-oriented programming 90:Single and dynamic dispatch 10: 532: 318: 291:polymorphism at run time. 370:addition must be invoked. 192:Ad hoc polymorphism is a 176:. This is in contrast to 357:"bab" + "oon" = "baboon" 467:Parametric polymorphism 178:parametric polymorphism 53:Parametric polymorphism 416:or more specifically, 337:3.14 + 0.0015 = 3.1415 285:subtyping polymorphism 265:subtyping polymorphism 385:, in the third case). 221:—and the right 145:programming languages 451:Operator overloading 419:operator overloading 323:Imagine an operator 242:operator overloading 182:Christopher Strachey 166:operator overloading 162:function overloading 44:Operator overloading 39:Function overloading 366:In the first case, 149:ad hoc polymorphism 67:Generic programming 30:Ad hoc polymorphism 398:In the last case, 105:Predicate dispatch 141: 140: 100:Multiple dispatch 523: 490: 483: 440: 436: 428: 409: 395:must be invoked. 358: 353: 348: 343: 338: 333: 326: 251: 247: 133: 126: 119: 85:Virtual function 62:Generic function 19: 18: 531: 530: 526: 525: 524: 522: 521: 520: 496: 495: 494: 493: 484: 480: 475: 447: 438: 434: 426: 407: 406:Thus, the name 356: 351: 346: 341: 336: 331: 324: 321: 273: 249: 245: 190: 158:object-oriented 137: 95:Double dispatch 17: 12: 11: 5: 529: 519: 518: 513: 508: 492: 491: 477: 476: 474: 471: 470: 469: 464: 458: 453: 446: 443: 404: 403: 396: 386: 379:type promotion 375:floating-point 371: 360: 359: 354: 349: 344: 339: 334: 320: 317: 272: 269: 189: 186: 139: 138: 136: 135: 128: 121: 113: 110: 109: 108: 107: 102: 97: 92: 87: 79: 78: 72: 71: 70: 69: 64: 56: 55: 49: 48: 47: 46: 41: 33: 32: 26: 25: 15: 9: 6: 4: 3: 2: 528: 517: 514: 512: 509: 507: 504: 503: 501: 488: 485:C. Strachey, 482: 478: 468: 465: 462: 459: 457: 454: 452: 449: 448: 442: 432: 427:"123" + "456" 423: 421: 420: 415: 414: 401: 397: 394: 393:concatenation 391: 387: 384: 383:type coercion 380: 376: 372: 369: 365: 364: 363: 355: 350: 345: 342:1 + 3.7 = 4.7 340: 335: 330: 329: 328: 316: 314: 310: 306: 300: 297: 292: 290: 286: 282: 278: 268: 266: 262: 257: 253: 243: 239: 235: 230: 228: 224: 220: 216: 212: 208: 204: 200: 195: 188:Early binding 185: 183: 179: 175: 171: 167: 163: 159: 154: 151:is a kind of 150: 146: 134: 129: 127: 122: 120: 115: 114: 112: 111: 106: 103: 101: 98: 96: 93: 91: 88: 86: 83: 82: 81: 80: 77: 74: 73: 68: 65: 63: 60: 59: 58: 57: 54: 51: 50: 45: 42: 40: 37: 36: 35: 34: 31: 28: 27: 24: 21: 20: 481: 430: 424: 417: 411: 405: 361: 322: 301: 295: 293: 288: 276: 274: 271:Late binding 263:as found in 261:late binding 258: 254: 250:print_string 231: 226: 222: 218: 215:real numbers 191: 153:polymorphism 148: 142: 29: 23:Polymorphism 516:Type theory 437:instead of 413:overloading 227:generically 213:, lists of 209:, lists of 203:interpreter 174:type system 168:. The term 500:Categories 473:References 456:Type class 305:reflection 431:naturally 332:1 + 2 = 3 281:Smalltalk 246:print_int 238:operators 184:in 1967. 76:Subtyping 445:See also 439:"123456" 313:Newspeak 207:integers 199:compiler 194:dispatch 368:integer 319:Example 211:strings 400:string 296:ad hoc 289:ad hoc 277:ad hoc 223:append 219:append 170:ad hoc 435:"579" 381:, or 352:+ = 347:+ = 390:list 311:and 309:Self 201:or 164:or 143:In 502:: 422:. 315:. 267:. 248:, 147:, 408:+ 325:+ 132:e 125:t 118:v

Index

Polymorphism
Ad hoc polymorphism
Function overloading
Operator overloading
Parametric polymorphism
Generic function
Generic programming
Subtyping
Virtual function
Single and dynamic dispatch
Double dispatch
Multiple dispatch
Predicate dispatch
v
t
e
programming languages
polymorphism
object-oriented
function overloading
operator overloading
ad hoc
type system
parametric polymorphism
Christopher Strachey
dispatch
compiler
interpreter
integers
strings

Text is available under the Creative Commons Attribution-ShareAlike License. Additional terms may apply.