Is this a bug? println!() can print argv[1] but now a value that may be it.

50 views
Skip to first unread message

Julian Fondren

unread,
Feb 12, 2018, 2:35:42 AM2/12/18
to ats-lang-users
The following code works as is:

    #include "share/atspre_staload.hats"
    
    implement main0(argc, argv) =
      let
        val who = (if argc > 1 then argv[1] else "world")
      in
        if argc > 1 then
          println!("Hello, ", argv[1], "!");
        print_string("Hello, ");
        print_string(who);
        print_string("!");
        print_newline();
      end

And is used like this:

    $ patscc -O2 -flto -D_GNU_SOURCE -DATS_MEMALLOC_LIBC main3.dats -o main3 -latslib
    $ ./main3
    Hello, world!
    $ ./main3 bob
    Hello, bob!
    Hello, bob!

If however the `argv[1]` in `println!` is changed to `who`,

    #include "share/atspre_staload.hats"
    
    implement main0(argc, argv) =
      let
        val who = (if argc > 1 then argv[1] else "world")
      in
        if argc > 1 then
          println!("Hello, ", who, "!");
        print_string("Hello, ");
        print_string(who);
        print_string("!");
        print_newline();
      end

then it fails to compile:

    main3.dats: 161(line=8, offs=7) -- 190(line=8, offs=36): error(3): the symbol [print] cannot be resolved due to too many matches:
    D2ITMcst(print_option) of 0
    D2ITMcst(print_list_vt) of 0

Hongwei Xi

unread,
Feb 12, 2018, 9:23:42 AM2/12/18
to ats-lan...@googlegroups.com
You need:

val who = (if argc > 1 then ... else ...): string

Otherwise, the compiler only knows that string is a subtype of the type
assigned to 'who', which is not enough to support overloading of 'print'.


--
You received this message because you are subscribed to the Google Groups "ats-lang-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ats-lang-users+unsubscribe@googlegroups.com.
To post to this group, send email to ats-lang-users@googlegroups.com.
Visit this group at https://groups.google.com/group/ats-lang-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/ats-lang-users/a763cecd-5120-4c97-b3bb-4537501adadf%40googlegroups.com.

Julian Fondren

unread,
Feb 12, 2018, 11:36:02 AM2/12/18
to ats-lang-users
That works, but this is very surprising because I tried what I thought was completely equivalent, and it does not work:

// fails with same error
val who: string = (if argc > 1 then ... else ...)

To unsubscribe from this group and stop receiving emails from it, send an email to ats-lang-user...@googlegroups.com.
To post to this group, send email to ats-lan...@googlegroups.com.

gmhwxi

unread,
Feb 12, 2018, 11:56:09 AM2/12/18
to ats-lang-users
>>val who: string = (if argc > 1 then ... else ...)

The pattern 'who: string' means that whatever matches
it should have a type X <= string.

This is something that I hope to be able to properly address in ATS3.
Reply all
Reply to author
Forward
0 new messages