Coder Social home page Coder Social logo

option inside a result about v HOT 24 CLOSED

demostanis avatar demostanis commented on May 25, 2024
option inside a result

from v.

Comments (24)

felipensp avatar felipensp commented on May 25, 2024 2

We cannot have result + option one. It must be a checker error there.

from v.

Delta456 avatar Delta456 commented on May 25, 2024 1

We cannot have result + option one. It must be a checker error there.

It gives an error with the latest V

file.v:3:11: error: the fn returns type `!?string`, but type `!string` is a Result alias, you can not mix them
    1 | import net.http
    2 | 
    3 | fn myfn() !(?string) {
      |           ~~~~~~~~~~
    4 |      url := ''
    5 |
file.v:3:11: error: the fn returns type `!?string`, but type `?string` is an Option alias, you can not mix them
    1 | import net.http
    2 | 
    3 | fn myfn() !(?string) {
      |           ~~~~~~~~~~
    4 |      url := ''
    5 |
file.v:7:56: error: undefined ident: `ip`
    5 |  
    6 |      mut req := http.Request{url: url}
    7 |      req.add_header(http.CommonHeader.x_forwarded_for, ip)
      |                                                        ~~
    8 |  
    9 |      res := req.do()!
file.v:7:56: error: `ip` (no value) used as value in argument 2 to `net.http.Request.add_header`
    5 |  
    6 |      mut req := http.Request{url: url}
    7 |      req.add_header(http.CommonHeader.x_forwarded_for, ip)
      |                                                        ~~
    8 |  
    9 |      res := req.do()!
    ```

from v.

JalonSolov avatar JalonSolov commented on May 25, 2024 1

Because a result either returns a concrete value or an error. An option is not a concrete value because it can also be one of 2 things... a valid value, or none.

from v.

JalonSolov avatar JalonSolov commented on May 25, 2024
  1. You need to update your V. What you have is quite a few commits behind.

  2. Please post a complete example. It's hard to guess exactly what you have that is showing the error. It doesn't have to be your whole project, just something complete enough to show the problem.

from v.

demostanis avatar demostanis commented on May 25, 2024

i cannot reproduce on the latest version of v
however, why cant I mix result + option? i have a function that does an http request, returning whether an optional string inside the body. i want to return errors and that optional string. what should i do instead?

from v.

demostanis avatar demostanis commented on May 25, 2024

but why should it give an error...

from v.

JalonSolov avatar JalonSolov commented on May 25, 2024

Because they are 2 distinct types. It would be like trying to return a string-int. There is no such thing.

from v.

demostanis avatar demostanis commented on May 25, 2024

yes, but i can have a result containing a string, an int, whatever. why not a result containing an option?

from v.

demostanis avatar demostanis commented on May 25, 2024

so there is no way of expressing what i want in V?

from v.

JalonSolov avatar JalonSolov commented on May 25, 2024

No, not the way you want. However, if we knew exactly what you want, perhaps a different way could be suggested.

from v.

demostanis avatar demostanis commented on May 25, 2024

I have to fetch an API returning JSON. The response might contain a user. I want to return an optional user, while handling any error in the calling function.

from v.

JalonSolov avatar JalonSolov commented on May 25, 2024

The simplest thing there would be to decode the JSON to a struct with an optional field.

    user ?string // or whatever...

By making the field optional, it will either have the user decoded from the JSON, or it will be none if it wasn't passed back.

Then return !<struct name> instead of a simple string.

from v.

demostanis avatar demostanis commented on May 25, 2024

But I only have one field? Do I really have to make a struct..?

from v.

JalonSolov avatar JalonSolov commented on May 25, 2024

That's the only way I can think of to get what you want.

You really will only either get a single string with a user name, or nothing at all? That sounds bizarre...

from v.

demostanis avatar demostanis commented on May 25, 2024

The API returns what user is logged on a computer, so yes

from v.

JalonSolov avatar JalonSolov commented on May 25, 2024

Then it should be even simpler. Don't bother with a struct, or JSON decoding, or anything else. Check to see what is returned. If it is "" (or however it returns something that doesn't exist), then return error('none') and check for that in the or block in the caller.

from v.

demostanis avatar demostanis commented on May 25, 2024

whats the point of having an option type we can do error('none') then?

from v.

JalonSolov avatar JalonSolov commented on May 25, 2024

That is 'none' as a string, not none the type.

from v.

JalonSolov avatar JalonSolov commented on May 25, 2024

It doesn't have to be error('none') - it should be whatever makes more sense to you. Other examples:

error('no user logged in')
error('no such user')
error('n/a')
error('say what??')
error("didn't get nuttin'")
...

from v.

demostanis avatar demostanis commented on May 25, 2024

yes, but it's not an error, it's an optional value..
what i meant is why does V have an option type if an error can be used instead?

from v.

JalonSolov avatar JalonSolov commented on May 25, 2024

Because none is not always an error. It might be completely expected. For example, if you ask for an array of things, and there aren't any, instead of getting back an empty array and checking for that, you can just do an Option return and the or will catch the none.

from v.

demostanis avatar demostanis commented on May 25, 2024

well in my code having no user is completly expected, yet i have to use a result.
maybe options inside results should be supported?

from v.

JalonSolov avatar JalonSolov commented on May 25, 2024

Option/Result used to be one combined thing.

Then a lot of people complained about having to do extra checks in the or block to decide if the return was an error or none. So it was split into 2 separate types.

This is what we have now, and it is unlikely to go back to what it was before.

from v.

demostanis avatar demostanis commented on May 25, 2024

im not saying results should be options, im saying V should support results inside options or options inside results, just like any other normal type, like a Result<Option> in Rust

from v.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.