inferring array type

48 views
Skip to first unread message

SprLinx

unread,
Oct 9, 2012, 8:58:41 PM10/9/12
to nemer...@googlegroups.com
I think the compiler should be smart enough to infer that the type of the array is object when the elements are different kinds, in other words I don't want to be forced to write the ':object' hint.

def ar = array["hello" : object, DateTime(2012, 10, 10)];

What do you think?

VladD2

unread,
Oct 10, 2012, 7:00:32 AM10/10/12
to nemer...@googlegroups.com
2012/10/10 SprLinx <in...@sgsoftware.it>

I think the compiler should be smart enough to infer that the type of the array is object when the elements are different kinds, in other words I don't want to be forced to write the ':object' hint.

def ar = array["hello" : object, DateTime(2012, 10, 10)];

What do you think?

It is error detection mechanism. Nemerle could infer the object type, but specifically does not do that.

NN

unread,
Oct 10, 2012, 11:22:38 AM10/10/12
to nemer...@googlegroups.com
I would add some words to Vald's response.
Array of objects can be changed to be more specific in most cases, so if you have enumerations of objects , think about changing it to specific type unless you have no choice.

SprLinx

unread,
Oct 10, 2012, 5:11:51 PM10/10/12
to nemer...@googlegroups.com
That wasn't real code, but just an example, glad to hear that the compiler was meant to infer the type correctly and that's just bug that forces the use of the hint.

By the way, if I want to create a unit-test to get notices when the bug is fixed, do you have any specification or line guides on this topic?

Kamil Skalski

unread,
Oct 10, 2012, 10:35:48 PM10/10/12
to nemer...@googlegroups.com
2012/10/10 SprLinx <in...@sgsoftware.it>:
> That wasn't real code, but just an example, glad to hear that the compiler
> was meant to infer the type correctly and that's just bug that forces the
> use of the hint.

The bug was in the code you pasted, basically it is pretty bad idea to
create array of objects with different types, so compiler is helping
you detect this problem. Of course it also allows you to ignore it,
but then for you need to explicitly specify that you know what you are
doing, which is overall helpful for readability.

>
> By the way, if I want to create a unit-test to get notices when the bug is
> fixed, do you have any specification or line guides on this topic?

I think we have unittests checking that compiler is doing what it's
doing, since this is expected.

>
> --
> nemerle-en Google Group, try http://groups.google.com/group/nemerle-en
> for more options.



--
Kamil Skalski
http://kamil-skalski.pl

SprLinx

unread,
Oct 11, 2012, 4:36:34 AM10/11/12
to nemer...@googlegroups.com
Il giorno giovedì 11 ottobre 2012 04:35:48 UTC+2, Kamil Skalski ha scritto:. 


The bug was in the code you pasted, basically it is pretty bad idea to
create array of objects with different types, so compiler is helping
you detect this problem. Of course it also allows you to ignore it,
but then for you need to explicitly specify that you know what you are
doing, which is overall helpful for readability.


ok, so we say that whenever we want to create an array of objects initialized that way, we have to put the hint to the compiler, this behavior is not a bug in compiler but its there because that helps to avoid errors.

This is coherent with c#:

def ar = array["hello" : object, DateTime(2012, 10, 10)];
var ar = new object[] {"hello", new DateTime(2012, 10, 10)};

notice that even with c# we can't omit the object keyword.

Bogdan Mart

unread,
Jan 18, 2013, 4:15:21 AM1/18/13
to nemer...@googlegroups.com


четверг, 11 октября 2012 г., 11:36:35 UTC+3 пользователь SprLinx написал:

ok, so we say that whenever we want to create an array of objects initialized that way, we have to put the hint to the compiler, this behavior is not a bug in compiler but its there because that helps to avoid errors.

This is coherent with c#:

def ar = array["hello" : object, DateTime(2012, 10, 10)];
var ar = new object[] {"hello", new DateTime(2012, 10, 10)};

notice that even with c# we can't omit the object keyword. 

I thought following code will be more expressive

    def ar:array[object] = array["hello" , DateTime(2012, 10, 10)];

but unfortunatrly it also fails to compile with same error as

    def ar = array["hello" , DateTime(2012, 10, 10)];

NN

unread,
Jan 19, 2013, 1:48:45 PM1/19/13
to nemer...@googlegroups.com
It is simple.
The type in the left part is what you expect.

While it is not related to the typing of the right part.
For instance:
def a : A = C() : B;

It is same as you type in C#
A a = (B)(new C());

The right part and the left part has different typing meanings.

So in this case when you try type array , you have same problem.
The problem that array[...] is array initializer like you have in C# new[] { ... }.
There is no analogue syntax for C# new Type[] {...}.

For instance we could use some special character array<object>[1, ""].
On the other hand, do you find this feature common ?:)

Kamil Skalski

unread,
Jan 20, 2013, 4:34:39 AM1/20/13
to nemer...@googlegroups.com
2013/1/19 NN <nn14...@gmail.com>:
> It is simple.
> The type in the left part is what you expect.
>
> While it is not related to the typing of the right part.
> For instance:
> def a : A = C() : B;
>
> It is same as you type in C#
> A a = (B)(new C());
>
> The right part and the left part has different typing meanings.
>
> So in this case when you try type array , you have same problem.
> The problem that array[...] is array initializer like you have in C# new[] {
> ... }.
> There is no analogue syntax for C# new Type[] {...}.
>
> For instance we could use some special character array<object>[1, ""].
> On the other hand, do you find this feature common ?:)

I think it is intuitive to expect this to work. Nemerle has a pretty
powerful "reasoning" type inference, e.g.
def x = Dictionary()
x["d"] = 4;

is inferred to be string -> int only after typing the second
expression, so I would expect all
this to work fine:
def x = array["d" : object, 4];
def x = array["d", 4] : array[object];
def x : array[object] = array["d", 4];

AFAIR lack of automatic conversion to array[object] is more of a
"warning", e.g. programmer likely made a mistake, so we require him to
express explicity that it using objects was his intention.
In this context I think both
def x = array["d", 4] : array[object];
def x : array[object] = array["d", 4];

make it similarly explicit

>
>
>
>
> On Friday, January 18, 2013 12:15:21 PM UTC+3, Bogdan Mart wrote:
>>
>>
>>
>> четверг, 11 октября 2012 г., 11:36:35 UTC+3 пользователь SprLinx написал:
>>>
>>>
>>> ok, so we say that whenever we want to create an array of objects
>>> initialized that way, we have to put the hint to the compiler, this behavior
>>> is not a bug in compiler but its there because that helps to avoid errors.
>>>
>>> This is coherent with c#:
>>>
>>> def ar = array["hello" : object, DateTime(2012, 10, 10)];
>>> var ar = new object[] {"hello", new DateTime(2012, 10, 10)};
>>>
>>> notice that even with c# we can't omit the object keyword.
>>
>>
>> I thought following code will be more expressive
>>
>> def ar:array[object] = array["hello" , DateTime(2012, 10, 10)];
>>
>> but unfortunatrly it also fails to compile with same error as
>>
>> def ar = array["hello" , DateTime(2012, 10, 10)];
>
Reply all
Reply to author
Forward
0 new messages