Typing

ASnake Documentation

Table Of Contents

Summary:

Typing requires or hints that a variable will primarily handle a specific datatype. This is optional.

Data Types:

Here is a list of types available by default:

  1. str
  2. int
  3. float
  4. complex
  5. list
  6. tuple
  7. set
  8. dict
  9. bool
  10. bytes
  11. bytearray
  12. memoryview
  13. frozenset
  14. range
  15. object

These must be called via their above name for them to register as applying a type to a variable.

str stands for String.

int stands for Integer.

float stands for Floating Point, like decimal.

dict stands for Dictionary.

bool stands for boolean.

You can read more about the types themselves at Python documentation.

Syntax type var:

To assign a type to a variable, it must be on the left of the variables name when it is declared. Preferrably for best perfomance when it is first used or even better, before the main set of code being ran, like before a loop or the start of a function. It is not required and you can type the variable anywhere. You only have to type a variable once. Once typed, the compiler and any static type checkers will assume the variable can only hold that type.

Here are some examples of usage:

int number = 2 + 2
str lang = "ASnake"
float euler = 0.57721
list weird = [1, 'hi', 4.444]
tuple state = (True, False)
set fruits = {'apple','lemon','dragonfruit'}
dict sales = {'chair': 24, 'bed': 6, 'plushies': 999}

Typing elements inside a list, tuple, set, dict is somewhat supported in simple cases.

list[int] scores = [24,74,29,23,67,11]

But is currently a bit buggy and not fully supported. Improvements may come in the future.

Currently defining custom types is not supported, but may be in the future.

Python's type hinting syntax is similar but in reverse, indicated by a colon and placing the type infront of the variable, like so:

number: int = 12

Bulk Annotation Syntax type::

You can declare the types of a bunch of variables by using a colon and indenting to start it's own block. Like so:

int:
    myFavoriteNumber = 12
    index 0
    noValueYetIntVar
    someVar is funcWhichGivesInt()

Saves you from having to do a bunch of int casting like:

int myFavorite number
int index 0
int noValueYetIntVar
int someVar is funcWhichGivesInt()

Yucky.

Function Define Type:

You can declare the type of a function. In Python it uses an arrow:

def give2() -> int: return 2

For ASnake's define, just include your type after does:

give2 does int do return 2

For function arguments it's the same way as declaring types for variables.

def addTwo(x: int):
    return x+2
addTwo does
    return x+2
from int x

As of now Python function defines don't support ASnake's typing like int x, however ASnake's function define supports both.

Scroll to top