Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: error check: proposal add arrow function to let error check more simple #69482

Closed
simpleKV opened this issue Sep 15, 2024 · 2 comments
Labels
Milestone

Comments

@simpleKV
Copy link

simpleKV commented Sep 15, 2024

Proposal Details

i think we discussed arrow function previous, and discussed error check again and again
but why we are still discussing it, maybe we didn't get the sweet point
hope make a little progress

below is common code, just do somethings, f1, f2, f3( here makePeople)...
OCZ, we need check every step's err result to progress next

i add a func Dos to collect err check, it works(sample1),

but i find if Go support arrow function, we can make it more simple(sample2)

package main

import (
	"fmt"
)

type people struct {
	age  int
	name string
}

var limit int

func f1() error {
	//blabla for init something
	if limit > 1000 {
		return fmt.Errorf("reach limit")
	}
	return nil
}

func f2(age int) (int, error) {
	if age < 0 {
		return 0, fmt.Errorf("invalid age")
	}
	return age, nil
}

func makePeople(name string, age int) (*people, error) {
	if len(name) == 0 {
		return nil, fmt.Errorf("invalid name")
	}

	return &people{age, name}, nil
}

type Action func() error

func Dos(actions []Action) error {
	for i := range actions {
		err := actions[i]()
		if err != nil {
			return err
		}
	}
	return nil
}

func sample1(ageIn int) {
	var (
		err error
		age int     //middle result
		pp  *people //final result
	)

	err = Dos(
		[]Action{
			func() error {         //here is a little boring, can we make it more simple ?
				return f1()
			},
			func() error {         //can we omit the word func and error,  to deduce it ?
				age, err = f2(ageIn)
				return err
			},
			func() error {
				pp, err = makePeople("Alice", age)
				return err
			},
		})

	if err == nil {
		fmt.Println("here we got people:", *pp)
	} else {
		fmt.Println("we failed:", err)
	}
}

/*
//currently, Go does support ()=>
//
func sample2(ageIn int) {
	var (
		err error
		age int     //middle result
		pp  *people //final result
	)

	err = Dos([]Action{
		()=> f1(),               //f1 match Action
		()=> {
			age, err = f2(ageIn) //here we save age, return err only to match Action
			return err
		},
		()=> {
			pp, err = makePeople("Alice", age)  //same with above
			return err
		},
	})

	if err == nil {
		fmt.Println("here we got people:", *pp)
	} else {
		fmt.Println("we failed:", err)
	}
}
*/

func main() {
	sample1(20)

	sample1(-1)

	limit = 1005
	sample1(20)
}
@gopherbot gopherbot added this to the Proposal milestone Sep 15, 2024
@seankhliao
Copy link
Member

Duplicate of #21498

@seankhliao seankhliao marked this as a duplicate of #21498 Sep 15, 2024
@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Sep 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants