diff --git a/README.md b/README.md index 79ce6cc..b77b9ae 100644 --- a/README.md +++ b/README.md @@ -194,7 +194,7 @@ $ f(a, b...) ~ f("discarded") ``` -Functions with the same name can be defined, as long as they have a different number of argument. +Functions with the same name can be defined, as long as they have a different number of argument. Functions will be selected based on the number of arguments given: ``` $ f(a, b) diff --git a/anselme.lua b/anselme.lua index 5783faf..28a9fd8 100644 --- a/anselme.lua +++ b/anselme.lua @@ -7,11 +7,11 @@ local anselme = { } package.loaded[...] = anselme --- TODO: for type checking functions: --- pour éliminer totalement les undefined --- ne type checker/compiler les fonctions que lorsqu'elles sont apppelées, en déterminant leur type à ce moment là selon les arguments donnés --- (du coup la surcharge ne se fera que selon l'arité. Mais ça sera 100% type checké) --- PB: les listes ont des types mixés (cf les varargs) + afficher des warnings dans le selecteur de variante lorsqu'un type de retour manque +-- TODO: improve type checking. +-- Right now, there is some basic type checking done at parsing - but since Lua and Anselme functions may not always define the type of +-- their parameters and return value, a lot of checks are skipped ("undefined argument" type). +-- Probably won't be able to remove them completely (since lists can have mixed types, etc.), but would be good to limit them. +-- Ideally, we'd avoid runtime type checking. -- load libs local preparse = require((...):gsub("anselme$", "parser.preparser")) diff --git a/stdlib/functions.lua b/stdlib/functions.lua index 786a0f4..f58b3a2 100644 --- a/stdlib/functions.lua +++ b/stdlib/functions.lua @@ -133,12 +133,14 @@ functions = { -- arithmetic ["+"] = { { - arity = 2, types = { "number", "number" }, return_type = "number", - value = function(a, b) return a + b end - }, - { - arity = 2, types = { "string", "string" }, return_type = "string", - value = function(a, b) return a .. b end + arity = 2, + value = function(a, b) + if type(a) == "string" then + return a .. b + else + return a + b + end + end } }, ["-"] = {