Skip to content
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

cannot import structs #2670

Closed
Tracked by #2693
mhorsley30896 opened this issue Feb 17, 2022 · 7 comments
Closed
Tracked by #2693

cannot import structs #2670

mhorsley30896 opened this issue Feb 17, 2022 · 7 comments

Comments

@mhorsley30896
Copy link

mhorsley30896 commented Feb 17, 2022

Suppose I have a simple contract that records a user input in a struct.

# @version ^0.3.0

struct A_Struct:
	a: uint256
	b: uint256

struct_map: public(HashMap[uint256, A_Struct])

struct_idx: uint256

@external
def __init__():
	pass

@external
def add_struct(_a: uint256, _b: uint256):
	
	struct_inst: A_Struct = A_Struct({a: _a, b:_b})

	self.struct_map[self.struct_idx] = struct_inst

	self.struct_idx += 1

Now if I export the interface for this contract such that another contract can use it

vyper -f interface contracts/MyContract.vy

# Functions

@external
def add_struct(_a: uint256, _b: uint256):
    pass

@view
@external
def struct_map(arg0: uint256) -> A_Struct:
    pass

If I save this output into an interface file called MyInterface.vy and then write a second contract

# @version ^0.3.0

import interfaces.MyInterface as MC

my_contract: MC

@external
def __init__(mc_address: address):

	self.my_contract = MC(mc_address)

@external
def other_add_struct(_a: uint256, _b: uint256):

	self.my_contract.add_struct(_a, _b)

Then when I try and compile this I get an error

Unhandled exception in 'contracts/MyOtherContract.vy':
UnknownType: Compilation failed with the following errors:

UnknownType: No builtin or user-defined type named 'A_Struct'
  contract "MC", function "struct_map", line 9:33 
       8 @external
  ---> 9 def struct_map(arg0: uint256) -> A_Struct:
  ----------------------------------------^
      10     pass

This error can be removed if I delete A_Struct as the return type for struct_map in MyInterface.vy. Is this expected behaviour or am I misunderstanding something around structs and interfaces?

@charles-cooper
Copy link
Member

@mhorsley30896 struct definitions aren't exported from interface definitions. right now the thing to do is redefine struct A_Struct again from MyOtherContract.vy -

import interfaces.MyInterface as MC

# ...
struct A_Struct:
	a: uint256
	b: uint256
# ...

my_contract: MC

I'm leaving this issue open though so we can track a discussion about changing the semantics here.

@mhorsley30896
Copy link
Author

@charles-cooper you also have to define A_Struct in the interface as well? It does seem like overkill (and a little unpythonic) to have to keep copying and pasting struct definitions into arbitrary numbers of files

@charles-cooper
Copy link
Member

@charles-cooper you also have to define A_Struct in the interface as well? It does seem like overkill (and a little unpythonic) to have to keep copying and pasting struct definitions into arbitrary numbers of files

yep you need to define in both places -- and I agree about the overkill part

@fubuloubu
Copy link
Member

A side note: ABI encoding defines structs as tuples of their members (although there is some undefined behavior with tight packing), so one way to handle this might to implicitly convert structs to and from tuples, so that the interfaces matched up

@charles-cooper charles-cooper changed the title return Type for structs in interfaces causing compilation failure cannot import structs Mar 14, 2022
@charles-cooper
Copy link
Member

meeting notes: we should allow this, A_Struct would probably be referenced like MC.A_Struct

@hadfieldn
Copy link

+1 for ability to import structs -- would be very helpful!

@charles-cooper
Copy link
Member

implemented in #3663

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants