Skip to content

Conversation

@gekkowrld
Copy link
Contributor

The formatting follows the following style:
andika("") // nothing
andika() // newline (historical reasons)
andika("hello") // hello
andika("mimi ni {0} niko hapa {1} kama {0}", "Nuru", "github")
// mimi ni Nuru niko hapa github kama Nuru
andika("{0}") // error
andika("", "") // error

The errors are still generic with no helpful information.
This is not an issue with this pr only but deeper so future problem.

This is potentially solves #81
and successor to #82

Actually does "Should have string manipulation instead" #82 (comment)

The formatting follows the following style:
    andika("") // nothing
    andika() // newline (historical reasons)
    andika("hello") // hello
    andika("mimi ni {0} niko hapa {1} kama {0}", "Nuru", "github")
	// mimi ni Nuru niko hapa github kama Nuru
    andika("{0}") // error
    andika("", "") // error

The errors are still generic with no helpful information.
This is not an issue with this pr only but deeper so future problem.

This is potentially solves NuruProgramming#81
and successor to NuruProgramming#82

Signed-off-by: Gekko Wrld <gekkowrld@gmail.com>
@AvicennaJr
Copy link
Member

Here are my thoughts:

  1. andika("mimi ni {0} niko hapa {1} kama {0}", "Nuru", "github") will break the syntax of how Nuru currently works:

Screenshot_20241030_102200

  1. What if someone wanted to print {0}? Since { and } are not escape characters this is not possible.
  2. There is no reason for andika("", "") to throw an error

My advice would be to add string formatting to the string object itself, I've included examples in Go and Python:

  1. Python:

Screenshot_20241030_105330

  1. Go:

Screenshot_20241030_110123

When you want to write something like '{}', previously it was an error,
now you can escape using '\{'.
The closing one may be escaped but its not mandatory. e.g
    `andika("\{}")` and `andika("\{\}")` will produce the same result.

Signed-off-by: Gekko Wrld <gekkowrld@gmail.com>
@gekkowrld
Copy link
Contributor Author

andika("mimi ni {0} niko hapa {1} kama {0}", "Nuru", "github") will break the syntax of how Nuru currently works:

True but not many programs will be affected.

What if someone wanted to print {0}? Since { and } are not escape characters this is not possible.

Fixed in comit f49a4b7

There is no reason for andika("", "") to throw an error

This is an invalid syntax, Go (the golps lsp atleast) throws a warning and go itself prints "%!(EXTRA string=)".
This is done on the Printf, but println works just fine.

My advice would be to add string formatting to the string object itself, I've included examples in Go and Python:

Python does have that only on fstring, the .format just iterates over the string object mutating as it goes by.
Look at https://github.com/python/cpython/blob/main/Lib/string.py for .format()
Look at https://docs.python.org/3/reference/lexical_analysis.html#f-strings for fstrings (they are baked into the lexical stage)
Lexing fstring starts here https://github.com/python/cpython/blob/main/Parser/lexer/lexer.c#L1063

Go doesn't have string object, because a string is just a sequence of bytes to it.
The formatting is done by Printf and Sprintf (f for formatting), all the others like Println, Print and so on
will just ignore the '%s' syntax as its not a special token in go itself but an added on by the stdlib

Look at the '%' formatting here https://github.com/golang/go/blob/master/src/fmt/print.go
and the whole module for other things like '%#'... https://github.com/golang/go/blob/master/src/fmt

@AvicennaJr
Copy link
Member

AvicennaJr commented Oct 30, 2024

Python does have that only on fstring, the .format just iterates over the string object mutating as it goes by.
Look at https://github.com/python/cpython/blob/main/Lib/string.py for .format()

So the equivalent in Nuru would be to do it on the String object, and not the built-in andika function right?

@AvicennaJr
Copy link
Member

Consider a case where someone wants to format a string and assign it to a variable, how would one achieve that with your implementation?

@gekkowrld
Copy link
Contributor Author

So the equivalent in Nuru would be to do it on the String object, and not the built-in andika function right?

Yes, so something like "Jina ni {0}\n".unda("Nuru").

This will mean that the andika() function can be used as before.

@gekkowrld
Copy link
Contributor Author

Consider a case where someone wants to format a string and assign it to a variable, how would one achieve that with your implementation?

We could have added another function for formatting e.g unda() and then by default call it for formatting and print the resulting string, like your example in golang Sprintf

@AvicennaJr
Copy link
Member

Yes, so something like "Jina ni {0}\n".unda("Nuru").

Yep, but probably a different name (don't wanna confuse people with unda).

This will mean that the andika() function can be used as before.

Exactly

We could have added another function for formatting e.g unda() and then by default call it for formatting and print the resulting string, like your example in golang Sprintf

This will add unnecessary complexity. Let's go with modifying the String object. If you agree, kindly close this PR and make a new one.

@gekkowrld
Copy link
Contributor Author

Yep, but probably a different name (don't wanna confuse people with unda).

What other name conveys that it will be formatting without confusion?

@AvicennaJr
Copy link
Member

What other name conveys that it will be formatting without confusion?

Not sure 😅

@gekkowrld
Copy link
Contributor Author

I'll just use unda until a better name is found.
Will close this one once i open another pr

gekkowrld added a commit to gekkowrld/Nuru that referenced this pull request Oct 30, 2024
Strings now can be formatted using the `"Jina langu ni {0}".badilisha('Nuru')`.

Sucessor to <NuruProgramming#93>
Instead of using a function, just do it on the string level.

Signed-off-by: Gekko Wrld <gekkowrld@gmail.com>
@gekkowrld
Copy link
Contributor Author

Look at: #94

@gekkowrld gekkowrld closed this Oct 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants