Comments (24)
We cannot have result + option one. It must be a checker error there.
from v.
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.
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.
-
You need to update your V. What you have is quite a few commits behind.
-
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.
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.
but why should it give an error...
from v.
Because they are 2 distinct types. It would be like trying to return a string-int
. There is no such thing.
from v.
yes, but i can have a result containing a string, an int, whatever. why not a result containing an option?
from v.
so there is no way of expressing what i want in V?
from v.
No, not the way you want. However, if we knew exactly what you want, perhaps a different way could be suggested.
from v.
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.
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.
But I only have one field? Do I really have to make a struct..?
from v.
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.
The API returns what user is logged on a computer, so yes
from v.
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.
whats the point of having an option type we can do error('none') then?
from v.
That is 'none'
as a string, not none
the type.
from v.
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.
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.
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.
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.
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.
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)
- Storing a function result value in a variable does not work as expected in an `or` block
- A struct with an option field of type interface doesn't accept none as value
- cgen error for `x := 5 * p` where `p` is &int HOT 2
- [info] and incase helps others (is now working) HOT 2
- vfmt erasing lines of code following `// vfmt off` HOT 2
- C error when compiling code
- compiling with clang sanitizer signals undefined behavior in rand module HOT 4
- zed supports v language HOT 1
- Undefined bitfield operation on enum alias HOT 1
- seems error happend when trying to access a folder under Documents macos HOT 1
- v test does not print float value HOT 2
- `v.reflection` `attrs` rework HOT 2
- segmentation fault when trying to use an array return type for a pool_processor callback
- calculated const causes failure when used as fixed array size HOT 3
- Error C (from H file to V : warning: implicit declaration of function 'sha3_finalize' /tmp/v_1000/SHA3.01HYG2REZ8HW9F433E3J5B37XR.tmp.c:13117: error: '{' expected (got ";")
- examples/sokol/02_cube_glsl VALIDATE_ABND_FS_EXPECTED_SAMPLER_BINDING
- gx - operator overloading not working for Color
- [bug]V run ERROR
- V compiler error when adding error processing using or {<expression>} HOT 1
- Is the data of an option reference returned from a method changed?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from v.