guild icon
Toit
#How to detect the data type of an object?
Thread channel in help
kaxori
kaxori 03/25/2025 03:14 PM
The value of 'nested' should be detected as 'map'.
How can I achieve this ?
map := { "simple":"value", "nested": { "A":1, "B":2 } }
kaxoriOPkaxori
The value of 'nested' should be detected as 'map'. How can I achieve this ? ``` map := { "simple":"value", "nested": { ...
floitsch
floitsch 03/25/2025 03:17 PM
You can do an is Map check
floitsch
floitsch 03/25/2025 03:19 PM
Or do you mean statically?
Statically you need to use as or assign it to a typed local:
nested/Map := map["nested"]
or
(map["nested"] as Map)...
kaxori
kaxori 03/25/2025 03:21 PM
so simple :😳:
Thank you
(die Verkalkung beginnt mit der Geburt)
😁1
kaxori
kaxori 03/25/2025 03:40 PM
I searched for 'is' in Toit docs, but couldn't find anything. Neither part of language, nor part of object. How is it implemented ?
floitsch
floitsch 03/25/2025 03:49 PM
Each object (except for small integers) have a class-id in their header.
floitsch
floitsch 03/25/2025 03:49 PM
If you are testing for an object, a small range-check can detect whether an object is in a class-hierarchy.
floitsch
floitsch 03/25/2025 03:50 PM
If you are testing for an interface, then we add is-interface methods to these classes.
floitsch
floitsch 03/25/2025 03:50 PM
These methods don't actually have any code, since we just check for their existence.
floitsch
floitsch 03/25/2025 03:50 PM
Integers are implemented differently: they are not actual objects, but their value shifted.
floitsch
floitsch 03/25/2025 03:51 PM
So the value 3 is simply 3 << 1 == 6.
floitsch
floitsch 03/25/2025 03:51 PM
To differentiate them from pointers to objects, pointers are offset by 1.
floitsch
floitsch 03/25/2025 03:51 PM
So all object pointers in the Toit VM have the last bit set to 1.
floitsch
floitsch 03/25/2025 03:52 PM
So is int starts by looking whether the "pointer" has 0 in the least-significant bit. If not, it also checks for the Integer class-id, which is used when values are bigger than 31 bits (since they wouldn't fit into words anymore).
floitsch
floitsch 03/25/2025 03:53 PM
You can see the class-ids, by running toit tools snapshot classes my-snapshot.
kaxori
kaxori 03/25/2025 03:53 PM
and is ?
floitsch
floitsch 03/25/2025 03:55 PM
is should be in there.
floitsch
floitsch 03/25/2025 03:55 PM
Where does the table come from?
kaxori
kaxori 03/25/2025 03:55 PM
floitsch
floitsch 03/25/2025 03:55 PM
(btw: there is also is not, as in if x is not Map)
floitsch
floitsch 03/25/2025 03:55 PM
I will update the table.
kaxori
kaxori 03/25/2025 03:56 PM
:👍🏻:
floitsch
floitsch 03/25/2025 03:56 PM
we probably missed it, because we have is and is not as operators in our parser.
floitsch
floitsch 03/25/2025 03:56 PM
Program your microcontrollers in a fast and robust high-level language. - toitlang/toit
kaxori
kaxori 03/25/2025 04:02 PM
... there are a lot of muggles outside hogwarts
kaxori
kaxori 03/25/2025 04:22 PM
If I try to create a composed map :
m-a := { "A":1 } m-b := { "B":2 } m-composed := { m-a, m-b }
this way does not work. Is there a way to combine maps ?
(edited)
floitsch
floitsch 03/25/2025 04:23 PM
The { m-a, m-b } is a set.
floitsch
floitsch 03/25/2025 04:24 PM
You can either make a list: [m-a, m-b], or a map: {"m-a": m-a, "m-b", m-b}.
Toit doesn't have JavaScript's automatic "use the variable name as key".
kaxori
kaxori 03/25/2025 04:29 PM
m-composed := { m-a.first: m-a, m-b.first: m-b}(edited)
kaxori
kaxori 03/25/2025 04:31 PM
Is that the correct way to create it using the data of the 2 predefined maps ? It seems to work, but is ugly.
kaxori
kaxori 03/25/2025 05:06 PM
m-composed := {:} m-a.do: | k v | m-composed[k] = v m-b.do: | k v | m-composed[k] = v
*... found another way *
floitsch
floitsch 03/25/2025 05:07 PM
Yes. the second approach is nicer.
kaxori
kaxori 03/25/2025 05:08 PM
I am preparing the technique to deal with maps. Goal is setup for home assistant device discovery ...
👍1
floitschfloitsch
I will update the table.
floitsch
floitsch 03/26/2025 01:03 PM
Updated table is live.
👍🏻1
kaxori
kaxori 03/26/2025 01:38 PM
for those interested in maps:
map-elements := { "A":1, "B":2, "C":3 } map := {"key":"value"} map["k"] = "v" // to add elements to map map-elements.do (: |k v| map[k] = v) map["nested"] = {:} // create "nested" sub-map (map["nested"])["k"] = "v" // add one k-v-element to sub-map // / to add elements to sub-map map-elements.do (: |k v| (map["nested"])[k] = v) sub-map := map["nested"] // easier to read sub-map["key"] = "value" ...
- 128 nesting levels supported !
35 messages in total