-
Notifications
You must be signed in to change notification settings - Fork 562
Description
Proposal
We know that Go has the following Untyped Literals:
- untyped int (e.g.,
100), which could actually be intN, uintN,uintptr, or their named types. Here, N can be default, 64, 32, 16, or 8. - untyped float (e.g.,
1.2), which could actually befloat64,float32, or their named types. - untyped complex (e.g.,
1 + 2i), which could actually becomplex128,complex64, or their named types. - untyped string (e.g.,
"Hello"), which could actually bestringor its named type. - untyped rune (e.g.,
'a'), which could actually beruneor its named type. - untyped bool (e.g.,
true), which could actually beboolor its named type. - untyped
nil, which could actually be any pointer type, includingunsafe.Pointer.
In XGo, we plan to extend the concept of Untyped Literals.
First, we intend to extend untyped types to Python Literals and JavaScript Literals. That is, for untyped int, untyped float, untyped string, untyped bool, etc., in addition to their possibilities in Go, they could also be of type *py.Object or *js.Object.
Second, we introduce the concepts of untyped list (slice) and untyped map. For a List Literal like [1, 2, 3], in addition to inferring the element type, we also need to decide whether it is a slice in Go, a List in Python, or a List in JavaScript. Similarly, for a Map Literal like {"Mon": 1, "Tue": 2}, in addition to inferring the element types, we need to decide whether it is a Go map, a Python Dict, or a JavaScript Map.
Finally, for variables defined based on Untyped Literals, such as:
x := 100
a := []
var s = "Hello"We will defer the type inference of the variable until its first use, rather than making an immediate decision as in Go. For example:
x := []
x <- 100, 200
echo x[0] + x[1]In previous versions of XGo, x would be inferred as []any, causing x[0] + x[1] to report a syntax error. But now, x will be deferred until the statement x <- 100, 200 and inferred as []int. This is equivalent to the following in Go:
x := []int{}
x = append(x, 100, 200)
fmt.Println(x[0] + x[1])Here's another example:
import "py/std"
x := 1.2
std.print(x)This is equivalent to the following in Go:
import "github.com/goplus/lib/py"
import "github.com/goplus/lib/py/std"
func main() {
x := py.Float(1.2)
std.Print(x)
}