Comments (2)
I have found a fix, but I am still thinking that some "autocast" feature dont work properly:
Change:
println(v.generic_new(math.abs)) // error
To:
math_abs := fn (s f64) f64 { return math.abs(s) }
println(v.generic_new(math_abs)) // fix with new function
from v.
Again: This is no priority bug, maybe it isn't an issue at all.
But here is another hint why I think that there must be casting issues; this time with an interface type.
Backround: For testing purposes with signed distance functions I wrote my own vector implementation. Then I tried to reduce the code to the absolute minimum - with interfaces . my goal is to generalize it in a simple way to more dimensions or other implementations (eg. array). But vlang sometimes is a beast ...
// vlang test, reduced for git:
// - vector operations (example) build with an interface
// - derive functions for a group of basic types
// questions:
// - how to use interface methods withou an additional cast
// - where are casts or generic types missing
module main
// scalar, basic type
type Scalar = f64
// basic function, not build as method, needed as arg
fn add_scalar(s1 Scalar, s2 Scalar) Scalar {
return s1 + s2
}
// vector, interface an derived functions
interface Vector {
libs (fn (Scalar, Scalar) Scalar) Scalar
// lib2 (Vector, fn (Scalar, Scalar) Scalar) Vector
}
// derived method 1
fn (v Vector) sum() Scalar {
return v.libs(add_scalar)
}
/* // derived method 2
fn (v1 Vector) add(v2 Vector) Vector {
return v1.lib2(v2, add_scalar)
}
*/
// vec3, representation of the interface
type Vec3 = struct {
x Scalar
y Scalar
z Scalar
}
// basic method a, safisfies the interface type
fn (v Vec3) libs(f fn (Scalar, Scalar) Scalar) Scalar {
return f(v.x, f(v.y, v.z))
}
/* // interface method b, safisfies the interface type
fn (v1 Vec3) lib2(v2 Vec3, f fn (Scalar, Scalar) Scalar) Vec3 {
//fn (v1 Vec3) lib2(v2 Vector, f fn (Scalar, Scalar) Scalar) Vector {
return Vec3{f(v1.x, v2.x), f(v1.y, v2.y), f(v1.z, v2.z)}
}
*/
fn main() {
println('### start')
// define a vector
v := Vec3{1, 2, 3}
// check if "Vec3" satifies the interface "Vector" -> ok
println(Vector(v))
// try method of type with fn as arg -> ok
println(v.libs(add_scalar))
// try methods of interface -> explicit cast needed, why?
// println(v.sum()) // should work
println(Vector(v).sum()) // but only works with a cast
// try methods of interface -> error
// - "Vec3" as args produce "unknown method Vec3.sum/add"
// - "Vector" as args produce "no field name x/y/z"
// println(v.add(v))
// the goal, concat
// println(v.add(v).add(v))
println('### end')
}
from v.
Related Issues (20)
- C codegen for struct with function member has gcc generating warnings HOT 4
- -dump-c-flags does not respect -o option HOT 7
- Add input_character_immediate() function to Stdlib
- Build error - undeclared varaible HOT 6
- v -prod for iui throws error HOT 1
- mut and & confusion HOT 4
- Compiler build error: cannot convert 'struct main__Object' to 'struct main__Object * HOT 3
- cgen error for program compiled with -autofree
- Array in closure not updated HOT 1
- Windows: coroutines should display a message that they don't work on Windows, instead of complaining about photonwrapper.so not found HOT 1
- error using `@[flag]` in `vlib/flag/flag.v`
- cgen error, when example is compiled with -autofree HOT 1
- [builder error] match + &
- Unhandled Exception 0xC0000005 HOT 1
- i tried the compile simple sdl window file from sdl example, it shows this error HOT 3
- cgen error: '__v_error' undeclared HOT 1
- anonymous struct embedding lacks checks
- How to obtain generic results using the following function? HOT 2
- string interpolation does not correctly reports pointer when used with maps
- carray_to_varray inside unsafe block warns about requiring an unsafe block
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from v.