All Projects → elliotchance → reflect

elliotchance / reflect

Licence: MIT license
🪞 Runtime reflection for V (vlang)

Programming Languages

V
68 projects

Projects that are alternatives of or similar to reflect

vredis
Redis client for V, written in V
Stars: ✭ 43 (+65.38%)
Mutual labels:  vlang, vlang-library, vlang-module
jni
V wrapper around the C Java Native Interface
Stars: ✭ 15 (-42.31%)
Mutual labels:  vlang, vlang-module
viup
A V wrapper for the cross-platform UI library, IUP.
Stars: ✭ 36 (+38.46%)
Mutual labels:  vlang, vlang-module
vsql
✌️ Single-file or PostgreSQL-server compatible transactional SQL database written in pure V.
Stars: ✭ 176 (+576.92%)
Mutual labels:  vlang, vlang-library
jisoni
A native JSON parser written in pure @vlang/v
Stars: ✭ 13 (-50%)
Mutual labels:  vlang, vlang-module
boundstone
High Performance / Fast Compilation / Lightweight MCBE Server
Stars: ✭ 42 (+61.54%)
Mutual labels:  vlang
kate-syntax-highlight-v
Syntax highlighting of V files for the Kate editor
Stars: ✭ 17 (-34.62%)
Mutual labels:  vlang
py2many
Transpiler of Python to many other languages
Stars: ✭ 420 (+1515.38%)
Mutual labels:  vlang
compiler-benchmark
Benchmarks compilation speeds of different combinations of languages and compilers.
Stars: ✭ 93 (+257.69%)
Mutual labels:  vlang
v-mode
🌻 An Emacs major mode for the V programming language.
Stars: ✭ 49 (+88.46%)
Mutual labels:  vlang
termtable
Simple and highly customizable library to display tables in the terminal.
Stars: ✭ 41 (+57.69%)
Mutual labels:  vlang
discord.v
Discord Bot Framework written in V
Stars: ✭ 83 (+219.23%)
Mutual labels:  vlang
v2048
A simple game of 2048, written in V.
Stars: ✭ 19 (-26.92%)
Mutual labels:  vlang
vave
🌊 A crazy simple library for reading/writing WAV files in V. Zero dependencies, 100% cross-platform.
Stars: ✭ 35 (+34.62%)
Mutual labels:  vlang
registry
Package registry for vpkg
Stars: ✭ 13 (-50%)
Mutual labels:  vlang
minesweeper-v
Minesweeper Written in Vlang
Stars: ✭ 19 (-26.92%)
Mutual labels:  vlang
vsql
A sql query builder for V
Stars: ✭ 35 (+34.62%)
Mutual labels:  vlang
vinix
Vinix is an effort to write a modern, fast, and useful operating system in the V programming language
Stars: ✭ 1,512 (+5715.38%)
Mutual labels:  vlang
vargs
Simple argument parsing library for V.
Stars: ✭ 36 (+38.46%)
Mutual labels:  vlang
vbf
A brainfuck interpreter/compiler written in V.
Stars: ✭ 17 (-34.62%)
Mutual labels:  vlang

elliotchance.reflect

Runtime reflection for V.

V does not carry runtime information about types. Although compile-time reflection is more performant it can be limiting in some cases that can't be avoided.

IMPORTANT: This project is more to demonstrate that a lot of the runtime reflection functionality one might need can be done without technically using runtime types. This implementation relies on static code being generated from compile-time reflection for known types going into the appropriate constructors.

Installation

v install elliotchance.reflect

Values

A Value can be created from any literal or simple value, for example:

import elliotchance.reflect

fn main() {
	v := reflect.value_of(1.23)

	println(v.typ)         // "f64"
	println(v.get_f64())   // 1.23
	println(v.get_int())   // V panic: value must be int but is f64
}

This becomes especially useful when dealing with arrays of mixed types:

// Only sum numbers.
fn sum(items []reflect.Value) f64 {
	mut total := 0.0

	for item in items {
		match item.typ.kind {
			.is_f64 { total += item.get_f64() }
			.is_int { total += item.get_int() }
			else { /* ignore */ }
		}
	}

	return total
}

fn main() {
	v := [
		reflect.value_of(1.23),
		reflect.value_of("hello"),
		reflect.value_of(7),
	]
	println(sum(v)) // 8.23
}

Types

All Values have a Type which can be accessed on the .typ field.

  • .kind: one of the Kind values: is_bool, is_string, is_i8, is_i16, is_int, is_i64, is_byte, is_u16, is_u32, is_u64, is_rune, is_f32, is_f64.
  • .elem: Only applies for arrays, describes the element type.
  • .str(): The string representation that matches the compile-time type in V.

Arrays

import elliotchance.reflect

fn main() {
	v := reflect.array_of([5, 6, 7])

	println(v.typ)                      // "[]int"
	println(v.typ.kind)                 // "array"
	println(v.typ.elem.kind)            // "int"
	println(v.len())                    // 3
	println(v.cap())                    // 3
	println(v.get_index(1).get_int())   // 6
	println(v.get_index(5))             // V panic: array index 5 is out of bounds (len = 3)
}

Maps

import elliotchance.reflect

fn main() {
	mut m := map[string]int{}
	m['a'] = 5
	m['b'] = 7
	m['c'] = 9

	v := reflect.map_of(m)

	println(v.typ)                                        // "map[string]int"
	println(v.typ.kind)                                   // "map"
	println(v.typ.key.kind)                               // "string"
	println(v.typ.elem.kind)                              // "int"
	println(v.len())                                      // 3
	println(v.keys())                                     // ['a', 'b', 'c']
	println(v.get_key(reflect.value_of('b')).get_int())   // 7
	println(v.get_key(reflect.value_of('d')))             // V panic: key not found: d
	println(v.get_key(reflect.value_of(123)))             // V panic: value must be string but is int
}

Structs

import elliotchance.reflect

struct Foo {
	a int
	b f64
	c string
}

fn main() {
	s := Foo{123, 4.56, 'hello'}
	v := reflect.struct_of(&s)

	println(v.typ)                    // "main.Foo"
	println(v.typ.kind)               // "struct"
	println(v.fields())               // ['a', 'b', 'c']
	println(v.field('b').typ)         // "f64"
	println(v.field('b').get_f64())   // 4.56
	println(v.field('d'))             // V panic: field not found: d

	v.field('b').set_string('hi')     // V panic: value must be f64 but is string

	v.field('c').set_string('hi')
	println(v.field('c').get_string()) // "hi"
	println(s.c)                       // "hi"
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].