Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
  • Oct 20 22:59
    @dockimbel banned @SmackMacDougal
  • Dec 03 2017 05:53
    @PeterWAWood banned @matrixbot
  • Sep 28 2016 12:19
    @PeterWAWood banned @TimeSeriesLord
  • Aug 13 2016 03:23
    @PeterWAWood banned @Vexercizer
Vladimir Vasilyev
@9214
@phillvancejr I'm not even sure if what you ask for is possible in other mainstream languages, because it goes against the very definition of variable, which has three components: an identifier, a type, and an address where its value resides.
Let's just say that in Red datatype of a "variable" is uniquely determined by value to which it refers.
phillvancejr
@phillvancejr
I don't need it be the actual none type. But in C languages I can declare a variable and its type without initializing it like int num; But if I use num: [integer!] num is of course of type datatype rather than of type integer
actually in this case it would be of type block, if i use the raw datatype like num: integer! it is of type data type. I can just use a comment if that is all we have. But It would be nice if I could just declare it so that when I type help myobject I can get the correct datatype even if it has no initial value
Vladimir Vasilyev
@9214
I see. Well, Red works very different from C, and there're no variables (in a usual sense of the term) and no variable declarations.
phillvancejr
@phillvancejr
I new it would not be like C, I was just checking if we had some alternative. Comments will have to do, thanks again for your help!
Vladimir Vasilyev
@9214
You just use one value (word!) as an indirect "pointer" to another value (none! or integer! in your case).
And if you don't explicitly "point" word! value to anything, it still points to a "default" value of type unset!.
So you can't "point" it to none! value and say "point to none but report it as an integer!".
Gregg Irwin
@greggirwin
@phillvancejr in Red/System it works like C and other statically typed languages. In Red, values are strongly typed, but words used as variable are not. That is, they can refer to any value.
phillvancejr
@phillvancejr
I knew it wouldn't work with the actual none! type. I was hoping it would be something like Go where each value is initialized to its datatype's respective none value if not explicitly set
Ah ok, awesome! thanks @greggirwin! I haven't gone through the Red/System documentation yet
Vladimir Vasilyev
@9214
@phillvancejr so, to rephrase, you want to initialize "variable" with some default value (e.g. none!) and then change its value to, say, integer!?
phillvancejr
@phillvancejr
I don't necessarily want to initialize it. when I say none, I mean some representation of no initialized value ( like NULL ) not necessarily the Red type none!. I just figured out a ghetto solution other than comments is to go ahead and assign the variable the type I want it to be so that when I probe person it at least lists the type. Then I'll just set it to the appropriate type when I create the object
Gregg Irwin
@greggirwin
It's an important distinction because variables in statically typed languages allocate memory to store their values. In Red words refer to values in a given context.
@phillvancejr adding clues that way is fine, just be aware that it doesn't provide any real protection, and you'll probably find other ways to achieve your goal as you get more familiar with Red.
phillvancejr
@phillvancejr
for sure. In this instance I don't need it to actually type check anything. I just want to annotate the code so that if I take a break from the project I can come back to it and figure out what I was doing easier. When i want to check the types when creating objects, I just use a factory function
nedzadarek
@nedzadarek
@phillvancejr you could break your code into objects or smaller functions.
  o: object [
    s: make string! 100
    i: 0 
    t: make tag! []
    ]
  append-n-times: func [str [string!] n [integer!] what [string!]][
    repeat i n [append str what]
    ]
  append-n-times "foo" 10 "*"
; "foo**********"
GiuseppeChillemi
@GiuseppeChillemi
@greggirwin and how is called the list of all words that have values in a context?
*value
Gregg Irwin
@greggirwin
words-of.
GiuseppeChillemi
@GiuseppeChillemi
@greggirwin I mean something like "vocabulary", "word list"...
Gregg Irwin
@greggirwin
"words of" then. :^)
GiuseppeChillemi
@GiuseppeChillemi
LOL :)
nedzadarek
@nedzadarek
@GiuseppeChillemi if you insist (buggy):
>> of: make op! func ['word obj] [probe word if word = 'words [words-of get obj]]
== make op! [['word obj]]
>> words of o
words
== [a]
>> ?? o
o: 
make object! [
    a: 1]
Gabriele Santilli
@giesse
@nedzadarek if you're going that way, then
>> of: make op! func ['word value] [reflect get value word]
== make op! [['word value]]
>> words of system
== [version build words platform catalog state modules codecs schemes ports locale options script standard lexer console view reactivity]
though, isn't it a bug that the second argument is also passed as a word?
Toomas Vooglaid
@toomasv

@giesse It's a known bug.

Musings on of:

of: make op! function ['word 'value] [
    either any [
        attempt [out: reflect get value word] 
        attempt [out: reflect value word] 
        attempt [
            type!: get to-word head change back tail to-string word #"!" 
            out: parse value compose/deep [
                collect any [keep (type!) | skip]
            ]
        ]
    ][out][
        cause-error 'user 'message ["Bad arguments!"]
    ]
]
;== make op! [['word 'value /local out type!]]
words of system
;== [version build words platform catalog state modules codecs schemes ports locale ;options script standard lexer console view reactivity]
body of collect
;== [
;    keep: func [v /only] [either only [append/only collected v] [append collected v] v] 
;    unless collected [collected: make block! 16] 
;    parse body rule: [
;  ...
spec of find
;== [{Returns the series where a value is found, or NONE} 
;    series [series! bitset! typeset! any-object! map! none!] 
;    value [any-type!] 
;    /part "Limit the lengt...
values of #(a 1 b 2 c: 3.0)
;== [1 2 3.0]
words of [a 1 b 2 c: 3.0]
;== [a b]
set-words of [a 1 b 2 c: 3.0]
;== [c:]
integers of [a 1 b 2 c: 3.0]
;== [1 2]
spec of [a 1 b 2 c: 3.0]
;*** User Error: "Bad arguments!"
Toomas Vooglaid
@toomasv
Improved:
of: make op! function ['word 'value] [
    either any [
        attempt [out: reflect get value word] 
        attempt [out: reflect value word] 
        attempt [
            type!: get to-word head change back tail to-string word #"!" 
            rule: compose/deep [collect any [keep (type!) | skip]] 
            out: parse value rule
        ] 
        attempt [out: parse get value rule]
    ][out][
        cause-error 'user 'message ["Bad arguments!"]
    ]
]
nedzadarek
@nedzadarek
@giesse It was just to show general syntax (word (without ') + of + value). I haven't made it safe nor implemented all X-of. Yes, something like reflect can be good - if reflect will add something, then it will be "automatically implemented". We can go deeper with other values, like @toomasv has done ( :clap: )
nedzadarek
@nedzadarek
@toomasv
ps. I don't what you are doing with the type! so, for simplicity, I added another attempt. We can add support for typeset!s as well :
  of: make op! function ['word 'value] [
        either any [
            attempt [out: reflect get value word] 
            attempt [out: reflect value word] 
            attempt [
                if all [
                    typeset? t: get word
                    block? value 
                ][
                    out: parse value [collect any [keep t | skip]]
                ]
            ]
            attempt [
                type!: get to-word head change back tail to-string word #"!" 
                rule: compose/deep [collect any [keep (type!) | skip]] 
                out: parse value rule
            ] 
            attempt [out: parse get value rule]
        ][out][
            cause-error 'user 'message ["Bad arguments!"]
        ]
    ]
; make op! [['word 'value /local out t type! rule]]
  t: make typeset! [integer! float!]
; make typeset! [integer! float!]
  t of [1 2.3 "foo" <ba> 3 4.4 <f>]
; [1 2.3 3 4.4]
Toomas Vooglaid
@toomasv
@nedzadarek Did you try it first? In former (little-bit improved) version:
>> scalars of [#"a" 1 3.0 a :g 5%]
== [#"a" 1 3.0 5%]
nedzadarek
@nedzadarek
@toomasv ah, sorry, I missed this.
Toomas Vooglaid
@toomasv
:smile:
>> t!: make typeset! [integer! float!]
== make typeset! [integer! float!]
>> ts of [1 4.0 a <b>]
== [1 4.0]
nedzadarek
@nedzadarek
@toomasv I was just going to post it. I would go with just some-type! syntax thought.
Toomas Vooglaid
@toomasv
@nedzadarek Sure, why not!
Alan Macleod
@fergus4
I have red in my env variables path (windows 10) but when I to use it from outside red folder I get "PROGRAM ERROR: Invalid encapsulated data"
Gregg Irwin
@greggirwin
Really nice @toomasv.
Toomas Vooglaid
@toomasv
I like it too :smile: Great idea from @giesse and @nedzadarek!
nedzadarek
@nedzadarek
  of: make op! func [rule source] [parse source [collect any [keep rule | skip]]]
; make op! [[rule source]]
  [integer!] of [1 2.3 4 5.6 <tag>]
; [1 4]
  [float!] of [1 2.3 4 5.6 <tag>]
; [2.3 5.6]
  [float! | integer!] of [1 2.3 4 5.6 <tag>]
; [1 2.3 4 5.6]
Gregg Irwin
@greggirwin
Clever twist.
Toomas Vooglaid
@toomasv

@nedzadarek Nice! Single type can be given w/o block too:

>> integer! of [1 3.0]
== [1]

Overall: reflective power lost, parsing power increased.

Toomas Vooglaid
@toomasv
And reflection regained:
of: make op! func [rule source] [
    either any [object? :source map? :source any-function? :source] [
        reflect :source rule
    ][
        parse source [collect any [keep rule | skip]]
    ]
]
'words of system
;== [version build words platform catalog state modules codecs schemes ports locale options script standar...
'words of #(a 1 b 2)
;== [a b]
'spec of :collect
;== [{Collect in a new block all the values passed to KEEP function from the body block} 
    body [block!...
number! of [1 a 3.0 b]
;== [1 3.0]
Toomas Vooglaid
@toomasv
digits: charset "0123456789" 
rejoin digits of "kj3245jklj34j5kl2k3j4n5234kklj"
;== "32453452345234"
Petr Krenzelok
@pekr
Would it be useful having ofoficially added to RED? But then we have those dash based -of alternatives too ....
Toomas Vooglaid
@toomasv
@pekr Sounds interesting to me, let’s see what lang-designers say ...
Vladimir Vasilyev
@9214
@phillvancejr @rcqls could you please add some specifics to this red/red#3860 bug report?
Semseddin Moldibi
@endo64
@pekr @toomasv we can add of wish to red/rep and move the design notes there to keep, and link to gitter to keep the conversation history
Vladimir Vasilyev
@9214
@endo64 not until this red/red#3344 issue is closed. Making an infix variant of reflect is one thing, but making a mish-mash of Parse, reflection and what-not is another (which I personally do not support).