You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否定义含首字母大写字段名的BuckleScript记录?

Can I define BuckleScript records with uppercase field names for JS object conversion?

Yes, you absolutely can define BuckleScript records with uppercase field names, and using [@bs.deriving jsConverter] will let you seamlessly convert between these records and JavaScript objects that have uppercase keys—exactly what you need for your use case.

Your example code is perfectly valid:

[@bs.deriving jsConverter]
type coordinates = {
  X: int,
  Y: int
};

This will auto-generate two handy conversion functions for you:

  • coordinatesToJs: Converts your BuckleScript record into a JavaScript object with matching uppercase X and Y properties.
  • coordinatesFromJs: Parses a JavaScript object with those uppercase keys back into your record type.

Here's how you'd use them in practice:

// Create a BuckleScript record
let myCoordinates = {X: 42, Y: 100};
// Convert to a JavaScript object
let jsObj = coordinatesToJs(myCoordinates);
// jsObj now exists as { X: 42, Y: 100 } in JavaScript

// Convert the JS object back to a BuckleScript record
let parsedRecord = coordinatesFromJs(jsObj);
// parsedRecord is the record {X: 42, Y: 100}

The official BuckleScript documentation includes a dedicated section on converting between JS objects and records that covers this scenario in detail. It confirms that the jsConverter deriver respects the exact case of your record field names when generating conversion logic, so you won't run into case mismatches with your target JavaScript objects.


内容的提问来源于stack exchange,提问作者GTDev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:43:05