red / REP

Red Enhancement Process
BSD 3-Clause "New" or "Revised" License
10 stars 4 forks source link

WISH: dumber `construct` #100

Open hiiamboris opened 3 years ago

hiiamboris commented 3 years ago

Discussed around here

>> m: #(a: 'a b: b: c: :c)
== #(
    a: 'a
    b: b:
    c: :c
)
>> construct to block! m
== make object! [
    a: 'a
    b: :c
    c: :c
]

It is currently impossible to use construct to pass any value to object fields, so one has to set/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.

Oldes commented 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
]
Oldes commented 3 years ago

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
]
Oldes commented 3 years ago

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
]
hiiamboris commented 3 years ago

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).

Oldes commented 3 years ago

I would like to see your real life construct use. Or ideal use.

Oldes commented 3 years ago

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.

hiiamboris commented 3 years ago

Where it matters:

In other code I have I usually know the data and know it won't hit mentioned edge cases.

hiiamboris commented 3 years ago

why construct/only is not the default behavior... the reason is

That's good to know, thanks. Good use case.

Oldes commented 3 years ago

In both cases you are making object with just one key... I'm not sure if it's good reason for changing construct behavior.

Oldes commented 3 years ago

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"
]
hiiamboris commented 3 years ago

maybe not worth changin the default, but then I'd like a refinement for it, or /only to be dumb.

greggirwin commented 3 years ago

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.