Type/fi: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Type/fi
| Old | New | Line | |
|---|---|---|---|
| 1 | + | [[Reserved word|Varattua sanaa]] `type` käytetään: | |
| 2 | + | ||
| 3 | + | - Aloittamaan osiot käyttäjän määrittelemille tyypeille ja | |
| 4 | + | - Tunnistamaan uusi tyyppi, kun viitataan toiseen [[Data type|tietotyyppiin]] | |
| 5 | + | ||
| 6 | + | ## Oman tyypin määrittely | |
| 7 | + | ||
| 8 | + | `Type` aloittaa osion, jossa ohjelmoija voi liittää [[Identifier/fi|tunnisteita]] uusiin tietotyyppeihin, erityisesti strukturoituihin tietotyyppeihin, kuten [[Record/fi|tietueisiin (record)]]. | |
| 9 | + | ||
| 10 | + | ``` pascal | |
| 11 | + | program typeDemo(input, output, stderr); | |
| 12 | + | ||
| 13 | + | type | |
| 14 | + | atom = record | |
| 15 | + | electrons: longword; | |
| 16 | + | neutrons: longword; | |
| 17 | + | protons: longword; | |
| 18 | + | end; | |
| 19 | + | ||
| 20 | + | var | |
| 21 | + | x: atom; | |
| 22 | + | ||
| 23 | + | begin | |
| 24 | + | x.protons := 1; // H | |
| 25 | + | x.neutrons := 1; // D | |
| 26 | + | x.electrons := 1; // 0 | |
| 27 | + | end. | |
| 28 | + | ``` | |
| 29 | + | ||
| 30 | + | ## Tyyppialiakset | |
| 31 | + | ||
| 32 | + | `Type` osiossa voidaan julistaa jo olemassa olevien tai aiemmin määriteltyjen tietotyyppien aliakset. Seuraavassa esimerkissä käytetään [[Conditional compilation|ehdollista]] kokoamista (conditional compilation), jotta alias on suurin käytettävissä oleva positiivinen (etumerkitön) [[Integer/fi|kokonaisluku]], joka on `wholeNumber` (huomaa, että on jo olemassa [`system.nativeUInt`](https://www.freepascal.org/docs-html/rtl/system/nativeuint.html)). | |
| 33 | + | ||
| 34 | + | ``` pascal | |
| 35 | + | program typeAliasDemo(input, output, stderr); | |
| 36 | + | ||
| 37 | + | type | |
| 38 | + | wholeNumber = | |
| 39 | + | {$ifdef CPU64} | |
| 40 | + | qword | |
| 41 | + | {$else} | |
| 42 | + | {$ifdef CPU32} | |
| 43 | + | longword | |
| 44 | + | {$else} | |
| 45 | + | {$fatal whole number too small} | |
| 46 | + | {$endif} | |
| 47 | + | {$endif} | |
| 48 | + | ; | |
| 49 | + | ||
| 50 | + | begin | |
| 51 | + | end. | |
| 52 | + | ``` | |
| 53 | + | ||
| 54 | + | ## Tyyppiklooni | |
| 55 | + | ||
| 56 | + | `Type` osiossa on tyypin tunniste, jota edeltää sana `type`, kloonaa tyypin, tyyppi-informaationsa, mutta luo erilaisia tyyppejä. | |
| 57 | + | ||
| 58 | + | ``` pascal | |
| 59 | + | program typeCloneDemo(input, output, stderr); | |
| 60 | + | ||
| 61 | + | type | |
| 62 | + | wholeNumber = type qword; | |
| 63 | + | ||
| 64 | + | begin | |
| 65 | + | writeLn('qword: ', sysBackTraceStr(typeInfo(qword))); | |
| 66 | + | writeLn('wholeNumber: ', sysBackTraceStr(typeInfo(wholeNumber))); | |
| 67 | + | end. | |
| 68 | + | ``` | |
| 69 | + | ||
| 70 | + | Tämä voidaan tehdä esimerkiksi määrittelemään kokonaan uuden operaattoreiden joukon. Muussa tapauksessa operaattorin määritelmiä, joita tyypille kloonattiin, sovelletaan edelleen. | |
| 71 | + | ||
| 72 | + | ## Katso myös | |
| 73 | + | ||
| 74 | + | - [§ “Type Aliases” in the Reference Guide](https://www.freepascal.org/docs-html/ref/refse19.html) | |
| 75 | + | ||
| 76 | + | --- | |
| 77 | + | *From the Free Pascal wiki, page "Type/fi", as it stood on 2022-08-06 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
