Typstã®ã¡ã½ããå¼åºãå®å ¨ã«ç解ãã話
Typstã®ä¸é¨ã®åã¯ã¡ã½ããããã¤ãä¾ãã°arrayåã®å¤ã¯èªèº«ã®é·ãï¼è¦ç´ æ°ï¼ãåå¾ããããã®len()
ã¡ã½ããããã£ã¦ããã
#let ary = (1, 2, 3) #ary.len() //==> 3
ããã§æ³¨æãã¹ããªã®ã¯ãããã¯ary,len
ã¨ããï¼functionåã®ï¼ãã£ã¼ã«ãã«é¢æ°å¼åºã®æ¬å¼§ãä»ãããã®ã§ã¯ãªããã¨ãããã¨ã§ãããå®éãarrayåã®å¤ary
ã«ã¯ary.len
ã¨ãããã£ã¼ã«ãã¯åå¨ããªã1ã
#ary.len //--> error: cannot access fields on type array
Typstã«ããã¦ãã£ã¼ã«ãã®åç §ã¨ã¡ã½ããã®åç §ãç°ãªãæ¦å¿µã§ãããã¨ã¯dictionaryåãã¿ãã°ããã«æããã«ãªãã以ä¸ã®ä¾ãã¿ã¦ãããããã«ããã£ã¼ã«ãã¨ã¡ã½ããã®ç©ºéã¯å ¨ãå¥ã«ãªã£ã¦ããã
#let dict = (foo: calc.abs, len: 42) #dict.len //==>42 #dict.len() //==>2 #dict.foo //==>abs (functionå¤ã®è¡¨ç¤º) #dict.foo(-8) //-->error: type dictionary has no method `foo` #dict.keys //-->error: dictionary does not contain key "keys" #dict.keys() //==>("foo", "len")
ã¨ãããã¨ã¯ãTypstã§ã¯val.name(...)
ã¨ããå½¢2ã®å¼ã¯ãã¡ã½ããå¼åºãã表ããã®ã§ãããããã¨val.name
ã®å½¢ã®ããã£ã¼ã«ãåç
§ãã¨ã¯å
¨ãå¥ã®ãã®ã§ãããã¨ããããã§ãããããããã£ã¼ã«ãã®functionå¤ãå¼ã³åºãå¼ããæ¸ãããã®ãªããval.name(...)
ã¨ããâå½¢å¼âãåé¿ããå¿
è¦ããããç°¡åãªæ¹æ³ã¨ãã¦ã¯val.name
ã®é¨åã«æ¬å¼§ãä»ããã°ããã
#(dict.foo)(-8) //==>8 ('calc.abs(-8)'ã®å¤)
âã¡ã½ããå¼åºã®æå³è«âã«ã¤ãã¦ã¯Typstã®å ¬å¼ã®ããã¥ã¡ã³ãã«èª¬æãããã
ããªãã¡ãval.name(...)
ã¨ããã¡ã½ããå¼åºã¯type(val).name(val, ...)
ã¨ç価ã«ãªããå
ã®ä¾ã§dict.len()
ã¯type(dict)
ãdictionary
3ã§ããã®ã§æ¬¡ã®å¼ã¨ç価ã«ãªããããã¯å®éã«2ãè¿ãã
dictionary.len(dict)
Typstã®ã¡ã½ããå¼åºããªã«ãããããªã話
ã¨ããã§å
ã®dictionaryã®ä¾ã§calc.abs
ã¨ããé¢æ°ã使ã£ããããã¯çµè¾¼ã®calc
ã¢ã¸ã¥ã¼ã«ï¼moduleåã®å¤4ï¼ã«å±ãã¦ããé¢æ°ã§ãæ°å¤ã®çµ¶å¯¾å¤ãè¿ããã®ã§ãããé常ã¯calc.abs
ã«é¢æ°å¼åºã®æ¬å¼§ãä»ãã¦ä½¿ãã
#calc.abs(-8) //==>8
ä½ã®å¤å²ããªãã³ã¼ãã§ãã£ãã¯ãã ããããã§å
ã®èå¯ãè¸ã¾ããã¨ããçåã湧ãã¦ããããã®calc.abs(-8)
ã¨ããã®ã¯ãã¡ã½ããå¼åºããªã®ã§ããããï¼
ãã®å¼ã¯ã¾ãã«val.name(...)
ã¨ããå½¢ãªã®ã§å½¢å¼ã®ä¸ã§ã¯ã¡ã½ããå¼åºã®ã¯ãã§ããããã ãå
ã®dictionaryãarrayã®è©±ã¨æ±ºå®çã«ç°ãªãç¹ããããcalc.abs
ã¯å®éã«calc
ã®ãã£ã¼ã«ãã¨ãã¦åå¨ããã®ã§ãããããã¯calc.abs
ã®é¨åã«æ¬å¼§ãä»ãã¦ãå¼ã³åºãããã¨ãããããã
#(calc.abs)(-8) //==>8
ãããè¸ã¾ããã¨calc.abs(-8)
ã¯ãcalc.abs
ã¨ãããã£ã¼ã«ãå¤ã«é¢æ°å¼åºã®æ¬å¼§ãä»ããå¼ãã§ããã¡ã½ããå¼åºã§ãªãæ°ããã¦ããð¤
ãã£ã±ãã¡ã½ããå¼åºã§ãããããªè©±
ããããé¢æ°ãèããã
#let call-len(val) = val.len()
Typstã¯åçåã®è¨èªã§ãããããval
ã®åã¯å®è¡æã«ãã決ã¾ããªããããããã§ãval
ã«arrayã®å¤ã¨moduleã®å¤ã®ã©ã¡ããåãä»ããã®ã§ããã°ãval.len()
ã¨ãã1ã¤ã®å¼ãæç«ãã以ä¸ãcalc.abs(-8)
ãary.len()
ã¨ã¯ç°ãªãæ§æã§ãããã¨ãããã¨ã¯ããããªããã¨ã«ãªããå®éã«ç¢ºããã¦ã¿ããã
len()
ã¨ããé¢æ°ããã¤ã¢ã¸ã¥ã¼ã«ï¼#let len() = 42
#import "mod.typ" #let call-len(val) = val.len() #let ary = (1, 2, 3) #let dict = (foo: calc.abs, len: 42) #call-len(ary) //==>3 #call-len(dict) //==>2 #call-len(mod) //==>42
âæå¾
éãâã®çµæã«ãªã£ããã¨ãããã¨ã¯ããã£ã±ãcalc.abs(-8)
ã¯ã¡ã½ããå¼åºã§ããâ¦â¦ï¼ð¤ð¤
ãã£ã±ãã¡ã½ããå¼åºã§ãªããããªè©±
calc.abs(-8)
ãã¡ã½ããå¼åºã§ãããªããå
ã»ã©ç´¹ä»ããâã¡ã½ããå¼åºã®æå³è«âãæºããã¯ãã§ãããã¤ã¾ããtype(calc)
ã¯module
ã§ããããcalc.abs(-8)
ã¯ä»¥ä¸ã¨åå¤ã«ãªãã
module.abs(calc, -8)
ã¤ã¾ããmodule
ï¼typeå¤ï¼ã«ã¯module.abs
ã¨ãããã£ã¼ã«ã5ããããã®å¤ã¯ãå¼æ°ã®ã¢ã¸ã¥ã¼ã«ã®abs
ãã£ã¼ã«ãã®é¢æ°ãå¼ã³åºããã¨ããå½¹å²ããã£ãé¢æ°ãã¨ãããã¨ã«ãªãããã¡ããã¢ã¸ã¥ã¼ã«å
ã®é¢æ°åã«ã¯ä»»æã®èå¥åã使ããã®ã§ããã®çå±ã«å¾ãã¨ãmodule
ã«ã¯ããã¨ããããååã®ãã£ã¼ã«ããå®ç¾©ããã¦ãããã¨ãããªã½ãã·ã¤ãã¨ã«ãªããã¾ãè«ççã«ããããªã話ã§ã¯ãªãã®ã§ãå®éã«ç¢ºããã¦ã¿ããã
#module.abs //-->error: type self does not contain field `abs` #module.abs(calc, -8) //-->error: type self does not contain field `abs`
ã©ããããããªãªã½ãã·ã¤è©±ã¯ãªãã£ãããã§ããð ã§ãããã ã¨ãã£ã±ãcalc.abs(-8)
ã¯ã¡ã½ããå¼åºã§ã¯ãªãâ¦â¦ï¼ð¤ð¤ð¤
Typstã®ã¡ã½ããå¼åºããã§ããããã«è©±
ãªã«ãããããªããªã£ãã®ã§ãå¦çç³»ã®å®è£ ãã¿ã¦ã¿ããã
詳細ã®èª¬æã¯ï¼ã¡ã³ãã¯ãµã¤ã®ã§ðï¼çããããã¯ããval.name(...)
ã®å½¢å¼ã®å¼ã®å®è¡ã«ããã¦ã¯val
ã®åã«ãã£ã¦è§£éãå¤ãã¦ããããã§ããã
val
ã®åãsymbolãfunctionãtypeãmoduleã®ä½ããã§ããå ´å6ã¯ããã£ã¼ã«ãval.name
ã®å¤ã«å¯¾ããé¢æ°å¼åºã¨è§£éããã- ãã以å¤ã®å ´åã¯å è¿°ã®âã¡ã½ããå¼åºã®æå³è«âã«å¾ãã
ã¤ã¾ãçµè«ã¨ãã¦ã¯ï¼
val.name(...)
ã¯val.name
ã¨ã¯å ¨ãå¥ã®æ§æã§ããã- ããã
val
ã®åã«ãã£ã¦ãã¡ã½ããå¼åºãã«ãªã£ããçµå±ãval.name
ã®é¢æ°å¼åºãã«ãªã£ããããã calc.abs(-8)
ã¯å¾è ã«è©²å½ããã®ã§ãã¡ã½ããå¼åºãã§ã¯ãªãã
ã¾ã¨ã
çããããããªç´°ãããã¨ã¯ä¸åæ°ã«ããã«ãã©ãã©ãTypstãã¾ãããð
- ãããããarrayåã®å¤ã¯ãã£ã¼ã«ããä¸åæã£ã¦ããªãã↩
-
name
ã¯åä¸ã®èå¥åã«éãããval
ã®é¨åã¯ä»»æã®å¼ã§ããã↩ -
ã¤ã¾ãããããã¬ãã«ã§
dictionary
ã¨ãã¦å®ç¾©ããã¦ããtypeåã®å¤ã↩ - æå¤ãããããªããTypstã§ã¯ã¢ã¸ã¥ã¼ã«ã¯ç¬¬ä¸ç´å¤ï¼first-class valueï¼ã§ããã↩
-
val
ãtypeå¤ã§ããå ´åã®val.name()
ã¯ï¼moduleå¤ã§ããã¨ãã¨åæ§ã«ï¼ãã£ã¼ã«ãã®val.name
ã®é¢æ°å¼åºã¨åãåä½ã«ãªããä¾ãã°dictionary.len
ã¨ãããã£ã¼ã«ãã¯å®éã«åå¨ããã↩ -
æ¬å½ã¯ãã®å ´åã§ã
type(val).name
ã®ãã£ã¼ã«ããåå¨ããå ´åã¯âã¡ã½ããå¼åºã®æå³è«âãåªå ãããããã§ããããã åã®æ§è³ªãèããéãããã®4ã¤ã®åã«ã¡ã½ãããè¨å®ãããå¯è½æ§ã¯ã»ã¼ãªãããã§ããã↩