Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

5
  • 1
    While this answers the question in the title, it seems that the actual intention of the person who asked it is to know whether the initialization can be done at the declaration line (to which the answer is no, as far as I know). Moreover, the question in the title is technically meaningless, because every variable in this language (and in any other language that I'm aware of) can be initialize with a predefined value. Think about it - the language would be useless otherwise! Commented May 8, 2019 at 10:28
  • 1
    OP didn't say anything about declaration line, but just showed a Python code and asked how to achieve similar behavior in Solidity. I don't think author meant that syntax should be in any way similar as well, because it is quite odd to expect similar syntax from languages as different from each other as Python is different from Solidity. Commented May 8, 2019 at 10:34
  • Even so, a link to solidity official documentation (as a comment to the question), would probably be more appropriate. The question itself should probably be closed (there would be millions of redundant such questions otherwise). And again, I'm pretty sure that if this dude knows the keyword mapping, then he also knows the basic initialization syntax. It makes sense that he's looking for a "non-basic" syntax. Commented May 8, 2019 at 10:36
  • I was asked to know if it is possible in declaration level and in case it's not possible, is there a best practice to do it - Constructor, function that will be called from the constructor, library? Commented May 8, 2019 at 10:46
  • 1
    Constructor is probably the best place, because it is guaranteed to be executed only once. Also, in case your initial values may be calculated from corresponding keys, you may consider lazy initialization. Commented May 8, 2019 at 10:47