Add extra generic test and comments

This commit is contained in:
Mattia Giambirtone 2023-12-08 18:46:12 +01:00
parent 2f74c23774
commit d04f412347
Signed by: nocturn9x
GPG Key ID: 8270F9F467971E59
1 changed files with 33 additions and 5 deletions

View File

@ -2,19 +2,47 @@ type int64 = object {
#pragma[magic: "int64"]
}
# In peon, all objects are "first class", meaning they can be passed around as
# values: Just like you can pass around instances of the int64 type (1, 2, etc.),
# peon allows you to pass around the int64 type itself; This is awesome for expressiveness,
# but it creates a few ambiguities when trying to figure out whether "int64" means "a value
# of type int64" or "the type int64 itself": for this reason, generic declarations split their
# arguments into two parts, generic values and generic types. Generic types go in between
# angle brackets, while generic values go in between square brackets. This means that the type described
# below has one generic argument T that is the integer type itself, and another generic argument V that
# is a value of type int64. This fixes the ambiguity and keeps the generic instantiation syntax as simple
# as possible. This syntax is very useful in cases like the built-in array type: it allows the syntax for it
# to be just array[T, N], where T is the type of its elements and N is its size
type Test<T: int64>[V: int64] = object {
typeObj: T;
value: V;
}
type Test2<T: Test>[V: Test] = object {
typeObj: T;
value: V;
}
# Feel free to uncomment these and see how the typechecker reacts (hopefully it fails lol)
Test[int64, 1]; # Works
# Test[int64, int64]; # Error: expecting an expression of type int64, got typevar[int64] instead
# Test[1, int64]; # Error: expecting an expression of type typevar[int64], got int64 instead
Test2[Test, Test[int64, 1]]; # Also works
Test[int64, 1]; # Works: int64 is a type and 1 is a value of type int64
# Test[int64, int64]; # Error: expecting an expression of type int64, got typevar[int64] instead
# Test[1, int64]; # Error: expecting an expression of type typevar[int64], got int64 instead
Test2[Test, Test[int64, 1]]; # This also works. Nested generic instantiation go brrrr
# Test2[Test[int64, 1], Test]; # Error: expecting an expression of type typevar[Test<T: typevar[int64]>[V: int64]], got Test<T: typevar[int64]>[V: int64] instead
# P.S.: You might be wondering "what the hell is a typevar?". Good question!
# A typevar is a special built-in type that represents a... type. Yeah, not very
# useful hm? Think of it like this: when you declare an object Foo, the object you
# get by referencing it is of type typevar[Foo]: this means "Foo is a type". When you
# construct an instance, say x, of Foo, its type is just Foo. Typevars are mostly needed
# in places where you want to enforce that some value must be a type and not a value. A
# name of type typevar[int64 | int32], for example, means "I want this thing to either be
# the int32 type or the int64 type, and NOT an instance of them". If you've ever used Python,
# you can think of typevar as the "type" class, but on steroids
# P.P.S: A typevar is a generic type, so usually it wouldn't be possible to use it by itself.
# For convenience purposes however, peon allows the use of a bare typevar by replacing it with
# typevar[any] (any is another special built-in type that means "anything that has a type")