gop icon indicating copy to clipboard operation
gop copied to clipboard

Static methods

Open xushiwei opened this issue 1 year ago • 3 comments

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.

xushiwei avatar Apr 08 '24 06:04 xushiwei

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".

aofei avatar Apr 08 '24 09:04 aofei

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 avatar Apr 08 '24 10:04 xushiwei

parser/printer: Static methods (#1848)

xushiwei avatar Apr 09 '24 09:04 xushiwei