• Zig, Rust जैसे curly-brace आधारित syntax पर बना है, लेकिन अधिक सरल language semantics और बेहतर syntax choices के साथ इसे और निखारता है
  • Integer literals में सभी types comptime_int से शुरू होते हैं और assignment के समय स्पष्ट रूप से convert किए जाते हैं, जबकि string literals \\ आधारित संक्षिप्त raw string notation का उपयोग करते हैं
  • .x = 1 रूप के record literals field writes को आसानी से searchable बनाते हैं, और सभी types को prefix notation में एकसमान तरीके से व्यक्त किया जाता है
  • and·or को control-flow keywords की तरह इस्तेमाल किया जाता है, और if·loop syntax में वैकल्पिक रूप से curly braces छोड़ी जा सकती हैं, जबकि formatter safety सुनिश्चित करता है
  • Namespace के बिना हर चीज़ को expression की तरह संभाला जाता है, जिससे type·value·pattern syntax एकीकृत हो जाती है, और generics·record literals·built-in functions (@import, @as आदि) को संक्षेप में इस्तेमाल किया जा सकता है

अवलोकन

  • Zig का बाहरी रूप Rust से मिलता-जुलता है, लेकिन यह अधिक सरल language structure अपनाता है
  • Syntax design में grep-friendliness, syntactic consistency, और अनावश्यक visual noise को कम करने पर ध्यान है

Integer literals

const an_integer = 92;  
assert(@TypeOf(an_integer) == comptime_int);  
  
const x: i32 = 92;  
const y = @as(i32, 92);  
  • सभी integer literals का type comptime_int होता है
  • Variable में assign करते समय type को स्पष्ट रूप से लिखना होता है या @as से convert करना होता है
  • var x = 92; रूप काम नहीं करता, इसमें explicit type चाहिए

String literals

const raw =  
    \\Roses are red  
    \\  Violets are blue,  
    \\Sugar is sweet  
    \\  And so are you.  
    \\  
;  
  • हर पंक्ति एक अलग token होती है, इसलिए indentation की समस्या नहीं होती
  • \\ को खुद escape करने की ज़रूरत नहीं होती

Record literals

const p: Point = .{  
    .x = 1,  
    .y = 2,  
};  
  • .x = 1 फ़ॉर्मेट पढ़ने/लिखने के अंतर को समझने में मददगार है
  • .{} notation block से अलग पहचानी जाती है और result type में अपने-आप convert हो जाती है

Type notation

u32        // 정수  
[3]u32     // 길이 3 배열  
?[3]u32    // null 가능 배열  
*const ?[3]u32 // 상수 포인터  
  • सभी types prefix notation में लिखे जाते हैं
  • Dereference suffix notation (ptr.*) में होता है

Identifier

const @"a name with space" = 42;  
  • Keyword conflict से बचा जा सकता है या विशेष नाम दिए जा सकते हैं

Function declaration

pub fn main() void {}  
fn add(x: i32, y: i32) i32 {  
    return x + y;  
}  
  • fn keyword और function name साथ-साथ होने से search करना आसान होता है
  • Return type notation में -> का उपयोग नहीं होता

Variable declaration

const mid = lo + @divFloor(hi - lo, 2);  
var count: u32 = 0;  
  • const और var का उपयोग होता है
  • Type notation नाम: type क्रम में होती है

Control flow: and/or

while (count > 0 and ascii.isWhitespace(buffer[count - 1])) {  
    count -= 1;  
}  
  • and, or control-flow keywords हैं
  • Bit operations के लिए &, | का उपयोग होता है

if statement

.direction = if (prng.boolean()) .ascending else .descending;  
  • Parentheses अनिवार्य हैं, curly braces वैकल्पिक
  • zig fmt सुरक्षित formatting सुनिश्चित करता है

Loops

for (0..10) |i| {  
    print("{d}\n", .{i});  
} else @panic("loop safety counter exceeded");  
  • for, while दोनों else clause को support करते हैं
  • Iterator और element names को सहज तरीके से रखा जाता है

Namespace और name resolution

const std = @import("std");  
const ArrayList = std.ArrayList;  
  • Variable shadowing की अनुमति नहीं है
  • Namespace और glob import नहीं हैं

हर चीज़ expression है

const E = enum { a, b };  
const e: if (true) E else void = .a;  
  • Type·value·pattern syntax को एकीकृत किया गया है
  • Type position में conditional expression रखी जा सकती है

Generics

fn ArrayListType(comptime T: type) type {  
    return struct {  
        fn init() void {}  
    };  
}  
  
var xs: ArrayListType(u32) = .init();  
  • Generics को function-call syntax (Type(T)) से व्यक्त किया जाता है
  • Type arguments हमेशा explicit होते हैं

Built-in functions

const foo = @import("./foo.zig");  
const num = @as(i32, 92);  
  • @ prefix से compiler-provided features को call किया जाता है
  • @import file path को स्पष्ट रूप से दिखाता है
  • Arguments अनिवार्य रूप से string literal होने चाहिए

निष्कर्ष

  • Zig का syntax छोटे-छोटे चुनावों का ऐसा समूह है जो मिलकर पढ़ने में आसान भाषा बनाता है
  • Features की संख्या घटाने से ज़रूरी syntax भी कम होती है, और syntax के बीच टकराव की संभावना भी घटती है
  • मौजूदा भाषाओं के अच्छे विचार अपनाते हुए, ज़रूरत पड़ने पर नई syntax को भी साहसपूर्वक शामिल किया जाता है

अभी कोई टिप्पणी नहीं है.

अभी कोई टिप्पणी नहीं है.