Skip to content

No way to return error without knowing value type (and vice versa) #28

@arnetheduck

Description

@arnetheduck

When creating a Result via err(x), the type of the value is not relevant until later - later being when the result is actually assigned to a variable of the Result type at which point the statically known type becomes dynamic.

It would be useful to be able to represent this "in-between" error value as being an error otherwise we always must know the full type at all times.

This becomes relevant in err itself that currently relies on type deduction of the result variable, a trick that only works in limited contexts.

A good example of this problem is or which cannot be combined with the self-deducing constructors:

proc f(): Result[float, int] =
  let v = Result[int, int].ok(42)
  # err deduces the "current" type to be `Result[int, int]` based on the 
  # `result` variable rather than on the context in which it's being evaluated
  # (in this case `or` with the `v` variable: the result of this operation should
  # be a `Result[int, int]`)
  let x = v or err(42)

Some ideas for how this could be solved:

  • err would use something else than result as a type source - how to inject that though?
  • err would return a magic type that converts to Result at the latest possible moment, automatically deducing T - since we know it's an error at this point, context can decide what T should be.
  • other solutions?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions