Data Types (part 1) - Value Types | Vyper (0.2) - 雙語字幕

Let's learn about the different data types in Piper.
Each data type is either a value or a reference.
In this video, we'll go over value types, and in the next video, we'll go over references.
First of all, what are value types?
value types are data types that are passed by their values.
For example, let's say that we have an unsigned integer named x and it is equal to 1.
And then we reassign variable x to a variable named y.
And then we modify the variable y.
For example, we add one to its current value.
Now the big question is, what is the value of x?
And the answer is x is still equal to one.
This is because when we assign x to y, the value of x was copied over to a variable y.
So that when we change the value of variable y, it doesn't change the value of x.
So that's value types.
They copy variables by their variable y.
so that making a change to one variable does not affect the other variable.
Let's now take a look at value types available in Viper.
The first value type that I'm going to show you is a Boolean.
So I'll name it B and then we'll say it's a public variable and up type.
Boo.
By declaring this variable as public, we'll be able to access this variable after we deploy it using Remix.
I'll make a separate video about the different visibility of the variable.
For now let's just go over what this line of code is doing.
Here we're declaring our variable named called b.
The type of this variable is a boolean and it is a public variable which means that we'll be able to get the value of this b variable after the contract is deployed.
All moving on another value type is int 128.
So we will name it i and then it will be public and we can declare the type has int 128.
The range of this variable goes from negative 2 to the 127 to 2 to the 127.
So this is the data type that you will want to use when you want to handle both negative and positive numbers.
Next, we have Uint 256, which ranges from 0 to 256 minus 1.
and this is the variable to use when you're only dealing with non-negative numbers.
In Viper we also have decimals.
The range of decimals is same as in 128 and it supports up to 10 decimal places.
Now this is a useful data type that is not available in Solidity.
Alright, moving on, we have our address type.
Bites 32 and it's useful when you want to store the cryptographic cache of something.
Now surprisingly we also have byte arrays and strings as value types.
So this means that every time you copy a byte array or a string, it will copy each element in the array.
If you have a string of length 1000 and then reassign it to another variable.
Basically are running a for loop for 1000 characters copying each element.
Let's quickly go over what these two lines of code are doing.
For the first one, BS, we're declaring a variable named BS, and it's a public variable.
The data type is bytes, and this byte is at most 100.
Likewise, first chains were declaring a variable named s, it's a public variable,
of type string, and this variable can be at most 100 characters long.
These are the value types available in Viper.
Let's set these variables to some concrete value.
So we'll define init function.
And the first thing that we need to do is say, add external, and say def to underscores init to underscores parentheses calling.
Now this external means that this init function can be called from outside of the contract.
and then we'll set the variable b to true by saying self dot b equals true.
We'll set i to minus 1 by saying self dot i equals minus 1.
We'll set the unsigned variable to some positive number.
We want to We'll set the decimal to 3.14 and we'll set the address type to some random
address and then the bytes 32 to also some random bytes 32.
The byte array,
we also set it to some random byte,
and we'll say that it's a byte by prefixing with a b, double quotes, slash x, and then the value, 0, 1.
For the string, we'll say this is a string.
Let's make sure that this contract compiles.
So I'm going to open my terminal and type Viper, followed by the name of the contract here.
I named it value types.
And you can see that our contract compile is run through a demo using remix.
So I'm going to copy the code.
Open remix.
And then activate Viper.
Next, I'll create a Viper file.
Here, I'll name it value type.by paste the code.
And then click on the biker compiler and then click remote compiler and then click compile All right,
let's deploy this contract,
so I'm gonna click deploy scroll down and then click on the contract that was just deployed and let's check that
these variables are set to these values over here address b is for boolean b32 is bytes 32 bs is bytes array g is decimal.
Note that remix does not know how to handle decimal types yet.
Alright, moving on, let's check the value of int 128.
It's equal to minus one.
the string value, this is a string, and the on-site value is 1 to 3, right?
So are the value types of Viper, Boolean, on-site and signed integers, decimals, edges, bytes 32, and also strings and bytes array.
The last two data types being a value type is a little bit surprising, since in Solidity these two types are reference types.
翻譯語言
中文-繁体

解鎖更多功能

安裝 Trancy 擴展,可以解鎖更多功能,包括AI字幕、AI單詞釋義、AI語法分析、AI口語等

feature cover

兼容主流視頻平台

Trancy 不僅提供對 YouTube、Netflix、Udemy、Disney+、TED、edX、Kehan、Coursera 等平台的雙語字幕支持,還能實現對普通網頁的 AI 劃詞/劃句翻譯、全文沉浸翻譯等功能,真正的語言學習全能助手。

支持全平臺瀏覽器

Trancy 支持全平臺使用,包括iOS Safari瀏覽器擴展

多種觀影模式

支持劇場、閱讀、混合等多種觀影模式,全方位雙語體驗

多種練習模式

支持句子精聽、口語測評、選擇填空、默寫等多種練習方式

AI 視頻總結

使用 OpenAI 對視頻總結,快速視頻概要,掌握關鍵內容

AI 字幕

只需3-5分鐘,即可生成 YouTube AI 字幕,精準且快速

AI 單詞釋義

輕點字幕中的單詞,即可查詢釋義,並有AI釋義賦能

AI 語法分析

對句子進行語法分析,快速理解句子含義,掌握難點語法

更多網頁功能

Trancy 支持視頻雙語字幕同時,還可提供網頁的單詞翻譯和全文翻譯功能

開啟語言學習新旅程

立即試用 Trancy,親身體驗其獨特功能

立即下載