Static methods
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.
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".
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
parser/printer: Static methods (#1848)