Hello guys, thanks for the response, 
After brainstorming for a while, I think we can implement this without 
changes in the language, and create static analysis tools for it,
What's wrong with my approach before is using the suffix "Pure" as a pure 
function marker, which is unreliable because the function name is not part 
of the function signature.
So to mark a function is pure, we need to embed the pure marker into the 
function signature, so it's impossible to lose this information,
then, the next observation is pure function must return something, there is 
no point of having a pure function that returns nothing.
With these facts and generic feature in the next golang, We can create a 
new type in a new package

// pure.go
package pure
type Value (type T) struct { Value T }

then to mark a function pure, we can use it like this:

func Sum(data []int) pure.Value(int) {
  total := 0
  for _, x := range data {
    total += x
  }
  return pure.Value{total}
}

the rest is the job for a static analysis tool to analyze that the purity 
contract is hold

This will also solve my problem about using a pure function in interface 
type as I state above

On Wednesday, July 8, 2020 at 3:37:07 PM UTC+7 jesper.lou...@gmail.com 
wrote:

> On Mon, Jul 6, 2020 at 6:46 PM <kurnia...@gmail.com> wrote:
>
>>
>> to my understanding, a pure function is a function that doesn't have a 
>> side effect, so we can limit pure function to:
>> - unable to call non-pure function
>> - unable to modify a variable that is not declared on current function 
>> (like a global variable)
>>
>>
> You also need an additional property, namely that the function always 
> returns the same output for a given input. That is, it is a "stable" 
> relation from inputs to outputs. Consider e.g.
>
> https://play.golang.org/p/XI4IgfiLoub
>
> where we read a global variable as part of the function body. You could 
> make this into an escaping variable in a closure if you want something more 
> local, but the global will serve the same purpose. There are many variants 
> of this scheme, for instance by reading on a channel as part of the 
> function body, and having that channel fed random values, etc.
>
> Marking an expression as pure is a nominal type of language construction. 
> Go usually prefers structural language constructions in most places, so I 
> don't think it fits into the language design as a whole.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/3afeb6b8-848c-491d-9d3d-1a89ca9d2e04n%40googlegroups.com.

Reply via email to