Function overloading allows one to define multiple functions under the same name, taking different arguments. The task below shows the requisite code for overloading:
Python |
Qt/Cpp |
- # Special thanks from jinsuun__ on #python irc
- from dataclasses import dataclass
- @dataclass
- class C:
- m_c: int
- isLetter : bool = False
- isNonCharacter : bool = False
- isNull : bool = False
- isNumber : bool = False
- isPrintable : bool = True
- isSpace : bool = False
- isUpper : bool = False
- n = 88
- c = C(n)
- match c:
- case C(0):
- c.isNull = True
- case C( m_c = x ) if x in range( 1, 32 ) or x == 127:
- c.isNonCharacter = True
- c.isPrintable = False
- case C(" "):
- c.isSpace = True
- case C(x) if x in range( ord("A"), ord("Z") + 1 ):
- c.isUpper = True
- c.isLetter = True
- case C(x) if x in range( ord("a"), ord("z") + 1 ):
- c.isUpper = False
- c.isLetter = True
- case C(x) if x in range( ord("0"), ord("9") + 1 ):
- c.isNumber = True
- case _:
- pass
|
- qint8 c(88);
- bool isLetter (false);
- bool isNonCharacter(false);
- bool isNull (false);
- bool isNumber (false);
- bool isPrintable (true );
- bool isSpace (false);
- bool isUpper (false);
- switch (c) {
- default: break;
- case 0 :
- isNull = true;
- [[fallthrough]];
- case 1 ... 31:
- case 127 : // Delete
- isNonCharacter = true;
- isPrintable = false;
- break;
- case ' ':
- isSpace = true;
- break;
- case 'A' ... 'Z':
- isUpper = true;
- [[fallthrough]];
- case 'a' ... 'z':
- isLetter = true;
- break;
- case '0' ... '9':
- isNumber = true;
- break;
- }
|
Read as:
|
- Define a debug function with two arguments
- [ whether it returns anything is unknown ]
- T & TT [ Shorthand for Type ]
- which implies a generic all purpose argument
- [ whether this function alters the arguments is unknown ]
- in which TT equals None [ Undefined ] by default.
- After several complex conditional statements
- [ return statements may be hidden in the definitions ]
- if all else fails then the system exits with the message
- 'Fatal: No Programming'.
|
- Four prototypes of a void type debug function exist
- [ They are probably defined elsewhere ]
- whos parameter lists are combinations of
- [ constant [ read-only ] references to ]
- Foo and Bar structures.
- These functions are probably for debugging Foo and Bar.
|
Benefits:
|
- Everything is defined under one function, which some people may like because all the definitions are close together.
- The syntax ' if T is Foo and TT is None ' is very straight forward and easy to understand.
- You can be a lot more flexible with the default behaviour, given the dynamic nature of the language.
|
- Requisite information [ gleamed from the function signature ] is provided with less lines of code
- Decoupled, and easy to extend.
- Logic for determining which definition to use, is handled by compiler.
|
Drawbacks:
|
- Every definition has to exist in an extra indentation.
- Have to sift through the definitions to read then write your relevant code.
- The complexity of the conditional statements is relative to the amount of parameters in the function.
- Its not obvious that this is intended for function overloading, even after glancing at the source code.
- Bugs are very easy to create in the conditional statements. For example, a person may define some code, but mess up the conditional, and it may never fire when expected.
- ' TT = None ' is kind of an odd red herring.
|
- Difficult to create a dynamic solution using this particular idiom.
|