User Tools

Site Tools


reflection-field-selection

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
reflection-field-selection [2018/02/05 16:27]
barryfp
reflection-field-selection [2019/10/19 17:00]
barryfp
Line 63: Line 63:
 Where ''​x''​ is any variable, or any literal type name such as ''​char[]''​. Where ''​x''​ is any variable, or any literal type name such as ''​char[]''​.
  
-The makeup of ''​Type''​ itself is defined in ''​reflect.Type'';​ you can also create your own custom type definitions by instantiating Type and populated its fields as you like, instead of using ''​typeof''​.+The makeup of ''​Type''​ itself is defined in ''​lang.Type'';​ you can also create your own custom type definitions by instantiating Type and populated its fields as you like, instead of using ''​typeof''​.
  
 Once you have a Type instance, you can then create new data or array instances using the ''​from''​ notation, such as: Once you have a Type instance, you can then create new data or array instances using the ''​from''​ notation, such as:
Line 72: Line 72:
  
 Combined with field selection, reflection allows you to create new data types and populate all of their fields in a completely dynamic way. Combined with field selection, reflection allows you to create new data types and populate all of their fields in a completely dynamic way.
 +
 +===== Type Construction =====
 +
 +==== Data Types ====
 +
 +A data type is dynamically constructed by using a Type instance, setting its ''​class''​ field to ''​Type.DATA'',​ and populating it fields with name/type pairs describing the fields of the data type.
 +
 +The following type:
 +
 +<code d>
 +data Example {
 +   int alpha
 +   dec beta
 +   }
 +</​code>​
 +
 +Can be dynamically constructed as follows:
 +
 +<code d>
 +Type newType = new Type()
 +newType.class = Type.DATA
 +
 +newType.fields[] = new Field[2]
 +
 +newType.fields[0] = new Field(new Type(Type.INTEGER,​ 0, 8), "​alpha"​)
 +newType.fields[1] = new Field(new Type(Type.DECIMAL,​ 0, 16), "​beta"​)
 +
 +Data d = new Data() from newType
 +</​code>​
 +
 +Primitive field types (either int or dec fields) must have a size specified as part of their type; in the above example we use 64-bit sizes for both types.
 +
 +The size of the data type itself is automatically calculated by Dana when constructing an instance of the type.
 +
 +==== Primitive Types ====
 +
 +As noted above, Dana only has two primitive type classes: integers and decimal types. These two type classes can have a range of valid sizes specified in bytes, including any base 2 sizes from 1 byte to 512. For decimal types, these sizes are doubled automatically.
 +
 +All other primitive types are synonyms for these type classes, including ''​byte'',​ ''​char''​ and ''​bool''​. All three of these types are actually of type integer with a size of 1. To aid in disambiguation,​ Dana uses attributes on char/bool, for which the flags field of Type is set to Type.F_CHR or Type.F_BOOL.
 +
 +==== Array Types ====
 +
 +An array type is specified by constructing a Type with its class field set to ''​Type.ARRAY''​. For dynamically-constructed arrays, the size field of the type instance is set to zero; for statically-sized arrays (which can only be of primitive type), the size field is set to the number of cells in the array.
 +
 +In either case, the Type instance describing the array has a single field in its fields array; this field has no name, and a type set to the type stored by the array.
 +
 +The following type:
 +
 +<code d>
 +char myArray[]
 +</​code>​
 +
 +Is described as follows:
 +
 +<code d>
 +Type newType = new Type()
 +
 +newType.class = Type.ARRAY
 +
 +newType.fields = new Field(new Type(Type.INTEGER,​ Type.F_CHAR,​ 1))
 +</​code>​
 +
 +==== Object types ====
 +
 +An object type is specified by constructing a Type with its class field set to ''​Type.OBJECT''​. Object types have three fields, each of type ''​Type.DATA'',​ describing their attributes, named:
 +
 +  * .functions
 +  * .events
 +  * .state
 +
 +The .functions field has a type which contains a list of fields describing each function on the object type, including its parameters and return type.
 +
 +The .events field has a type which contains a list of the event types on the object types (i.e., events that it can emit).
 +
 +The .state field has a type which contains a list of fields describing the transfer state fields of the interface implemented by the object type.
 +
 +As with data types, Dana automatically configures the size field of the type and so the programmer does not need to set this field.
reflection-field-selection.txt ยท Last modified: 2019/10/19 17:00 by barryfp