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)
- Can't add same element to cleared map HOT 2
- veb: memory leak HOT 1
- After clear(), a map allows double entries
- After calling clear() on a map several times, duplicate keys are allowed
- calling map.clear() fails to change the receiver map, in such a way that it has the exact same observable behaviour as doing `m = {}` for some maps HOT 4
- flag.FlagParser bug with certain combination of flags
- Binary crashes when compiled with tcc on OpenBSD HOT 5
- C compilation error when compiling simple Generic Interface test case
- cgen error for using a `default := p or { T{} }` construct in a generic function HOT 6
- type OneD = []Thing causes unexpected behaviour HOT 1
- FlagParser single letter options dependent on order of flags.
- C error when trying to use a &Struct value where Struct is required instead
- toml: Provide a way to set default values when decoding TOML
- C error when implementing two interfaces, one of them generic
- Show where bad union code is located HOT 2
- if ternary: failing to compile HOT 1
- vfmt removes selective import of interface when used only in implements clause HOT 3
- cgen error for `for mut ccc in aaa { match ccc {` where `aaa` is `[]string{}`
- tcc compiler not detected without path HOT 4
- random big.Integer function HOT 3
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.