Created
May 26, 2022 12:28
-
-
Save sudo-give-me-coffee/2df533f7eb131557c4d4f8c6ac35fe3d to your computer and use it in GitHub Desktop.
Emulando sobrecarga em lua
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function desambiguacao(map,args) | |
| local count = #args | |
| return map[#args](table.unpack(args)) | |
| end | |
| -- Os métodos ficam privados | |
| local function metodo_0_arg() | |
| print("método sem argumentos") | |
| end | |
| local function metodo_1_arg(a) | |
| print("método com 1 argumento:",a) | |
| end | |
| local function metodo_2_arg(a,b) | |
| print("método com 2 argumentos:",a,b) | |
| end | |
| local function metodo_3_arg(a,b,c) | |
| print("método com 3 argumentos:",a,b,c) | |
| end | |
| -- Só esse fica público | |
| function metodo(...) | |
| return desambiguacao({ | |
| [0]=metodo_0_arg, | |
| [1]=metodo_1_arg, | |
| [2]=metodo_2_arg, | |
| [3]=metodo_3_arg | |
| },{...}) | |
| end | |
| metodo() | |
| metodo("Oi") | |
| metodo("Olá","mundo") | |
| metodo("Esquisito","mas","funciona") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment