User: Password:
Subscribe / Log in / New account



Posted Apr 5, 2013 22:47 UTC (Fri) by jimparis (subscriber, #38647)
In reply to: Rust-lang by HelloWorld
Parent article: Mozilla and Samsung building a new browser engine

> It doesn't matter whether the branch can be taken, the values of both branches are of type ()

So imagine a case where both branches are different. Or where the conditional is deep inside a function before the return value finally gets to the assignment. If having a conditional return type () makes sense in some cases and not others, then you can't trivially warn about it.

(Log in to post comments)


Posted Apr 6, 2013 0:26 UTC (Sat) by HelloWorld (guest, #56129) [Link]

> So imagine a case where both branches are different.
That's a type error in itself in most cases. In order for such code to typecheck, the branches' types must have a common supertype. As far as I can see, Rust allows only interfaces to form hierarchies, and I don't know if it's even possible for () to implement interfaces. But even if it is, this situation is going to be so rare that it's not worth losing any sleep over it.


Posted Jun 21, 2013 8:04 UTC (Fri) by jessopher (guest, #91536) [Link]

You can return a pointer to a value of a type that satisfies a trait constraint, but not a direct value.

Imagine something like this

fn foo(which: bool) -> Number
if which { return (1 as int); }
else { return (1 as byte); }

now ask yourself "what is the bit-width of the value returned?", and "how is the implementation of the interface specific to each subtype determined at runtime?". For pointers, what is actually pointed to can be something that carries runtime type information, like the bit-width of the value, and the address of some dispatch table.

So really the situation isn't a rarity, it is non-existent. You are not going to accidentally return a pointer to (), from a function that has a polymorphic return type that () satisfies.

Copyright © 2017, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds