Jump to content

V (programming language): Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
clarify
→‎Syntax: template:unreferenced section, remove library examples
Line 86: Line 86:


== Syntax ==
== Syntax ==

{{Unreferenced section|date=November 2023}}


=== Hello world ===
=== Hello world ===
Line 170: Line 172:
println(b)
println(b)


</syntaxhighlight>

=== Vweb ===

<syntaxhighlight lang="V">
import vweb

struct App {
vweb.Context
}

fn main() {
vweb.run(&App{}, 8080)
}
</syntaxhighlight>
or
<syntaxhighlight lang="V">
import vweb

struct App {
vweb.Context
}

fn main() {
vweb.run_at(new_app(), vweb.RunParams{
host: 'localhost'
port: 8099
family: .ip
}) or { panic(err) }
}
</syntaxhighlight>

=== ORM ===

V has a built-in ORM (object-relational mapping) which supports SQLite, MySQL and Postgres. Aims to also support MS SQL and Oracle.

V's ORM provides a number of benefits:

* One syntax for all SQL dialects
* Queries are constructed using V's syntax
* Safety (queries are sanitized to prevent SQL injection)
* Compile time checks (prevents typos which can only be caught during runtime)
* Readability and simplicity (need not manually parse results of a query or the objects from the parsed results)
<syntaxhighlight lang="V">
import pg

struct Member {
id string [default: 'gen_random_uuid()'; primary; sql_type: 'uuid']
name string
created_at string [default: 'CURRENT_TIMESTAMP'; sql_type: 'TIMESTAMP']
}

fn main() {
db := pg.connect(pg.Config{
host: 'localhost'
port: 5432
user: 'user'
password: 'password'
dbname: 'dbname'
}) or {
println(err)
return
}

defer {
db.close()
}

sql db {
create table Member
}!

new_member := Member{
name: 'John Doe'
}

sql db {
insert new_member into Member
}!

selected_member := sql db {
select from Member where name == 'John Doe' limit 1
}!

sql db {
update Member set name = 'Hitalo' where id == selected_member.id
}!
}
</syntaxhighlight>
</syntaxhighlight>



Revision as of 14:58, 4 November 2023

V
A capitalized letter V colored blue
The official V logo
ParadigmsMulti-paradigm: functional, imperative, structured, concurrent
Designed byAlexander Medvednikov[1]
First appeared20 June 2019; 5 years ago (2019-06-20)[2]
Stable release
0.4.2[3] / September 30, 2023; 11 months ago (2023-09-30)
Typing disciplinestatic, strong, infered
Memory managementoptional (automatic or manual)
Implementation languageV
Platformx86-64
OSLinux, macOS, Windows, FreeBSD, OpenBSD, NetBSD, DragonflyBSD, Solaris
LicenseMIT
Filename extensions.v, .vsh
Websitevlang.io
Influenced by
Go, Kotlin, Oberon, Python, Rust, Swift

V also known as vlang, is a statically typed compiled programming language currently in beta[4] created by Alexander Medvednikov in early 2019.[5] Its creators were inspired by the Go programming language, as well as other influences including C, Rust, and Oberon-2.[6][7][8] It is free and open-source software released under the MIT license.

The goals of V include readability and fast compile time.[9][better source needed][10][11] Some programming ambiguities are eliminated in V; for example, variable shadowing is not allowed; that is, declaring a variable with a name that is already used in a parent scope will cause a compilation error.[12][13][better source needed]

History

According to one of the developers, the new language was created as a result of frustration with existing languages being used for personal projects.[14] Originally the language was intended for personal use, but after it was mentioned publicly and gained interest, it was decided to make it public. V was initially created in order to develop a desktop messaging client known as Volt.[7] The V language was created in order to develop it, along with other software applications such as Ved (also called Vid). As the extension in use was already ".v", to not mess up the git history, it was decided to name it "V".[11] Upon public release, the compiler was written in V, and could compile itself.[15] Along with Alexander Medvednikov, the creator, its community has a large number of contributors from around the world[16][15] who have helped with continually developing and adding to the compiler, language, and modules. Key design goals behind the creation of V: easier to learn and use, higher readability, fast compilation, increased safety, efficient development, cross-platform usability, improved C interop, better error handling, modern features, and more maintainable software.[11][17][18][19]

Veasel is the official mascot of the V programming language

Features

Safety

  • Usage of bounds checking
  • Usage of Option/Result
  • Mandatory checking of errors
  • No usage of values that are undefined
  • No shadowing of variables
  • No usage of null (unless in unsafe code)
  • No usage of global variables (unless enabled via flag)
  • Variables are immutable by default
  • Structs are immutable by default
  • Function args are immutable by default
  • Sum types can be used
  • Generics can be used

Performance

  • Fast like C (human-readable C compiled by backend)[20][21][22][23]
    • Added features for safety, if desired, can be disabled or bypassed for greater performance
  • C interop[24][25][26][27]
  • Allocations kept to minimal[28]
  • Serialization with no runtime reflection[20]
  • Native binaries compiled with no dependencies[8][29]

Fast build

V is written in itself and can compile within a second.[30][15][23]

Memory management options

  • Allocations handled by an optional GC, that is the default, which can be disabled
  • Manual memory management (-gc none)
  • Autofree (-autofree), handles most objects via free call insertion
    • Remaining percentage freed by GC
  • Arena allocation with (-prealloc)

Source code translators (code transpilation)

C source code conversion: V (via module) can translate an entire C project into V code.[12][31][19]

Working translators (under various stages of development) exist for Go, JavaScript, and WASM.[32][33]

Syntax

Hello world

fn main() {
	println('hello world')
}

Structs

struct Point {
	x int
	y int
}

mut p := Point{
	x: 10
	y: 20
}
println(p.x) // Struct fields are accessed using a dot
// Alternative literal syntax for structs with 3 fields or fewer
p = Point{10, 20}
assert p.x == 10

Heap structs

Structs are allocated on the stack. To allocate a struct on the heap and get a reference to it, use the & prefix:

struct Point {
	x int
	y int
}

p := &Point{10, 10}
// References have the same syntax for accessing fields
println(p.x)

Methods

V doesn't have classes, but you can define methods to types. A method is a function with a special receiver argument. The receiver appears in its own argument list between the fn keyword and the method name. Methods must be in the same module as the receiver type.

In this example, the is_registered method has a receiver of type User named u. The convention is not to use receiver names like self or this, but preferably a short name.

struct User {
	age int
}

fn (u User) is_registered() bool {
	return u.age > 16
}

user := User{
	age: 10
}
println(user.is_registered()) // "false"
user2 := User{
	age: 20
}
println(user2.is_registered()) // "true"

Error handling

Optional types are for types which may represent none. Result types may represent an error returned from a function.

Option types are declared by prepending ? to the type name: ?Type. Result types use !: !Type.

fn do_something(s string) !string {
	if s == 'foo' {
		return 'foo'
	}
	return error('invalid string')
}

a := do_something('foo') or { 'default' } // a will be 'foo'
b := do_something('bar') or { 'default' } // b will be 'default'
c := do_something('bar') or { panic("{err}") } // exits with error 'invalid string' and a traceback

println(a)
println(b)

References

  1. ^ "Creator of V". GitHub.
  2. ^ "First public release". GitHub. 20 June 2019.
  3. ^ "Latest releases". GitHub.
  4. ^ "The V Programming Language". vlang.io. Retrieved 4 November 2023.
  5. ^ "Getting Started with V Programming". subscription.packtpub.com. Retrieved 1 November 2023.
  6. ^ Lewkowicz, Jakub (25 June 2019). "SD Times news digest: V language now open sourced". SD Times. Retrieved 25 June 2019.
  7. ^ a b James, Ben (23 July 2019). "The V Programming Language: Vain Or Virtuous?". Hackaday. Retrieved 23 July 2019.
  8. ^ a b Umoren, Samuel. "Building a Web Server using Vlang". Section. Retrieved 5 April 2021.
  9. ^ Knott, Simon (27 June 2019). "An introduction to V". Retrieved 27 June 2019.
  10. ^ Cheng, Jeremy. "VLang for Automation Scripting". Level Up Coding. Retrieved 27 December 2020.
  11. ^ a b c Jonah, Victor (25 February 2021). "What is Vlang? An introduction". LogRocket. Retrieved 25 February 2021.
  12. ^ a b "Introducing the V Tutorial!". Replit. Retrieved 4 January 2021.
  13. ^ Galuh, Rosa (8 August 2022). "A Brief Introduction to the V Language". MUO. Retrieved 8 August 2022.
  14. ^ "How To Maintain And Iterate With V - SYNCS 2023 (Sydney Computing Society at the University of Sydney)". YouTube. Retrieved 18 October 2023.
  15. ^ a b c Rao 2021.
  16. ^ "Contributors" – via OSS.
  17. ^ Independent Laboratory 2020.
  18. ^ Lyons 2022.
  19. ^ a b Nasufi, Erdet. "An introduction to V - the vlang". DebConf. Retrieved 24 July 2022.
  20. ^ a b Shóstak, Vic (15 January 2020). "The V programming language". DEV. Retrieved 8 November 2021.
  21. ^ "C is how old now? - Learning the V programming language". l-m. 10 April 2022. Retrieved 10 April 2022.
  22. ^ "V language: simple like Go, small binary like Rust". TechRacho. Retrieved 3 March 2021.
  23. ^ a b "Want to learn and master V?". Exercism. Retrieved 20 January 2023.
  24. ^ Choudhury, Ambika. "Meet V, The New Statically Typed Programming Language Inspired By Go & Rust". Analytics India Magazine (AIM). Retrieved 3 July 2019.
  25. ^ "How to install the V programming Language on Ubuntu 20.04 / Debian 10?". osradar. 24 June 2020. Retrieved 24 June 2020.
  26. ^ Grabowski, Hank. "Fighting Bloat With the V Language". nequalsonelifestyle. Retrieved 25 June 2021.
  27. ^ Walker, Jeremy. "Mechanical March". Exercism. Retrieved 1 March 2023.
  28. ^ "The V programming language is now open source". Packt Hub. 24 June 2019. Retrieved 24 June 2019.
  29. ^ Dr. Rangarajan Krishnamoorthy. "Building V Language DLL". rangakrish. Retrieved 2 April 2023.
  30. ^ Oliveira, Marcos (18 January 2022). "V , the programming language that is making a lot of success". Terminal Root. Retrieved 18 January 2022.
  31. ^ Schlothauer, Sarah. "The trendy five: Blazing hot GitHub repos in June 2019". JAXenter. Archived from the original on 17 February 2020. Retrieved 1 July 2019.
  32. ^ "Convert Go to V with go2v". Zenn. 26 January 2023. Retrieved 26 January 2023.
  33. ^ "The V WebAssembly Compiler Backend". l-m. 26 February 2023. Retrieved 26 February 2023.

Further Reading