Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Static methods #1847

Open
xushiwei opened this issue Apr 8, 2024 · 3 comments
Open

Static methods #1847

xushiwei opened this issue Apr 8, 2024 · 3 comments

Comments

@xushiwei
Copy link
Member

xushiwei commented Apr 8, 2024

Basic

Go syntax:

func NewT(arg1 T1, arg2 T2, ..., argN TN) *T

t := NewT(arg1, arg2, ..., argN)

Go+ syntax:

func T.New(arg1 T1, arg2 T2, ..., argN TN) *T

t := T.new(arg1, arg2, ..., argN)

Go+ classfile syntax:

func .New(arg1 T1, arg2 T2, ..., argN TN) *T  // Or:
func T.New(arg1 T1, arg2 T2, ..., argN TN) *T

Here T.New is a normal static method. The general static method syntax is:

func T.Method(arg1 T1, arg2 T2, ..., argN TN) (ret1 R1, ret2 R2, ..., retM RM)

Typecast

explicit cast:

func T.cast(arg1 T1, arg2 T2, ..., argN TN) T

t := T(arg1, arg2, ..., argN)

implicit cast:

func T.icast(v OrigT) T

var t T = v  // var v OrigT

right cast:

func T.rcast(v T) DestT

dest := DestT(v)  // var v T

Note: Here static method cast, icast, rcast are special functions like init in Go.

@aofei
Copy link
Member

aofei commented Apr 8, 2024

Introducing static methods at this point seems reasonable, especially since Go+ already has the concept of classfile.

However, the "right cast" appears somewhat peculiar. It seems to create more problems than it solves.

For instance, in this example:

func OrigT.rcast(arg1 OrigT) DestT
func DestT.cast(arg1 OrigT) DestT // It can be defined like this, right?

var v1 OrigT
v2 := DestT(v1) // OrigT.rcast(v1) or DestT.cast(v1)?

It's unclear which method v2 := DestT(v1) would call. At least we can't tell just from this line of code. I assume there would be a priority, but that still wouldn't mitigate the confusion introduced by the "right cast".

@xushiwei
Copy link
Member Author

xushiwei commented Apr 8, 2024

Introducing static methods at this point seems reasonable, especially since Go+ already has the concept of classfile.

However, the "right cast" appears somewhat peculiar. It seems to create more problems than it solves.

For instance, in this example:

func OrigT.rcast(arg1 OrigT) DestT
func DestT.cast(arg1 OrigT) DestT // It can be defined like this, right?

var v1 OrigT
v2 := DestT(v1) // OrigT.rcast(v1) or DestT.cast(v1)?

It's unclear which method v2 := DestT(v1) would call. At least we can't tell just from this line of code. I assume there would be a priority, but that still wouldn't mitigate the confusion introduced by the "right cast".

eg.

func T.rcast(v T) int // convert T to int

xushiwei added a commit to xushiwei/gop that referenced this issue Apr 9, 2024
@xushiwei
Copy link
Member Author

xushiwei commented Apr 9, 2024

parser/printer: Static methods (#1848)

xushiwei added a commit to xushiwei/gop that referenced this issue Apr 9, 2024
xushiwei added a commit to xushiwei/gop that referenced this issue Apr 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants