-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Closed
Labels
T-langRelevant to the language team, which will review and decide on the RFC.Relevant to the language team, which will review and decide on the RFC.
Description
With visibility of function parameters, possibly with default return values, to achive functionality of Delphi's function "Result" auto variable or as shown in the below "Go" code:
func OpenPrinter(name string) (success bool, printer HANDLE) {
openPrinter := winspool.NewProc("OpenPrinterA")
Cname := unsafe.Pointer(C.CString(strToAnsi(name)))
defer C.free(Cname)
Cret, _, _ := openPrinter.Call(uintptr(Cname), uintptr(unsafe.Pointer(&printer)), uintptr(0 /Cnil/))
success = (int(Cret) != 0)
return
}
Note the usage of return varibales "success" & "printer" within the function body. Very handy & clear.
The current "Rust" code for this function is as follows :
fn open_printer(name: String) -> (bool, HANDLE) {
let h: HANDLE = ptr::null_mut();
unsafe {
(OpenPrinterW(str2wcstr(&*name).as_ptr(), &h, std::ptr::null_mut()) == TRUE, h)
}
}
RickMortynson and Divide-By-0fstirlitz, liigo, safinsingh and jeffsEndlessCheng
Metadata
Metadata
Assignees
Labels
T-langRelevant to the language team, which will review and decide on the RFC.Relevant to the language team, which will review and decide on the RFC.
Type
Projects
Milestone
Relationships
Development
Select code repository
Activity
mark-i-m commentedon May 11, 2017
I don't have strong feelings, but it feels "unrustic" to return a value by assigning to named return variable. Also, this might just be me, but I find the rust code you posted much more readable than the Go code you posted...
SimonSapin commentedon May 11, 2017
This sounds like "structural records". Rust used to have them, and removed them in 0.6:
Adding them back was proposed in https://internals.rust-lang.org/t/pre-rfc-unnamed-struct-types/3872 . As far as I can tell this proposal was not explicitly rejected, the conversation just died out. It may be worth writing a formal RFC pull request.
burdges commentedon May 11, 2017
You could rephrase this as asking to place a pattern in return position and assign to it. And you might as well ask for patterns in argument position while you're at it. I donno if either really buys much, but it might look like
or maybe you could omit the
mut
s in return position so long as you only assigned to each once.IvankoB commentedon May 11, 2017
One more advantage of this approach is that no need explicitly to maintain stack variables to return within function - those (if declared as proposed) are automatically created & managed on function calling & returning. Who programmed in Delphi definetely loved its "result" auto variable.
IvankoB commentedon May 11, 2017
Yes. In order that not to create this pattern within function.
IvankoB commentedon May 11, 2017
Yes, it's short but note the "&h -> h" l-value usage in the return calculation.
burdges commentedon May 11, 2017
I think Rust already requires the caller allocate the space for returned
struct
s @IvankoB but maybe one could optimize it for tuples by passing pointers for each component instead of one pointer for the whole tuple.I think non-lexical lifetimes might enable this without adding complexity to the
fn
declaration,so maybeas opposed to
IvankoB commentedon May 11, 2017
It's an explicit creation of stack variables which will be returned when function finishes. The ability of returning stack variables as ref counted function results is very handy fetarure of Rust. With the proposal in simple cases it can be simplified even more.
wesleywiser commentedon May 11, 2017
Wouldn't the "rustic" way of writing this actually use
Result
or at leastOption
? For example, this is what thelibstd
does:https://github.com/rust-lang/rust/blob/2cc3358e4f1c4a79685745a461a1be1ce784b88a/src/libstd/sys/windows/fs.rs#L257
IvankoB commentedon May 11, 2017
Our collegue "burdges" has got the idea.
mark-i-m commentedon May 11, 2017
Sorry, I'm still not convinced. For me this piece of code was genuinely confusing for a minute until I realized
r
is the implicit return value. I can't think of anywhere else in rust where variables can just appear out of nowhere like that...burdges commentedon May 12, 2017
Yeah I realized it'd need an explicit name like
fn
@mark-i-mI've no idea if this is a good idea, but I wanted to point out that destructuring can happen in the body without fancy new syntax in the
fn
declaration.IvankoB commentedon May 12, 2017
This feature also involves empty return ("Go") or no return at all ("Delphi").
mark-i-m commentedon May 12, 2017
@IvankoB what do you mean by "empty return" and "no return at all"? Are they analogous to return
()
or!
?31 remaining items