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

Simplify Result(T) #4

Open
tdakkota opened this issue Jun 30, 2020 · 2 comments
Open

Simplify Result(T) #4

tdakkota opened this issue Jun 30, 2020 · 2 comments

Comments

@tdakkota
Copy link
Contributor

So, if Result(T) is interface, why not just create two

type ok(type T) T
// Implement Result(T)

type err(type T) error
// Implement Result(T)

types implementing Result(T)?

  • More similar to Rust's Result(T) (you can add type ok(T), err(T) constraint to prohibit any other implementations)
  • Ok's size is equal to T and Err's size is equal to error
@kovetskiy
Copy link
Member

That's an interesting idea, but it feels like I don't really get your idea, what can we achieve by having these two types implementing Result(T)?

it would be great to achieve pattern matching at some point.

I think we could have a method that returns either Ok(T) or Err(T), so we would be able to use it in type-switch:

    switch v := result.SomeMethod().(type) {
    case Ok(int):
        //
    case Err(int):
        ///
    }

But it still doesn't look really useful.

The Match() function already addresses this problem, but it requires passing two functions though.

@tdakkota
Copy link
Contributor Author

tdakkota commented Jul 1, 2020

  • It reduces Result(T) size, e.g. Result(VeryBigStruct)'s err case contains only error
  • Case resolves by type, you does not need to check it manually.
func (o ok(T)) Or(res Result(T)) Result(T) {
	return o
}

func (e err(T)) Or(res Result(T)) Result(T) {
	return res
}

instead of

func (result *result(T)) Or(res Result(T)) Result(T) {
	if result.ok {
		return result
	} else {
		return res
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants