Coder Social home page Coder Social logo

Comments (2)

felipensp avatar felipensp commented on July 23, 2024

Even this example does not works:

#include "@VMODROOT/foo.h"

struct C.Foo {
	a int
}

struct C.Bar {
	a int
}

type Baz = C.Foo | C.Bar

fn main() {
	mut a := Baz{}
	a = C.Foo{a: 1000}
	dump(a)	
}

from v.

spytheman avatar spytheman commented on July 23, 2024

the language should provide a way to use V sumtypes as an alias for C union types

V sumtypes are not a direct match to C union types.

However, V union types are a direct match to C union types:

union MyValue {
    x int
    y f32
    z string
}
v1 := MyValue{ x: 123 }
v2 := MyValue{ y: 3.14 }
v3 := MyValue{ z: 'abc' }
unsafe {
    dump(v1.x)
    dump(v2.y)
    dump(v3.z)
}

Note that accessing the fields of an union value is unsafe, since the language has no means to check what the value actually contains.

In contrast a sumtype value, has both a pointer to the actual value, and a type index field in it, and you can determine at runtime what it is.

from v.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.