JuliaData / JuliaData/StructTypes.jl

Standard interface for programatic structure definition

Open
#7 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Julia
Stars
86
Forks
21
PR merge metrics
No merged PRs in 30d

Description

I think it would be useful to have a standard interface for programmatic structure. Here I give the basic structure, but StructTypes related features can be added to the functions.

1-time function

functions API:

struct_define(struct_name::Symbol; fields_names::Vector{Symbol}, fields_types::Vector{Union{DataTypes, Symbol, Missing})

Example

struct_define(:Foo, [:a, :b, :c], [:Int64, missing, :String])

over-time method

functions API:

struct_define(struct_data::StructConstructor) # defines a struct based on the data

mutable struct StructConstructor
   name::Symbol
   fields_names::Vector{Symbol} 
   fields_types::Vector{Union{DataType, Symbol, Missing}}
end

# a constructor for when we know the name and number of fields:
function StructConstructor(nfields::Integer)  
   fields_names = Vector{Symbol}(undef, nfields)
   fields_names = Vector{Union{DataType, Symbol, Missing}}(undef, nfields)
   return StructConstructor(:TempName, fields_names, fields_names)
end

function StructConstructor(name::Symbol, nfields::Integer)  
   fields_names = Vector{Symbol}(undef, nfields)
   fields_names = Vector{Union{DataType, Symbol, Missing}}(undef, nfields)
   return StructConstructor(name, fields_names, fields_names)
end

Example

mystruct_data = StructConstructor()
mystruct_data = mystruct_data.name = :Foo # adds name

names = [:a, :b, :c]
types = [:Int64, missing, :String]
for (name, type) in zip(types, names)
  push!(mystruct_data.fields_names, name)
  push!(mystruct_data.fields_types, type)
end
struct_define(mystruct_data)
mystruct_data = StructConstructor(:Foo, 3)
names = [:a, :b, :c]
types = [:Int64, missing, :String]
for ifield = 1:3
  mystruct_data.fields_names[ifield] = names[ifield]
  mystruct_data.fields_types[ifield] = types[ifield]
end
struct_define(mystruct_data) 

We can more feature such as

  • the default value for the fields
  • functions that check the values during the definition of an instance or updating

Related packages:

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names no files, tests, or existing entry points; begin by reviewing the current StructTypes.jl API and the related packages listed in the proposal. Done would require an agreed standard for defining structures, including the one-time and incremental forms and any StructTypes-related extensions.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
developer-experience
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.