aeternity / aesophia

Stand alone compiler for the Sophia smart contract language
https://docs.aeternity.com/aesophia
ISC License
51 stars 19 forks source link

Where clause #465

Closed subhod-i closed 1 year ago

subhod-i commented 1 year ago

The Where clause is self-explanatory in the functional programming paradigm. In Sophia, writing independent functions which are used only once in a method will improve the readability of the smart contract.

  stateful entrypoint withdraw(amount : int) =
      if(Call.caller == state.beneficiary)
        withdraw_beneficiary()
      elif(is_contributor(Call.caller))
        withdraw_contributor()
      else
        abort("Not a contributor or beneficiary")
    where
        withdraw_beneficiary 0 = abort("Invalid amount")
        withdraw_beneficiary amount = spend({recipient = state.beneficiary,  amount })
        withdraw_contributor = spend({recipient = Call.caller,  amount    = state.contributions[to] })
radrow commented 1 year ago

I don't like it for two reasons:

Note that this is not Haskell; evaluation order does matter, and so this could lead to ambiguities.

subhod-i commented 1 year ago

Can the let keyword be used to assign a function to a variable in Sophia?

you have to read code bottom top in order to understand what is going on

This may not be a problem. You know the function you are calling is not somewhere in the smart contract but rather in the where clause that is attached to this caller function body.

Note that this is not Haskell; evaluation order does matter, and so this could lead to ambiguities.

Agreed

radrow commented 1 year ago

Can the let keyword be used to assign a function to a variable in Sophia?

let cannot do it at the moment, but this is rather a missing feature. I would rather let let introduce functions than implement where. We are actually considering fixing that.

You know the function you are calling is not somewhere in the smart contract but rather in the where clause that is attached to this caller function body.

I have seen many abuses of it in Haskell and Purescript. In those languages this is especially confusing when people mix it with let.

radrow commented 1 year ago

We could possibly consider limitations to how where may be used, eg only on toplevel functions, only non-stateful functions, no nesting, etc. But that would complicate the language, while being even less useful. Also, if you need so many auxilary functions that let is unreadable, then maybe you should create a namespace? We are planning to introduce nested namespaces in the upcoming future, so that would be my way to go.

subhod-i commented 1 year ago

I see your point. let might work in this case. Looking forward to the namespace feature. Feel free to close this issue!