FAQ
I am fairly new Go programmer but have extensive experience in C++ and
other languages.

It appears to me that fmt.Sscan and friends return error if you give a
string such as "1.5" when variable is complex128 for example.

Seems to me that the scanning is overly restrictive, shouldn't it just set
the imaginary part to 0, store the number read into the real part and be
happy? Maybe use flags to indicate how restrictive it should be?

Similarly with a number such as "5i" - here the real part should be set to
0 and the imaginary part should be set to 5.

How about "4 + i" - here the imaginary part should be set to 1.0 and real
part to 4.0 and again it should accept it unless you want it to behave
restrictive.

Now, I would like to argue in favor of changes to fmt but that will
probably not happen any time soon - is there an easy way that I could write
my own Scan function so that I could have it do what I want without having
to copy almost all of the stuff in fmt - many of the functions where the
interesting stuff happens are private to fmt and so I don't really see how
you can change that function without actually having to re-implement quite
a lot of it and how does it work if you try to define your own
func (c * complex) Scan( ss * fmt.ScanState, verb rune) error { /* .... */ }
will go complain that such a function is already defined (in fmt) or will
it use yours over the fmt one if it see both?


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Rob Pike at May 15, 2013 at 3:36 pm
    The restriction is for simplicity of implementation and specification.
      For instance, is "4 +1i" one number or two?

    The tools exist to write custom scanners. As you suggested (but didn't
    try), you can write your own Scan method. You could also use the
    bufio.Scan interface, which provides another approach through "split
    functions".

    -rob

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Ian Lance Taylor at May 15, 2013 at 3:36 pm

    On Wed, May 15, 2013 at 6:26 AM, wrote:
    It appears to me that fmt.Sscan and friends return error if you give a
    string such as "1.5" when variable is complex128 for example.

    Seems to me that the scanning is overly restrictive, shouldn't it just set
    the imaginary part to 0, store the number read into the real part and be
    happy? Maybe use flags to indicate how restrictive it should be?
    I suggest that you open an issue for this. I agree that it seems
    reasonable to accept a float string for a complex number.
    Similarly with a number such as "5i" - here the real part should be set to 0
    and the imaginary part should be set to 5.
    Sounds reasonable.
    How about "4 + i" - here the imaginary part should be set to 1.0 and real
    part to 4.0 and again it should accept it unless you want it to behave
    restrictive.
    I don't think I agree with this, I think we should require a number
    before the 'i'.
    Now, I would like to argue in favor of changes to fmt but that will probably
    not happen any time soon - is there an easy way that I could write my own
    Scan function so that I could have it do what I want without having to copy
    almost all of the stuff in fmt - many of the functions where the interesting
    stuff happens are private to fmt and so I don't really see how you can
    change that function without actually having to re-implement quite a lot of
    it and how does it work if you try to define your own
    func (c * complex) Scan( ss * fmt.ScanState, verb rune) error { /* .... */ }
    will go complain that such a function is already defined (in fmt) or will it
    use yours over the fmt one if it see both?
    You can define your own type, define a Scan method on it, and it will
    be used by fmt.Scan.

    Ian

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedMay 15, '13 at 3:30p
activeMay 15, '13 at 3:36p
posts3
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase