Complex typed variables

75 views
Skip to first unread message

roberto porcelli

unread,
Mar 4, 2025, 5:57:05 PM3/4/25
to Blockly
Good evening, is it possible to define a type with nested elements and construct variables of this type? For example:
PersonType:
  Name
  SurName
  Address.Country
  Address.City

Thanks, Roberto



roberto porcelli

unread,
Mar 6, 2025, 8:08:38 AM3/6/25
to Blockly
Basically, I don't be able to create new types in Blockly, e.g. to represent complex data structures, and then to assign this type to variables. 
Thanks for your help, Roberto

Ben Henning

unread,
Mar 6, 2025, 6:21:45 PM3/6/25
to Blockly
Hi,

Have you perhaps taken a glance at the page for defining variables? See: https://developers.google.com/blockly/guides/create-custom-blocks/variables. If I'm understanding your problem correctly, you should be able to create a custom variable that supports your PersonType example.

If this isn't quite what you're looking for, can you perhaps provide a bit more context as to how PersonType would be used--maybe an example?

Thanks,
Ben

Mark Friedman

unread,
Mar 6, 2025, 7:11:36 PM3/6/25
to blo...@googlegroups.com
Roberto,

  As Ben mentioned, the types per se are relatively straightforward to set up using the info that he linked to (especially the "Typed Variable Blocks" section).  Also note that that doc page also mentions using the @blockly/plugin-typed-variable-modal , which can help if you can potentially end up with a large number of types in your app.  I'll also point out that you probably want to understand the ideas behind "connection checks" in Blockly (see here), since those essentially define how Blocky does type checking (by allowing only certain blocks to connect to certain other blocks).

  Note, though, that the variable blocks are only one part of the problem that I think you are trying to solve. You may also be having a little trouble figuring out how to implement the blocks that construct and reference the structured data objects that you want the types to represent. You probably also want to define blocks for those things, i.e. the equivalent in, say JavaScript, to "new PersonType()", as well as references like "myPerson.Name", "myPerson.Address", "myPerson.Address.City" and the corresponding assignments.  Blockly currently doesn't define any blocks for these kind of data structures in its predefined set of blocks, so you'll have to define them yourself by creating custom blocks (see here) to correspond to things like constructors and property/field getters and setters of your new types.  If it helps, I created this gist a while back that defines a set of blocks corresponding to JavaScript Objects.  I'll warn you that that gist is 7 years old, so it's likely that it no longers totaly works with the latest version of Blockly, but it might give you the basic idea.

  Another issue that you might face here is if you want your users to be able to define their own new types, rather than just additional ones that you provide.  That, of course, will require that you create custom blocks for defining new types and their structure.  You'll probably want to detect (via Blockly events) when those blocks are used and modified and dynamically create custom blocks in the Blockly toolbox for the new type's constructor, getters, setters, etc. And you'll also want to modify your typed variable modal (i.e. the plugin mentioned above) to include the new type as the potential type for your variables.

  It's a bit complicated, but I suspect that others have done something similar via Blockly and if you give some more details about what blocks you want, the behavior you want, and the language that you want to generate code for, that others in this group may be able to help you with some specifics.

  Hope this helps!

-Mark


--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/blockly/e48b8615-beb2-49d7-b31b-908d5d9ce167n%40googlegroups.com.

roberto porcelli

unread,
Mar 10, 2025, 11:51:51 AM3/10/25
to Blockly
Hi, thanks a lot! That's right, my problem is not how to create variables of a certain type, but how to create custom types. The example you suggested to me talks about Panda type variables, and what I can't understand is how to create the Panda type through the blockly developer tools. In my case, I'm trying to create the PersonType type and then I should also create the = operator, which allows you to check if 2 personType variables are equal (if they have the same properties, such as name, surname, etc.). In the meantime I'll read carefully what you suggested, thanks for everything.
Reply all
Reply to author
Forward
0 new messages