[SOLVED]why is there no mp_obj type for small int?

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

[SOLVED]why is there no mp_obj type for small int?

Post by jickster » Sun Apr 08, 2018 8:03 pm

why is there no mp_obj type for small int?
Last edited by jickster on Wed Apr 18, 2018 4:24 pm, edited 1 time in total.

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: why is there no mp_obj type for small int?

Post by deshipu » Mon Apr 09, 2018 6:50 am

Because small ints are stuffed directly into the pointers themselves.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: why is there no mp_obj type for small int?

Post by dhylands » Mon Apr 09, 2018 4:40 pm

And in particular, this is where it's documented:
https://github.com/micropython/micropyt ... .h#L51-L93

The pyboard uses MICROPY_OBJ_REPR_A

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: why is there no mp_obj type for small int?

Post by jickster » Mon Apr 16, 2018 8:28 pm

dhylands wrote:
Mon Apr 09, 2018 4:40 pm
And in particular, this is where it's documented:
https://github.com/micropython/micropyt ... .h#L51-L93

The pyboard uses MICROPY_OBJ_REPR_A
deshipu wrote:
Mon Apr 09, 2018 6:50 am
Because small ints are stuffed directly into the pointers themselves.
Then why is there a `qstr` typedef?
Now I know the typedef `qstr` is not an MP_OBJ - you have to use MP_OBJ_NEW_QSTR() to shift the bits to turn it into an MP_OBJ - but the fact remains that the typedef `qstr` exists.

I like that `qstr` exists - for reasons of type safety - so then wouldn't it similarly make sense to also have a `small_int` type?

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: why is there no mp_obj type for small int?

Post by dhylands » Mon Apr 16, 2018 8:35 pm

Having a qstr typedef serves mostly as documentation.

A qstr is really just an integer, and if you saw a function:

Code: Select all

void some_function(qstr name);
then you know that the name argument is a qstr, whereas

Code: Select all

void some_function(size_t name);
wouldn't tell you that same thing.

And a qstr isn't an mp_obj type. It has to be converted to an mp_obj type using the appropriate macro.

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: why is there no mp_obj type for small int?

Post by jickster » Tue Apr 17, 2018 4:06 am

dhylands wrote:Having a qstr typedef serves mostly as documentation.

A qstr is really just an integer, and if you saw a function:

Code: Select all

void some_function(qstr name);
then you know that the name argument is a qstr, whereas

Code: Select all

void some_function(size_t name);
wouldn't tell you that same thing.

And a qstr isn't an mp_obj type. It has to be converted to an mp_obj type using the appropriate macro.
And the same argument could be made about small int.

So why isn’t there a typedef for small int?


Sent from my iPhone using Tapatalk

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: why is there no mp_obj type for small int?

Post by stijn » Tue Apr 17, 2018 8:31 am

Isn't that mp_int_t ?

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: why is there no mp_obj type for small int?

Post by dhylands » Tue Apr 17, 2018 4:02 pm

I'm not aware of any (or many) functions which would take just a small int as an argument, except for the macros which convert to/from the mp_obj_t and the small int.

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: why is there no mp_obj type for small int?

Post by jickster » Tue Apr 17, 2018 4:08 pm

stijn wrote:
Tue Apr 17, 2018 8:31 am
Isn't that mp_int_t ?
A small int is 29-bits. mp_int_t is 32

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: why is there no mp_obj type for small int?

Post by stijn » Tue Apr 17, 2018 6:05 pm

Well, yes, but like Dave says: just like functions wanting a qstr take a qstr and not a pointer-sized type, functions wanting an int use mp_int_t. Or maybe I'm missing the point.

Post Reply