Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the admin-site-enhancements domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/prntech/public_html/wp-includes/functions.php on line 6121

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the acf domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/prntech/public_html/wp-includes/functions.php on line 6121

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the hide-my-wp domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/prntech/public_html/wp-includes/functions.php on line 6121

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the unlimited-elements-for-elementor domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/prntech/public_html/wp-includes/functions.php on line 6121

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the wp-security-audit-log domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/prntech/public_html/wp-includes/functions.php on line 6121

Warning: Cannot modify header information - headers already sent by (output started at /home/prntech/public_html/wp-includes/functions.php:6121) in /home/prntech/public_html/wp-content/plugins/wp-force-ssl/inc/wp-force-ssl-tools.php on line 19

Warning: Cannot modify header information - headers already sent by (output started at /home/prntech/public_html/wp-includes/functions.php:6121) in /home/prntech/public_html/wp-content/plugins/wp-force-ssl/inc/wp-force-ssl-tools.php on line 23

Warning: Cannot modify header information - headers already sent by (output started at /home/prntech/public_html/wp-includes/functions.php:6121) in /home/prntech/public_html/wp-content/plugins/wp-force-ssl/inc/wp-force-ssl-tools.php on line 27

Warning: Cannot modify header information - headers already sent by (output started at /home/prntech/public_html/wp-includes/functions.php:6121) in /home/prntech/public_html/wp-content/plugins/wp-force-ssl/inc/wp-force-ssl-tools.php on line 31

Warning: Cannot modify header information - headers already sent by (output started at /home/prntech/public_html/wp-includes/functions.php:6121) in /home/prntech/public_html/wp-content/plugins/wp-force-ssl/inc/wp-force-ssl-tools.php on line 35

Warning: Cannot modify header information - headers already sent by (output started at /home/prntech/public_html/wp-includes/functions.php:6121) in /home/prntech/public_html/wp-content/plugins/wp-force-ssl/inc/wp-force-ssl-tools.php on line 39

Warning: Cannot modify header information - headers already sent by (output started at /home/prntech/public_html/wp-includes/functions.php:6121) in /home/prntech/public_html/wp-content/plugins/betterdocs-pro/includes/Admin/Analytics.php on line 25

Warning: Cannot modify header information - headers already sent by (output started at /home/prntech/public_html/wp-includes/functions.php:6121) in /home/prntech/public_html/wp-content/plugins/hide-my-wp/models/Rewrite.php on line 2905

Warning: Cannot modify header information - headers already sent by (output started at /home/prntech/public_html/wp-includes/functions.php:6121) in /home/prntech/public_html/wp-content/plugins/hide-my-wp/models/Rewrite.php on line 2908

Warning: Cannot modify header information - headers already sent by (output started at /home/prntech/public_html/wp-includes/functions.php:6121) in /home/prntech/public_html/wp-content/plugins/hide-my-wp/models/Rewrite.php on line 2909

Warning: Cannot modify header information - headers already sent by (output started at /home/prntech/public_html/wp-includes/functions.php:6121) in /home/prntech/public_html/wp-content/plugins/hide-my-wp/models/Rewrite.php on line 2910

Warning: Cannot modify header information - headers already sent by (output started at /home/prntech/public_html/wp-includes/functions.php:6121) in /home/prntech/public_html/wp-content/plugins/hide-my-wp/models/Rewrite.php on line 2911

Warning: Cannot modify header information - headers already sent by (output started at /home/prntech/public_html/wp-includes/functions.php:6121) in /home/prntech/public_html/wp-content/plugins/hide-my-wp/models/Rewrite.php on line 2919
انواع داده‌های ابتدایی | پراناتک

انواع داده‌های ابتدایی

در این بخش با انواع داده‌های ابتدایی موجود در سالیدیتی آشنا می‌شیم. برای جدیدترین نسخه این محتوا می‌تونین به Primitive Data Types (Code Example) در Cyfrin.io مراجعه کنین.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;

contract Primitives {
    // نوع داده‌ای boolean
    bool public boo = true;

    /*
    uint مخفف "unsigned integer" هست، یعنی اعداد صحیح غیرمنفی.
    اندازه‌های مختلفی برای این نوع داده موجوده:
        uint8   از 0 تا 2 ** 8 - 1
        uint16  از 0 تا 2 ** 16 - 1
        ...
        uint256 از 0 تا 2 ** 256 - 1
    */
    uint8 public u8 = 1;
    uint256 public u256 = 456;
    uint256 public u = 123; // uint معادل uint256 هست.

    /*
    برای نوع int، اعداد منفی هم قابل قبول هستن.
    مشابه uint، اندازه‌های مختلفی از int8 تا int256 وجود داره.

    int256 از -2 ** 255 تا 2 ** 255 - 1 رو پوشش میده.
    int128 از -2 ** 127 تا 2 ** 127 - 1 رو پوشش میده.
    */
    int8 public i8 = -1;
    int256 public i256 = 456;
    int256 public i = -123; // int معادل int256 هست.

    // حداقل و حداکثر مقدار برای int
    int256 public minInt = type(int256).min;
    int256 public maxInt = type(int256).max;

    // نوع داده‌ای آدرس
    address public addr = 0xCA35b7d915458EF540aDe6068dFe2F44E8fa733c;

    /*
    نوع داده byte در سالیدیتی نمایانگر یک توالی از بایت‌هاست.
    دو نوع برای این داده موجوده:
     - آرایه‌های بایت با اندازه ثابت
     - آرایه‌های بایت با اندازه پویا
     
    عبارت bytes به یک آرایه پویا از بایت‌ها اشاره می‌کنه
    و در واقع همون shorthand برای byte[] هست.
    */
    bytes1 a = 0xb5; //  [10110101]
    bytes1 b = 0x56; //  [01010110]

    // مقادیر پیش‌فرض
    // متغیرهایی که مقدارههی نشن، مقدار پیش‌فرض دارن:
    bool public defaultBoo; // مقدار پیش‌فرض: false
    uint256 public defaultUint; // مقدار پیش‌فرض: 0
    int256 public defaultInt; // مقدار پیش‌فرض: 0
    address public defaultAddr; // مقدار پیش‌فرض: 0x0000000000000000000000000000000000000000
}

چند تا توضیح برای درک بهتر:

  • boolean: نوع داده‌ای که فقط دو مقدار true یا false رو می‌پذیره.
  • uint و int: اعداد صحیح، با این تفاوت که uint فقط غیرمنفیه، اما int می‌تونه منفی باشه.
  • address: آدرس‌هایی که برای شناسایی حساب‌ها یا قراردادها در بلاکچین استفاده می‌شه.
  • bytes: برای کار با داده‌های باینری مثل رشته‌های بایت استفاده می‌شه.

این کد برای شروع و فهم اولیه انواع داده‌ها در سالیدیتی مثال خوبیه. امتحانش کن و ببین چطور کار می‌کنه!