as-bignum icon indicating copy to clipboard operation
as-bignum copied to clipboard

What does the safe version refer to here

Open yjhmelody opened this issue 3 years ago • 4 comments

What does the safe version refer to here? Is it similar to checked_op in rust? It seems that it is more convenient to introduce an Option, and in this case, it seems that new types should not be defined

yjhmelody avatar May 18 '21 09:05 yjhmelody

Is it similar to checked_op in rust?

Yes, but instead return option we just throw abort.

It seems that it is more convenient to introduce an Option, and in this case, it seems that new types should not be defined

No, monadic option is good for functional programming when null safety doesn't support on language level. In AssemblyScript we follow typescript paradigm with nullish types.

We need another type due to overflow checking required extra runtime overhead which not always needed.

MaxGraey avatar May 18 '21 10:05 MaxGraey

We need another type due to overflow checking required extra runtime overhead which not always needed.

I mean providing the checked_op operator instead of providing a new type and overloading it. Isn’t it troublesome to convert between two types sometimes? The function name also distinguishes the difference in operation, just like in rust

yjhmelody avatar May 18 '21 10:05 yjhmelody

I mean providing the checked_op operator instead of providing a new type and overloading it.

I see. I'm not fun of method based safe operations. Some applications like smart contacts required always and only safe operations which will looks very verbose in this case

The function name also distinguishes the difference in operation, just like in rust

I don't think Rust's approach is good for our case. Rust is low level language which also highly based on trait system. TypeScript / AssemblyScript follow another paradigm.

I don't really understand the general imitation of rust, which actually mimics Ocaml, which in general is a functional language

MaxGraey avatar May 18 '21 10:05 MaxGraey

Well, in the blockchain or smart contract, I personally think that we should not throw abort directly, but assert every place that needs to be checked, so that we can easily find the wrong place after an abnormal exit or can return an error type to describe error type. This is just a set of chained calls in rust.

And you are right. Many times we don't want to have redundant checks. For example, a type only supports operations with checks. If we don't want to check here (because there is no error in logic), then type conversion is needed here. At least I see in the rust code that many arithmetic operations with checks are interspersed with ordinary operators without checks. Obviously in this case, the safe/unsafe type conversion is more verbose.

I have seen some cases where the maximum or minimum value is returned when overflow (this is really common in blockchain projects, and saturating_op is used in rust). If the function exits directly here, we cannot implement this function based on this API.

Well, it may be more important to have a safe API first, but I reserve my opinion.

yjhmelody avatar May 18 '21 10:05 yjhmelody