modularml / mojo

The Mojo Programming Language
https://docs.modular.com/mojo/manual/
Other
23.37k stars 2.6k forks source link

[Feature Request] Rename `[Unsafe]Pointer.address_of(x)` to something shorter #3602

Open soraros opened 1 month ago

soraros commented 1 month ago

Review Mojo's priorities

What is your request?

As title. Maybe to Pointer(to=thing).

What is your motivation for this change?

Any other details?

For the bike-shading crowd: I mostly just moved Chris's comments from Discord, please don't shoot the passenger.

melodyogonna commented 1 month ago

+1 to this, conveys the same meaning with a more succinct syntax, definitely more Pythonic.

lattner commented 1 month ago

I don't think that making it shorter is the only motivation, it is also about consistency: I'd suggest using initializer syntax, so that "creating a T" is always done with initializers.

If we did that, it would argue for something like Pointer(to=x) or Pointer(at=x) or Pointer(of=x) or Pointer(address=x) etc.

martinvuyk commented 1 month ago

Yesss, this will even spare having to set the type that the pointer is for since it can be inferred :+1:

Pointer(at=x) [...] Pointer(address=x)

these ones in particular might be confusing if you pass an Int and you come from the C world though

martinvuyk commented 1 month ago

Opened PR #3606 but I'm getting a segfault for _ListIter even though I only added a constructor for Pointer and didn't touch any more code :/

soraros commented 1 month ago

If we did that, it would argue for something like Pointer(to=x) or Pointer(at=x) or Pointer(of=x) or Pointer(address=x) etc.

More broadly speaking, the fact that Mojo supports function overloads opens the door to Swift-style, argument-label-rich API designs. I'm overall supportive to this approach, but we also need to aware of the trade-offs.

For object initialisation, static methods (technically classmethods) are like a "poor man's" overloading in Python. They do have some advantages though: