openlang

Solc 0.7.0 rejects construction of my struct with a mapping member

My npm installation provides Solc version 0.7.0. Creating a Struct with a mapping inside emits the message: "Struct containing a (nested) mapping cannot be constructed."

Here is the code to look at:

// SPDX-License-Identifier: MIT
pragma solidity 0.7.0;

contract Store {
  struct Submission {
    uint256 amount;
    mapping(address => bool) votes;
  }
  Submission[] public submissions;
    ...

  function addSubmission(
    uint256 amount
  ) public {
    Submission memory fresh = Submission({ // compiler reports the error here
      amount: amount
    });

    submissions.push(fresh);
  }
}

With earlier solc versions, this same code builds successfully.

Thanks for taking a look!

The conversation continues 2

Follow the reasoning
  1. Permalink

    This ought to work:

    function addSubmission(uint256 amount) public {
      Submission storage fresh = submissions.push();
      fresh.amount = amount;
    }

    Best!

  2. Permalink

    Here's what worked for me:

    struct Submission {
      uint256 amount;
      mapping(address => bool) votes;
    }
    
    uint256 submissionCount;
    mapping(uint256 => Submission) submissions;
    
    function addSubmission(uint256 amount) public {
      Submission storage fresh = submissions[submissionCount++];
      fresh.amount = amount;
    }

Other sides of the same question

  1. Testing D arrays for a matching entry

    with associative arrays this is possible how is this done with ordinary arrays? I'd like a validation expression, for example

    Replies 2Latest activity d
  2. rows.Next said false and the error went nowhere

    Small tool on database/sql returned half a list. No error. The code. Context with a deadline. The deadline fired in the middl…

    Replies 3Latest activity sqlgo
  3. A partial index and a parameter in the WHERE clause

    I'm writing a small job runner in Go for a class project. The setup is pgx v5 through database/sql, Postgres 16 locally, and…

    Replies 10Latest activity sqlgo