Replies: 5 comments 1 reply
-
Now I'm creating precompile code generation script using And I'm using |
Beta Was this translation helpful? Give feedback.
-
can be used for constructors struct Name {
field1 int
field2 string
}
// special syntax for constructors
fn Name.field1(field1 int) Name {
return Name {
field1: field1
field2: 'default'
}
}
fn main() {
n := Name.field1(7)
} |
Beta Was this translation helpful? Give feedback.
-
@medvednikov This is a must feature. Any timeline estimate? |
Beta Was this translation helpful? Give feedback.
-
//THIS is not POSSIBLE
pub struct Margin {
top f64
right f64
bottom f64
left f64
}
// if the static method allowed
pub fn new() Margin {}
pub type Padding = Margin
pub type Rect = Margin
Padding::new()
Margin::new()
Rect::new()
// Alternate seems ugly
// We have to add new_margin() and use the same fn for other types or create many new variants.
// It becomes ugly when we have other constructors also like
from_vals(a, b, c, d)
from_val(a)
//etc
We can't name the module in any file because it is based on dir. I have all files inside types dir. It will like //margin and padding are module names
margin.new()
padding.new()
//Right now to achieve this, I have to create 4 folders for small files.
In the doc, it mentioned that v is very modular. I feel without a filename based module name, it's not that much modular. Solutions:
|
Beta Was this translation helpful? Give feedback.
-
V now has static methods (for all types, not just structs): struct Abc { x int }
fn Abc.new() Abc { return Abc{ x: 42 } }
dump( Abc.new() ) produces now:
|
Beta Was this translation helpful? Give feedback.
-
I want static method for struct
It might make json handling easier.
Currently, decoding json is depending on compiler, and
x.json2
module requires all fields to bemut
.(It's the same content with #7610 (comment))
Static method for struct is something like followings.
Syntax can be anything.
or maybe
Beta Was this translation helpful? Give feedback.
All reactions