Types and Type Hinting#
Primitive Types#
Jac supports all Python primitive types including int
, float
, str
, bool
, list
, dict
, set
, and tuple
. These types work identically to their Python counterparts.
Custom Types#
In Jac, custom types are created using the obj
keyword, which provides functionality similar to Python's dataclasses but operates like a regular class. Objects defined with obj
use the has
keyword to declare attributes and can post_init
for initialization logic (analogous to post_init in dataclasses). While traditional init
in Python is supported in Jac, using has
declarations with post_init
is the recommended pattern in Jac for clearer and more maintainable code. Additionally, passing self
keyword as an argument is not required when declaring abilities under the custom type.
Here is an example.
Type Hinting and Annotation#
Type hints in Jac are mandatory for ability (function)
parameters and object attributes declared with has
. While return type hints are optional and default to -> None
when omitted, including them is considered good practice for code clarity and maintainability.