Over the last few days I experimented with the definition of structs, like struct in good old C or record in Pascal. They are basically great to store data. How many times did we write boilerplate data classes in Java? :-)
Note that structs are not classes yet. They are really designed for storing data. By default, instances of a struct are mutable, but a "frozen copy" can be obtained.
In either case we provide strict semantics on equals() and hashCode() definitions.
Finally struct instances also have a nice toString() method.
So... what's the point of having structures when there are dynamic objects? Well, if all you need is storing data and you know in advance what members you need then choosing struct is a no-brainer. Indeed, it compiles to JVM classes, and as such, they are way faster than dynamic objects.
One thing structs don't do and that dynamic objects do is invoking "fake" methods if you pass a closure as a member value. But given that closures are method handles, you can still do something around the lines of:
struct Foo = { bar }
# (...)
let foo = Foo(|x| -> x + 1)
foo: bar(): invoke(1)
Looking forward to your feedback!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Basically it means generating lots of constructors. I believe that if you want to initialize just a subset of members then the following form is probably better:
let h = Human(): firstName("foo"): lastName("bar")
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Over the last few days I experimented with the definition of
structs
, likestruct
in good oldC
orrecord
inPascal
. They are basically great to store data. How many times did we write boilerplate data classes in Java? :-)You can checkout my branch at https://github.com/jponge/golo-lang/tree/wip/structs
Documentation is given at https://github.com/jponge/golo-lang/blob/wip/structs/doc/structs.asciidoc and a sample can be checked at https://github.com/jponge/golo-lang/blob/wip/structs/samples/structs.golo
Note that
structs
are not classes yet. They are really designed for storing data. By default, instances of astruct
are mutable, but a "frozen copy" can be obtained.In either case we provide strict semantics on
equals()
andhashCode()
definitions.Finally
struct
instances also have a nicetoString()
method.So... what's the point of having structures when there are dynamic objects? Well, if all you need is storing data and you know in advance what members you need then choosing
struct
is a no-brainer. Indeed, it compiles to JVM classes, and as such, they are way faster than dynamic objects.One thing
structs
don't do and that dynamic objects do is invoking "fake" methods if you pass a closure as a member value. But given that closures are method handles, you can still do something around the lines of:Looking forward to your feedback!
hi
if i write that
struct Human = {firstName, lastName, atchoum}
, then i have to initialize like that :it would be fine, if i could write (like optional parameters of a function):
... unless it is expensive
Basically it means generating lots of constructors. I believe that if you want to initialize just a subset of members then the following form is probably better:
yes, you're right
hi again,
i've played with it and it's great
Augmentations run VERY well
Last edit: Philippe Charriere 2013-07-16
Glad you like it :-)
Note that you should be able to use
invoke()
instead ofinvokeWithArguments()
.i think i've a little problem with invoke
I just added a few goodies...
A number of helper methods are now being generated:
members()
returns a tuple of the member names,values()
returns a tuple with the current member values,iterator()
provides an iterator over a structure where each element is a tuple[member, value]
,get(name)
returns the value of a member by its name,set(name, value)
updates the value of a member by its name, and returns the same structure.Structs now inherit from the abstract
gololang.GoloStruct
Java class.Do you like it?