why is this return a copy?

# @value
struct UnixSocket:
var address: Optional[UnixAddress]
var fd: Int32

@staticmethod
fn connect(path: StringSlice) raises -> UnixSocket:
socket = UnixSocket()
address = UnixAddress(path)
connect(socket.fd, address, sizeof[UnixAddress]())
socket.address = address^ # this would also be a copy, easy to forget the ^
return socket # 'UnixSocket' is not copyable because it has no '__copyinit__'

fn accept(self) raises -> UnixSocket:
if not self.address:
raise Error("socket address unintialized")
conn = accept(self.fd, self.address.value(), sizeof[UnixAddress]())
return UnixSocket(fd=conn) # this one is fine, not sure why
# @value
struct UnixSocket:
var address: Optional[UnixAddress]
var fd: Int32

@staticmethod
fn connect(path: StringSlice) raises -> UnixSocket:
socket = UnixSocket()
address = UnixAddress(path)
connect(socket.fd, address, sizeof[UnixAddress]())
socket.address = address^ # this would also be a copy, easy to forget the ^
return socket # 'UnixSocket' is not copyable because it has no '__copyinit__'

fn accept(self) raises -> UnixSocket:
if not self.address:
raise Error("socket address unintialized")
conn = accept(self.fd, self.address.value(), sizeof[UnixAddress]())
return UnixSocket(fd=conn) # this one is fine, not sure why
22 Replies
sb
sb2mo ago
You can use the return keyword with the ^ move sigil return socket^
aurelian
aurelianOP2mo ago
why doesn't the return in accept need that? if I add it, the compiler says it's redundant
sb
sb2mo ago
The first one is already basically an lval, the second one is an rval because you construct the socket in the expr It shouldn't mark it as redundant
aurelian
aurelianOP2mo ago
No description
aurelian
aurelianOP2mo ago
this is such a footgun
sb
sb2mo ago
Oh, right, you don't need it on the second one because it is an rval :) I have my opinions but it's ultimately one of the various concessions for pythonicity
aurelian
aurelianOP2mo ago
python doesn't copy structs either that only makes sense with boxed classes
sb
sb2mo ago
I agree, and am not a fan of the relatively loose copy semantics but it is at least possible to learn your way around (and is a hill I have already died on to little effect)
aurelian
aurelianOP2mo ago
yeah I have to disable the constructors and watch what breaks
sb
sb2mo ago
That's what I've taken to doing--completely nuking implicit copy constructors and combing through the rubble left behind I think the situation will improve once explicit copy is better supported
aurelian
aurelianOP2mo ago
I don't see how this impacts python users negatively, the copies will be rare and they would IMO appreciate the education this is basically sweeping it under the rug
sb
sb2mo ago
Python users (most programmers, honestly) are not often open to significant disruptions to their intended approach Rust already steals a lot of that crowd, Haskell another chunk
aurelian
aurelianOP2mo ago
say ^ did a copy vs transfer, and move was the default it's a 2 second fix if you get an error
sb
sb2mo ago
I personally don't like it being a magic char in the first place, but yeah--I personally might have swapped the two if it were my call
aurelian
aurelianOP2mo ago
and you can say thank you compiler for ensuring I don't do wasteful things if anything, the python people would go, wait python just yolos this stuff around? mojo is awesome
sb
sb2mo ago
I've honestly seen a good chunk of python devs be rather dismayed by being forced to consider it--systems dev folks who want that level of control are a surprisingly rare breed 🤓
aurelian
aurelianOP2mo ago
I've seen more microbenchmarking from the python crowd that most others they do care about critical code when they find out there was a better default they will scratch their heads, like I am
sb
sb2mo ago
And they aren't rewriting those parts in C?? 😵‍💫
aurelian
aurelianOP2mo ago
and presumably you want them to use mojo instead? I get it though, it's a gray area
rd4com
rd4com2mo ago
@aurelian , there is a video that explains this: https://www.youtube.com/watch?v=9ag0fPMmYPQ&ab_channel=Modular
Modular
YouTube
Mojo🔥: a deep dive on ownership with Chris Lattner
Learn everything you need to know about ownership in Mojo, a deep dive with Modular CEO Chris Lattner If you have any questions make sure to join our friendly Discord community: https://modul.ar/discord If you're ready to get started with Mojo, check out the programming manual and installation guide: https://modul.ar/mojo-get-started 0:00 Moj...
aurelian
aurelianOP2mo ago
is there a written deep dive on lifetimes? this video is great btw
rd4com
rd4com2mo ago
There is the manual (which is really good ❤️‍🔥): https://docs.modular.com/mojo/manual/values/lifetimes
Lifetimes and references | Modular Docs
Working with lifetimes and references.
Want results from more Discord servers?
Add your server