rooch-network / rooch

VApp Container with Move Language
https://rooch.network
Apache License 2.0
128 stars 54 forks source link

[CodeStyle] The `Context` should be first or last argument #922

Closed jolestar closed 6 months ago

jolestar commented 7 months ago

The Context should be the first or last argument? This is just a code-style convention.

public entry fun my_entry(ctx: &mut Context, a: u64, b: string::String, c: vector<u8>){
} 

Or

public entry fun my_entry(a: u64, b: string::String, c: vector<u8>, ctx: &mut Context){
} 

The TxContext in sui is the last argument.

https://github.com/MystenLabs/sui/blob/1292544b58bb8558063a44992e79ba952c2a6d37/sui_programmability/examples/defi/sources/pool.move#L129-L138

Part of #921

baichuan3 commented 7 months ago

I prefer Context as first argument

stevenlaw123 commented 7 months ago

I prefer Context as first argument

I agree with too. In this way, the verifier can easily determine whether the first function is of type Context, but it becomes a bit more complicated if it is the last one.

stevenlaw123 commented 7 months ago

For example, in C++ class functions, a this pointer is implicitly added during compilation, and this this pointer becomes the first parameter of the function.

yubing744 commented 7 months ago

prefer first

jolestar commented 7 months ago

I think the main reason Sui uses TxContext as the last argument is that this argument is a system argument and the user doesn't have to care about it.

For example, when passing command line arguments, the user needs to skip the first Context argument, and then do the count.

If we make Context the last argument, the user doesn't need to skip it.

But in rooch, we support both signer and Context as system arguments.

So, the example should look like this:

public entry fun my_entry(ctx: &mut Context, signer: &signer, a: u64, b: string::String, c: vector<u8>, ){
} 

vs

public entry fun my_entry(a: u64, b: string::String, c: vector<u8>, ctx: &mut Context, signer: &signer){
} 
jolestar commented 7 months ago

I prefer Context as first argument

I agree with too. In this way, the verifier can easily determine whether the first function is of type Context, but it becomes a bit more complicated if it is the last one.

This is only about the code-style convention, not about the verifier. Currently, the verifier and MoveosVM support Context in any position.

stevenlaw123 commented 7 months ago

I prefer Context as first argument

I agree with too. In this way, the verifier can easily determine whether the first function is of type Context, but it becomes a bit more complicated if it is the last one.

This is only about the code-style convention, not about the verifier. Currently, the verifier and MoveosVM support Context in any position.

During compilation, check if the first parameter of the gas_free function is Context. If the Context parameter can be supported at any position in the function parameters, then the rust code needs to be slightly modified. it's a small problem.

jolestar commented 7 months ago

This is only about the code-style convention, not about the verifier. Currently, the verifier and MoveosVM support Context in any position.

During compilation, check if the first parameter of the gas_free function is Context. If the Context parameter can be supported at any position in the function parameters, then the rust code needs to be slightly modified. it's a small problem.

The gas_free validate function is like implementing an interface. The implementation should follow the interface function's signature.

WGB5445 commented 7 months ago

I prefer the parameters at the end, because the first thing a function needs to pay attention to is the ordinary parameters. You don't need to worry about whether there is a context at all times. If you need it, you will add it.

jolestar commented 6 months ago

Most people favor Context as the first argument, so follow that convention.

And 10xhunter also contributes a view. Go also puts Context as the first argument (https://go.dev/blog/context-and-structs). As the first argument, it can support variable-length arguments in the future (https://gobyexample.com/variadic-functions).