A Tour of Web 3: Ethereum & Smart Contracts with Solidity

# Solidity Language Basics

## ThePrimeagen

terminal

### Check out a free preview of the full A Tour of Web 3: Ethereum & Smart Contracts with Solidity course

The "Solidity Language Basics" Lesson is part of the full, A Tour of Web 3: Ethereum & Smart Contracts with Solidity course featured in this preview video. Here's what you'd learn in this lesson:

ThePrimeagen introduces the Solidity language and shares some common code structures like conditions, loops, and type declarations. Contracts are similar to classes. The base type in Solidity is a 256-bit unsigned integer.

Preview
Close
Get \$100 Off
Get \$100 Off!

### Transcript from the "Solidity Language Basics" Lesson

[00:00:00]
>> I'm gonna go on a brief, quick tour of the language. Like I said, I'm not gonna really go over every one of these things. I just wanna show them to you. So solidity, if you've seen an if statement, you know how if statements work, right? If you've seen a for Loop, just like every for Loop you've ever used.

[00:00:14]
Contracts effectively are identical to classes. You can hang members off contracts. There are constructors on contracts. You can specify a function with a scope and a return type. All these things should be pretty familiar. Yes you may not be able to type the exact syntax your first try just because you haven't done it enough time for it to be habitual but for now that's that, all right.

[00:00:35]
So that's the simplicity of the language. Like I said, the language is actually very simple. It only gets into the weeds when you get to the more advanced stuff. So the base type on solidity is a 256 bit number. It's unusual because base types usually aren't numbers, nor are they 256 bits that's actually a really large number.

[00:00:55]
It's effectively the size of the universe in atoms. Maybe I'm off by about a factor of 10,000. But that's a very large number, right? 10 to the 79, I think is two to the 56. And so you can subdivide these out and do like a u and eight, that's eight bits or one byte or representing 256 distinct values.

[00:01:16]
You can do a u and 16, 32, 64, 128. It allows some for some basic subdividing in there. Strings suck, so don't do them. That's the general rule of thumb is that unless if you have a bunch of hard coded ones, you're gonna construct some sort of output with.

[00:01:29]
Working with strings is really painful because the base type is 256 bit number. So you're kind of mashing strings into these number representations. It can lead to a lot of pain. I tend to just avoid strings in the things I've written every single time except for like I said meta information generation by simply just concatenate a bunch of stuff together that are hard coded strings and pulling out data.

[00:01:53]
Array, there are dynamic and static arrays or fixed arrays. If you specify array on the contract as a member, it is a dynamic array. Right now you're probably thinking, why does that matter? When we get to the final part of this it will make perfect sense for now, except it is beautiful.

[00:02:12]
If you specify an array inside the function, you specify it as a fixed array. I need an array of size 10, you cannot inline generate an array right now so you just have to do this and then you can add in each number as you'd like. So fixed inside of functions dynamic outside when it's on a member of a contract.

[00:02:30]
Maps again, different from what you're used to. It's a mapping, not necessarily a map, a map stores keys and values. Mappings do not store keys, and they technically don't even store values. It's kinda confusing. You do a mapping from type to type and the type can even be a complex type.

[00:02:50]
It doesn't have to be a simple type. And that will be a mapping. It'll do some super special magic underneath the hood, and it will be able to store that. But you cannot for each over keys. You cannot for each over values. You don't even know how many items are inside of your map because it's actually just simply a mathematical function that gets executed.

[00:03:09]
And we'll go over that in more detail later on, but it will make perfect sense why this is and just for now keep it that way all right. Struct, they're just like C you simply say struct foo or I guess go lang is another example and you can specify a bunch of stuff in there.

[00:03:25]
You can create a mapping from say you went to foo. You can create a full array, right? So you can use those, you can create a mapping to a mapping that is a UN to a full array. So there is no, you can do all the standard mixes you'd like.

[00:03:39]
There are also enums. We'll be using one here shortly, so I just didn't even put it in here. And there you go. That is the basic of the language. At this point, you've already seen enough of the solidity language, you pretty much know most of its bits. Some of the concepts get pretty hard if you ever want to learn more about solidity, go to soliditylang.org.

[00:03:59]
And it gives you this really nice like, you can learn everything about it. The layout of the memory, how call data works, types, global things, expressions, contracts, inline assembly, it has everything to it. It's very, very nice. I figured you probably didn't want me to read the docs for you.

[00:04:16]
You can read the friendly manual on your own time,we should probably discuss more of the fun parts and so we are gonna build as we learn that is always my favorite way to learn. It's not just simply reading but to build something fail get better learn why and slowly build over time.

[00:04:33]
So is there any questions thus far on what I just showed you with just the basic types, the unit type which is of course, 256 bit number, anything like that.?
>> Why does solidity exist? Why couldn't they have just made a library from Python or another Language?
>> My guessing is that as we go on, you'll see a lot of the complications with how they chosen some of their things that they've done.

[00:04:58]
Solidity seems to fit very, very well with this. I know Solona went with rust. I don't know how much it actually resembles rust itself or what subset they've chosen to implement from rust, but it appears to be much nicer that way. I don't know why they went with their own language.

[00:05:14]
I don't know why anyone invents their own language in fact, with all the languages we already have, pretty much all the problems you've ever wanted have all ready been solved. But they've chosen this language. It works very well for what they want. And when you break into inline assembly, it works very, very well.

[00:05:28]
And so I think that they kind of chose this to ease a lot of it. I cannot speak on behalf that I don't know the technical answer, but a lot of the peculiarities of the Ethereum network are built into the language and you'll see why soon because like I said, it's not a map.

[00:05:42]
There is no map. There's a mapping. It's like a map. It's unique, if you will.

### Learn Straight from the Experts Who Shape the Modern Web

• In-depth Courses