Updated specification #8
23
README.md
23
README.md
|
@ -18,6 +18,25 @@ kernel.
|
|||
|
||||
Only types supported in the kernel can be referenced.
|
||||
|
||||
Examples:
|
||||
```python
|
||||
FOO = 0
|
||||
|
||||
@kernel
|
||||
def correct() -> int:
|
||||
global FOO
|
||||
return FOO + 1
|
||||
|
||||
@kernel
|
||||
def fail_without_global() -> int:
|
||||
return FOO + 2
|
||||
|
||||
|
||||
@kernel
|
||||
pca006132 marked this conversation as resolved
Outdated
lriesebos
commented
Should we use brackets instead, just like used for all other typing? Should we use brackets instead, just like used for all other typing? `KernelInvariant[T]`
sb10q
commented
I think so. I think so.
pca006132
commented
Yes, I would fix this later. Yes, I would fix this later.
|
||||
def fail_write() -> None:
|
||||
FOO += 1
|
||||
|
||||
```
|
||||
|
||||
## Class and Functions
|
||||
* Instance variables must be annotated: (Issue #1)
|
||||
```python
|
||||
|
@ -31,7 +50,7 @@ Only types supported in the kernel can be referenced.
|
|||
* Three types of instance variables: (Issue #5)
|
||||
* Host only variables: Do not add type annotation for it in the class.
|
||||
* Kernel Invariants: Immutable in the kernel and in the host while the kernel
|
||||
is executing. Type: `KernelInvariant(T)`. The types must be immutable.
|
||||
is executing. Type: `KernelInvariant[T]`. The types must be immutable.
|
||||
(use tuple instead of list in the host, but the type annotation should still
|
||||
be list?)
|
||||
* Normal Variables: The host can only assign to them in the `__init__`
|
||||
|
@ -47,6 +66,8 @@ Only types supported in the kernel can be referenced.
|
|||
* Function pointers are supported, and lambda expression is not supported
|
||||
currently. (maybe support lambda after implementing type inference?)
|
||||
|
||||
Its type is denoted by the typing library, e.g. `Call[[int32, int32], int32]`.
|
||||
|
||||
## Built-in Types
|
||||
* Primitive types include:
|
||||
* `bool`
|
||||
|
|
Loading…
Reference in New Issue
Would it still be possible to type host-only variables and class variables?
I did not think about that, maybe we can add some new syntax similar to ClassVar for host only variables?
Sounds a bit messy and would perhaps negate the advantages of host type annotations (e.g. third-party tools may not understand
ClassVar
and complain that the type annotation is incorrect).I propose not supporting host type annotations in kernel classes; anything that is annotated goes on the device.
We type practically everything in our DAX library, and I think it would be good to still allow typing of host code. Maybe you like one of these two ideas:
Allow host-only variable typing in methods or by using
Immutable[]
Make kernel-only variable typing special using
Kernel[]
and be fully compatible with regular Python typingThat option would be ok, but might need better names than
Kernel
(andImmutable
).Maybe
Kernel[]
/KernelImmutable[]
since the two annotations are similar. And we would use "kernel" terminology in nac3core.Yes, I think
Kernel[]
andKernelImmutable[]
is great and I like the fact that it is explicit!Yes, it is fine for me. Did you think about the syntax I mentioned in the screenshot? Using
Immutable(...)
instead ofImmutable[]
.I'm not entirely sure if we can let IDE understand
Immutable[T] == T
for completion and checking, but we can do that withImmutable(T)
. However, one caveat is that it would be harder to let users to define the type elsewhere and use it, as the information ofImmutable()
would be loss. Maybe we should have a look at how typing does it for other generics.Tbh, I do not think the IDE autocomplete feature should be leading this choice. And besides, IDE's understand that
ClassVar[T] == T
, so it would not surprise me if we can also make that happen forKernel[]
andKernelImmutable[]
. Brackets are the syntax used for typing in Python, so I would prefer we stick with that.To further motivate my point of view, type checkers such as mypy go totally bad when using parenthesis. See for example this error message for
TArray()
(ARTIQ 6):@pca006132 @sb10q did we now decide on the
Kernel[]
andKernelImmutable[]
annotations? It is not contained in this merge request.Sorry I forgot this one, this is two months ago :(.
I think
Kernel
is used for kernel only attributes, andKernelImmutable
is for kernel invariants right? CanKernelImmutable
be changed by the host? I think it is OK, or we can open another issue If we want further discussion about this.As per the discussion in #5, an attribute with
KernelImmutable
can be modified by the host, but only when no kernel is running. In particular, it cannot be modified inside a RPC call, and this would be enforced in the Python interpreter via the@kernel
decorator on the class.