Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

VIP: Convert Event construction to use kwargs #4249

Open
z80dev opened this issue Sep 19, 2024 · 0 comments · May be fixed by #4257
Open

VIP: Convert Event construction to use kwargs #4249

z80dev opened this issue Sep 19, 2024 · 0 comments · May be fixed by #4257

Comments

@z80dev
Copy link
Contributor

z80dev commented Sep 19, 2024

Simple Summary

Make the syntax for creating event instances more clear, readable, and consistent with the syntax for struct creation by leveraging keyword arguments.

Motivation

Structs have been updated to be constructed by kwargs ( #2381 ). This new syntax makes struct construction more convenient than the previous implementation.

Event creation could benefit from this same syntax change, not only improving the existing event syntax, but also making it consistent with struct creation, which lowers the mental overhead programmers need to work with when writing Vyper.

Right now, users have to remember the two different, but similar syntaxes for creating events and for creating structs. The syntax for creating structs is more readable, clear, and explicit. So we should update events to use the same syntax as structs.

Specification

There are no semantic changes involved in this VIP.

Curent Syntax

The current syntax around events uses positional arguments as follows

# event definition
event EntryCreated:
    id: indexed(uint256)
    round_id: indexed(uint256)
    owner: indexed(address)
    amount: uint256

# event construction
log EntryCreated(foo, bar, msg.sender, 1000)

Proposed Syntax

We propose changing this to the following syntax which uses keyword arguments

# event definition (unchanged)
event EntryCreated:
    id: indexed(uint256)
    round_id: indexed(uint256)
    owner: indexed(address)
    amount: uint256

# event construction
log EntryCreated(id=foo, round_id=bar, owner=msg.sender, amount=1000)

Comparison with Struct Syntax

The proposed kwarg is more consistent with the current syntax around structs, and the benefits of the change are very apparent when seen side by side

Given the following struct and event definitions:

# Struct Definition
struct Entry:
    id: uint256
    round_id: uint256
    owner: address
    amount: uint256

# Event Definition (unchanged)
event EntryCreated:
    id: indexed(uint256)
    round_id: indexed(uint256)
    owner: indexed(address)
    amount: uint256

We construct these as follows:

Existing Syntax

# Creating a Struct
entry: Entry = Entry(id=foo, round_id=bar, owner=msg.sender, amount=10000)
# Logging an Event
log EntryCreated(foo, bar, msg.sender, 10000)

Proposed Syntax

# Creating a Struct
entry: Entry = Entry(id=foo, round_id=bar, owner=msg.sender, amount=10000)
# Logging an Event
log EntryCreated(id=foo, round_id=bar, owner=msg.sender, amount=10000)

Backwards Compatibility

Similarly to the Struct Kwarg VIP, we can support both syntax options via an AST transformation and warn the user about the change. After a few releases, the previous syntax can be dropped, as it is better to have one syntax for this in the long run.

Dependencies

N/A

References

#2381
#3777

Copyright

Copyright and related rights waived via CC0

@z80dev z80dev changed the title VIP: Events should VIP: Convert Event construction to use kwargs Sep 19, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant