In Io, you can set the execution context using do
:
Http := Object clone
Http get := method(uri, ("<GET request to " .. uri .. ">") println)
Http delete := method(uri, ("<DELETE request to " .. uri .. ">") println)
Database := Object clone
Database insert := method(table, data, ("<insert data to " .. table .. ">") println)
Database delete := method(table, id, ("<delete " .. id .. " from " .. table .. ">") println)
Http do(
get("http://example.com/")
delete("http://example.com/something")
)
Database do(
insert("cats", list("Phil", "gray"))
delete("cats", 12)
)
(Ruby has a similar feature with Object#instance_exec
, but its object model is a bit more complicated.)
In effect, this gives you a temporary namespace, which is nice for writing domain specific languages. Is there a technique to achieve a similar effect (a temporary namespace) in Haskell?
For example, something like: (Not necessarily exactly like this, but something with similarly succinct syntax.)
main = do
http $ do
get "http://example.com/"
delete "http://example.com/something"
database $ do
insert "cats" ["Phil", "gray"]
delete "cats" 12
Notice that the two delete
s are completely different functions. I'd prefer to avoid writing things like H.delete
and D.delete
, because that would get messy quick. I realize that this could be avoided by renaming that database version to, for example, deleteFrom
, but I don't want to.