-
Notifications
You must be signed in to change notification settings - Fork 11
Allow custom ExternArgument in extern declaration #83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow custom ExternArgument in extern declaration #83
Conversation
This is useful if people want to specify ArrayReferenceType as an argument to an extern function. ArrayReferenceType require an explicit Access modifier (mutable or readonly). Since this is just one edge case, we can let users still specify just the type for all other extern declarations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great implementation, I have minor comments that should be easy to resolve.
oqpy/subroutines.py
Outdated
|
||
SubroutineParams = [oqpy.Program, VarArg(AstConvertible)] | ||
|
||
FnType = TypeVar("FnType", bound=Callable[..., Any]) | ||
|
||
|
||
@dataclass | ||
class OqpyArgument: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we keep the style that we use for other classes? -> OQPyArgument
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
oqpy/subroutines.py
Outdated
|
||
name: str | ||
dtype: ast.ClassicalType | ||
access: Literal["readonly", "mutable"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add None
and make it the default value?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
|
||
name: str | ||
dtype: ast.ClassicalType | ||
access: Literal["readonly", "mutable"] | None = None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This follows the precedent established by the OpenQASM reference AST, so it's fine to have here. But I will say that I think the underlying premise is slightly mistaken. Access modifiers or type qualifiers make most sense as part of the type itself. In a subroutine, if you ask "what is the type of this variable" it's nice to be able to say "a mutable array reference".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I might agree. After reading the reference AST code and the parser grammar, I think we should cut a ticket to gh/openqasm and fix it there first. ArrayReferenceType is only used with *Argument
node but the modifier has been added to the *Argument
node and not the type node.
This is useful if we want to specify ArrayReferenceType as an argument to an extern function. ArrayReferenceType require an explicit Access modifier (mutable or readonly). Since this is just one edge case, we can let users still specify just the type for all other extern declarations.
I have added a test that demos its usage.