-
Notifications
You must be signed in to change notification settings - Fork 118
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
Improve CircuitValue #269
Improve CircuitValue #269
Conversation
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.
some comments:
abstract class CircuitValue { | ||
constructor(...props: any[]) { |
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.
fun fact: this function was written verbatim, completely correctly, by github copilot on the first try!!!
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.
That's pretty amazing!
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.
That is really cool! I'm going to try out copilot.
@@ -122,21 +166,6 @@ abstract class CircuitValue { | |||
} | |||
} | |||
|
|||
(CircuitValue as any).check = function (v: any) { |
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.
was able to remove this hack because I got the types working on CircuitValue
super(); | ||
this.magnitude = magnitude; | ||
this.sgn = sgn; | ||
super(magnitude, sgn); |
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 left this constructor because it adds something nice -- a default value for one of the arguments
@@ -14,6 +14,7 @@ | |||
"useDefineForClassFields": false, // ensure correct behaviour of class fields with decorators | |||
|
|||
"strict": true, // for the full TypeScript experience | |||
"strictPropertyInitialization": false, // to enable generic constructors, e.g. on CircuitValue |
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.
if we go with this PR, this should be added in the zkapp CLI template projects as well
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.
cc @ymekuria has a heads up 👀
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 agree we should fix this globally in the CLI and push users to do the same instead fixing locally by adding a !
to the prop, e.g. @prop value!: Field
.
@@ -84,6 +83,7 @@ class SudokuZkapp extends SmartContract { | |||
|
|||
// finally, we check that the sudoku is the one that was originally deployed | |||
let sudokuHash = this.sudokuHash.get(); // get the hash from the blockchain | |||
this.sudokuHash.assertEquals(sudokuHash); |
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 is a fix that's unrelated to the rest of the PR
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.
The changes look good to me as long as everyone agrees on this controversial change :D
I added an issue in the zkapp-cli repo to update the Sudoku example if this lands in it's current state.
const value = (v as any)[key]; | ||
if (propType.check === undefined) | ||
throw Error('bug: circuit value without .check()'); | ||
propType.check(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.
Does this error get thrown if a CircuitValue class does not define a check
function for itself?
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.
no! it would get thrown if one of our primitives was missing check -- which isn't the case --, or maybe if a developer uses something which isn't a CircuitValue,
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.
but probably won't get thrown at all :D
abstract class CircuitValue { | ||
constructor(...props: any[]) { |
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.
That's pretty amazing!
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.
The changes LGTM assuming we all decide to accept the controversial changes. I added an issue to the CLI similar to @MartinMinkov 's to update the TS Config if this PR lands.
I generally like this approach of reducing boilerplate in the constructor for users. The TS error downside is minimal and can be mitigated fairly easily. Introducing a new pattern of adding a static from
method will be more involved. It would involve changing existing code, updating docs, and user eduction. I think the upside is probably worth all this.
Hmm ok, we also don't have to do this. If you don't prefer it then we can also stick with constructors in our examples, or just don't single out one way of doing it |
I'm ok to do this if we all agree that this pattern is better. We should be clear and consistent on our recommendations of best practices. |
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 looks great, I think the tradeoffs are all worth it 💯
this makes significant changes to the
CircuitValue
class, some non-controversial and some potentially controversial:"Property has no initializer and is not definitely assigned in the constructor"
this can be fixed locally by adding a
!
to the prop, e.g.@prop value!: Field
. it can also be fixed globally in the project by adding a TS config option, which we should probably do in the CLI template projectsuper()
. as such, this change probably breaks a couple of existing code, in a way that fixing it might not be very obvious. if we go with this, I think going forward we should not encourage the pattern of overriding the constructor in examples, but instead add staticfrom
methods which add any additional logic and invoke the original constructor. e.g., see how I changed the sudoku example