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
sb3d ago
You can use the return keyword with the ^ move sigil return socket^
aurelian
aurelian3d ago
why doesn't the return in accept need that? if I add it, the compiler says it's redundant
sb
sb3d 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
aurelian3d ago
No description
aurelian
aurelian3d ago
this is such a footgun
sb
sb3d 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
aurelian3d ago
python doesn't copy structs either that only makes sense with boxed classes
sb
sb3d 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
aurelian3d ago
yeah I have to disable the constructors and watch what breaks
sb
sb3d 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
aurelian3d 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
sb3d 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
aurelian3d ago
say ^ did a copy vs transfer, and move was the default it's a 2 second fix if you get an error
sb
sb3d 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
aurelian3d 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
sb3d 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
aurelian3d 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
sb3d ago
And they aren't rewriting those parts in C?? 😵‍💫
aurelian
aurelian3d ago
and presumably you want them to use mojo instead? I get it though, it's a gray area
rd4com
rd4com19h 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
aurelian17h ago
is there a written deep dive on lifetimes? this video is great btw
rd4com
rd4com17h 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