In a dynamically/loosely typed lang, how good of a solution is to align the type to whatever is on the left of the operator


string + string = string
num + num = num
string + num = string
num + string = num or conversion error

  • 0
    num+string is not num or error, atleast in JS, 1+'1' gives '11'
  • 3
    @theabbie read the rant, I'm saying how good of a solution is to force the type to what is on the left of the operator
    Because in JS it gets very messy with the types
  • 0
    @frogstair That's the simplest solution, read the data from left to write and assign the types in that order.
  • 2
    I don't know how I feel about that

    I never liked the idea of using the + operator to concat strings in general, as it just gets confusing when working with numeric types.

    The fanciest solution would be embedding it some way or another, like for example in C#:
    $"The value of a is {a} square meters"
  • 1
    @LotsOfCaffeine I didn't only mean strings and numbers, I meant all other types too such as arrays booleans or whatever
  • 1
    @frogstair ... What would array + Boolean even do?
  • 0
    @LotsOfCaffeine array + bool appends a bool to an array
    bool + array = error
  • 2
    @frogstair that is wrong on so many levels
  • 0
    @frogstair https://developer.mozilla.org/en-US...

    Most of the time JS avoids converting string to things, and try to convert to string and append things.
  • 2
    Depends on what you're doing and what you're optimizing for.

    If you're going for performance in a dynamic language, you want as few implicits as possible because you'll potentially be making that check over and over again. If you assume that the left side is always static, it pseudos like this when it's evaluated:

    l + r
    Check type r is the same as typeof l
    If false
    Check type r has implicit to typeof l
    If true convert r
    Apply operation

    tl;dr The best from perf perspective is no implicits. The convenience/flexibility design would say exhaustively try for an implicit.

    An operator-sided agnostic approach would be best when pre-compiled; the compiler can could choose an optimized strategy given what is in place and has the luxury of analyzing the full graph to make that decision if I need to.
  • 2
    @SortOfTested I need to add that it depends on evaluation order.

    JavaScript is strictly left-to-right order.

    But it musn't be that way....

    Python has migrated to an PEG parser recently and their Dokumentation gives a nice background information on LL vs PEG:

  • 1
    (PEG and LL just for examples how a parser can work)
  • 0
    Just go num + (+string) for every possible string variable in a sum and '' + num for leading concatenating variables that can be a number and forget about it.
  • 0
    hmmmm, is Java loosley typed? Probably not right?

    Python is more what you mean by that maybe?

    Whenever I need a string and a number combined in something like java I use:

    ( String.valueOf(int x) + String ) = String

    I like parenthesis just because it makes it feel safer for some reason.
  • 1
    @melezorus34 the post is not about JS wtf

    @SortOfTested thank you for the only sensible answer

    @Entity-01000101 the post isn't about Java or Python, I'm creating my own implementation
  • 0
    @frogstair the coercion vent usually comes from JS devs, just sayin.
  • 0
    @melezorus34 this isn't a vent either it's my own fucking implementation
  • 0
    @frogstair yes I got it when you last said it, it was after my message.

    Can we rest now?
Add Comment