Open hiiamboris opened 3 years ago
Just a note.. I can find many places in old Rebol code where is used construct
as a shortcut:
>> construct [a: b: c:]
== make object! [
a: none
b: none
c: none
]
Maybe you should rather request for a way how to convert a map!
into object!
like:
>> make object! m: #(a: 'a b: b: c: :c)
== make object! [
a: 'a
b: b:
c: :c
]
And just for a record... one important aspect of construct
:
>> construct [a: 1 print "hello" b: 2]
== make object! [
a: 1
b: 2
]
>> object [a: 1 print "!!! hello !!!" b: 2]
!!! hello !!!
== make object! [
a: 1
b: 2
]
The need arises not only from maps, but every time you want to construct an object from known words and values.
I would sacrifice construct [a: b: c:]
for object [a: b: c: none]
- it's easy to adapt it.
While setting every object's word to a value is a lot of overhead (esp. considering that construct-using functions are meant to be fast).
I would like to see your real life construct
use. Or ideal use.
It would be also nice to see unit tests for all situations, which may become ;-)
Btw... in the chat you was wondering, why construct/only
is not the default behavior... the reason is, that main use for construct
was meant to be loading settings from external files.. where you don't want to evaluate potentially dangerous code (like with object
), but still have common words like true
, false
or none
not to be words, but logic or none values.
Where it matters:
anonymize
could have been shorter/faster: https://gitlab.com/hiiamboris/red-mezz-warehouse/-/blob/master/setters.red#L133apply
when given an object (but that's a temporary/experimental design) https://gitlab.com/hiiamboris/red-mezz-warehouse/-/blob/master/apply.red#L23In other code I have I usually know the data and know it won't hit mentioned edge cases.
why construct/only is not the default behavior... the reason is
That's good to know, thanks. Good use case.
In both cases you are making object with just one key... I'm not sure if it's good reason for changing construct
behavior.
Btw... in Rebol3 construct
is also used to parse strings into objects... like:
>> construct "a: 1^/b: 2^/c: 3 4"
== make object! [
a: "1"
b: "2"
c: "3 4"
]
maybe not worth changin the default, but then I'd like a refinement for it, or /only
to be dumb.
Related chat on make
with objects and maps WRT words: https://gitter.im/red/help?at=60cb051cb346e9618bd74a28
Having a clear guide and behavior is important, because maps are close to the idea of passive objects, so people need to understand their similarities and differences.
Discussed around here
It is currently impossible to use
construct
to pass any value to object fields, so one has toset/any
those fields post-construction.I would like
construct
to treat it's block as key-value pairs. Because otherwise it's all up to workarounds.