Where clause #465

Closed
opened 2023-06-29 13:29:05 +09:00 by zxq9 · 5 comments
zxq9 commented 2023-06-29 13:29:05 +09:00 (Migrated from gitlab.com)

Created by: subhod-i

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] })
*Created by: subhod-i* 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. ```js 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] }) ```
zxq9 commented 2023-07-01 01:31:32 +09:00 (Migrated from gitlab.com)

Created by: radrow

I don't like it for two reasons:

  • it doesn't offer anything that let doesn't
  • you have to read code bottom top in order to understand what is going on

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

*Created by: radrow* I don't like it for two reasons: - it doesn't offer anything that `let` doesn't - you have to read code bottom top in order to understand what is going on Note that this is not Haskell; evaluation order does matter, and so this could lead to ambiguities.
zxq9 commented 2023-07-01 02:08:51 +09:00 (Migrated from gitlab.com)

Created by: subhod-i

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

*Created by: subhod-i* 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
zxq9 commented 2023-07-01 04:27:22 +09:00 (Migrated from gitlab.com)

Created by: radrow

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.

*Created by: radrow* > 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`.
zxq9 commented 2023-07-01 04:31:34 +09:00 (Migrated from gitlab.com)

Created by: radrow

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.

*Created by: radrow* 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.
zxq9 commented 2023-07-03 13:44:01 +09:00 (Migrated from gitlab.com)

Created by: subhod-i

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

*Created by: subhod-i* I see your point. `let` might work in this case. Looking forward to the `namespace` feature. Feel free to close this issue!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: QPQ-AG/sophia#465