Hello!/Obs. Language Elements/extent Swift for loops/Tuples init. APL in Swift

Hello. This is my first time here, so please allow me to introduce myself.. thank you.
Grab a chair. Coffee?
I've worked with many programming languages since 1979. Interesting years.
Fortran, Cobol, PL/1 C, CSP, C++, Java, Microprocessor, Assembler, Rexx, Pascal,
Smalltalk, APL, etc. Objective C, and also Swift since it became available.
OOD/OOP is my Holy Grail: the thing we found "somehow-something-is-missing-but-dunno-what?”
before it came along :o) My current creativity is RavelNotes, an ObjC/Swift hybrid iPad app
and under construction is a 3D game app with Swift/SceneKit for Apple TV.
I am 65: Now that I am retiring, app development will be the main thing for me to do
because it is fun and also because I simply cannot stop making software…
and I have lots of time for it.. without any quality compromising deadlines...

I am quite happy with Swift! It connects really good with the stuff that is already inside
my head allowing me to transfer my ideas and creativity into the virtual world without
too much hassle in-between. Swift is still new, but right from the start it has everything
I need and could wish for! There is really not that much missing, if any.
Thanks to Chris Lattner and team: you guys did a really amazing job!

Thanks for reading..
still here? OK

···

--------------------------------------------------------------------------------

1. On removing (possibly) superfluous language elements

IMHO it is nearly impossible to estimate whether or not a certain language element
will or will not useful now or in the future. It's like Lego. So don't remove bricks from the
box that you might think will not be used anymore, just because a quick scan in the
neighborhood in GitCity reveals that at the moment no others are using this kinda bricks...

So why not keep these things like i++ i--
and the C-style for loop? More important imho: do the best to maintain downward compatibility
e.g. save us from modifying 10000 source files written in 2016 in say 2021 when Swift 8.0 arrives..

--------------------------------------------------------------------------------

2: Swift "for" statement: why not add this variants:
(to get rid of reverse, strides, generators..and other indirect stuff)

for i in 12 to 0 by -2

for v:Double in x1 to -2.8 by -0.1

Swift could handle that implicitly. Most languages have these loops.
--------------------------------------------------------------------------------

3 . I beg you! Please, please, please implement implicit conversion between Floats/CGFloats/Double..
Why does one need to convert floating point vars explicitly?
Yes, one can loose precision, but we know that, don't we?
--------------------------------------------------------------------------------

4 Tuples: please take a look at this code:

static var z: UInt8 = 0 // initalize tuple with 256 UInt8 values, bytes:
    
    // Silly: why not an array instead of this.. a tuple is needed.. length must be exact 256..
    // know of no other way to create a tuple with 256 elements...
    var midiDataTuple = (z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z,
z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z,
z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z,
z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z,
z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z,
z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z)

    func midiSend(status: Int, val1: Int, val2 :Int)
    {
        var midipacket = MIDIPacket()
        
        midipacket.timeStamp = 0
        midipacket.length = 3
        midipacket.data = midiDataTuple //<-=<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        
        midipacket.data.0 = UInt8(status)
        midipacket.data.1 = UInt8(val1 )
        midipacket.data.2 = UInt8(val2 )
        
        var midipacketlist = MIDIPacketList(numPackets: 1, packet: midipacket)
        
        MIDIReceived(midiSource, &midipacketlist)
    }
    
I can't treat tuples as an array, which in this case would be handy to initialize all the tuple elements.

IfTrue: Why are tuples the only data type to use for unmanaged byte arrays?

--------------------------------------------------------------------------------

Sideways: fun & inspiration: take a look at APL.. [I'll wait here] this language uses mostly symbols for operations.
E.g. it has an excellent set of chars for Set operations. To experiment with this, replaced Xcode's source font with
an APLFont and then define operator extensions for some APL characters like e.g. Ro to get or alter the shape
of a multi dimensional array or +/ to sum a vector.

Regards
Ted

Welcome to the Swift project, Ted!

A couple of general points:

* swift-dev is primarily for discussing Swift implementation details, like progress on fixing compiler bugs, binary representations of language metadata, or optimizations that should be applied. Discussion of high-level language features should generally be on swift-evolution.

* It's usually better to post unrelated proposals to swift-evolution as separate threads. This allows everyone to keep the discussion organized and avoids mixing discussion of unrelated topics together.

On your specific suggestions...

1. On removing (possibly) superfluous language elements

Swift is pretty aggressive about removing things that are mistake-prone, rarely used, and easy to achieve in a different way. The C-style for loop and increment/decrement operators fit all three criteria.

More important imho: do the best to maintain downward compatibility
e.g. save us from modifying 10000 source files written in 2016 in say 2021 when Swift 8.0 arrives..

We are trying to stabilize the language; the hope is that Swift 4 will be a less jarring transition than Swift 3, and Swift 5 less jarring than Swift 4, and so on. But accomplishing that means removing as much cruft as possible as *early* as we can.

2: Swift "for" statement: why not add this variants:
(to get rid of reverse, strides, generators..and other indirect stuff)

for i in 12 to 0 by -2

for v:Double in x1 to -2.8 by -0.1

Swift's philosophy is that, where practical, it's actually *better* to implement features in the standard library than to create special syntax in the language. Doing it that way sometimes means you need some extra function names or punctuation characters, but it also means that when you want to do something a little bit different—maybe you need to multiply the previous value instead of adding it, or you need to stride over NSDates instead of numbers—you can build things just like the features everyone already knows how to use.

In general, we try to design features that allow us to put *more* things in the standard library, not less. For instance, swift-evolution is currently reviewing a proposal called Property Behaviors which would allow us to make `lazy` and possibly `willSet` and `didSet` into standard library features, rather than special built-in language features, and thereby allow anyone to define similar special behavior attached to a property. Building striding support into the `for` loop runs opposite to that trend, and it's unlikely to find very much support.

3 . I beg you! Please, please, please implement implicit conversion between Floats/CGFloats/Double..
Why does one need to convert floating point vars explicitly?

There are long-term plans to do something about this, but for technical reasons it's actually somewhat difficult. Basically, type inference gets slower when it has to consider more types. Allowing implicit conversions like these increases the number of types involved, which makes compiling slower. And I don't mean it adds a couple of seconds here and there; I mean it can make a compilation take minutes instead of seconds or hours instead of minutes.

This isn't impossible to overcome, and like I said, we do hope to do something about it eventually. But the time isn't right yet.

I can't treat tuples as an array, which in this case would be handy to initialize all the tuple elements.

There is an active discussion on swift-evolution right now about treating tuples as fixed-size arrays, including subscripting and declaration shorthands. The current thread is called "CollectionType on uniform tuples [forked off Contiguous Variables]".

If you'd like to discuss any of these further, please start a thread on swift-evolution; that's the appropriate venue for this kind of discussion.

···

--
Brent Royal-Gordon
Architechies

4 Tuples: please take a look at this code:

static var z: UInt8 = 0 // initalize tuple with 256 UInt8 values, bytes:

    // Silly: why not an array instead of this.. a tuple is needed..
length must be exact 256..
    // know of no other way to create a tuple with 256 elements...
    var midiDataTuple = (z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z,
z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z,
z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z,
z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z,
z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z,
z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z,
z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z,
z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z,
z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z,
z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z,
z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z,
z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z)

    func midiSend(status: Int, val1: Int, val2 :Int)
    {
        var midipacket = MIDIPacket()

        midipacket.timeStamp = 0
        midipacket.length = 3
        midipacket.data = midiDataTuple
//<-=<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

        midipacket.data.0 = UInt8(status)
        midipacket.data.1 = UInt8(val1 )
        midipacket.data.2 = UInt8(val2 )

        var midipacketlist = MIDIPacketList(numPackets: 1, packet:
midipacket)

        MIDIReceived(midiSource, &midipacketlist)
    }

I can't treat tuples as an array, which in this case would be handy to
initialize all the tuple elements.

IfTrue: Why are tuples the only data type to use for unmanaged byte
arrays?

There's currently some work on a proposal for Contiguous Variables (A.K.A.
Fixed Sized Array Type)
<https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160125/007984.html&gt;
which should help with the creation of tuples. (It can be read in a better
fashion on gmane
<http://thread.gmane.org/gmane.comp.lang.swift.evolution/4809&gt;\)

— Johan

as Brent Royal-Gordon correctly wrote to me in swift-dev,
my introduction was meant to be in swift-evolution,
not in swift-dev. By my mistake, it landed there a few days ago…

Swift-dev is for compiler magicians, which I deeply respect.
I know nearly nothing about making complies and linkers.

Anyway, here it is. after this I wil be more concise and
bring up single topics in separate emails.
Thanks
Ted

···

Hello. This is my first time here, so please allow me to introduce myself.. thank you.
Grab a chair. Coffee?
I've worked with many programming languages since 1979. Interesting years.
Fortran, Cobol, PL/1 C, CSP, C++, Java, Microprocessor, Assembler, Rexx, Pascal,
Smalltalk, APL, etc. Objective C, and also Swift since it became available.
OOD/OOP is my Holy Grail: the thing we found "somehow-something-is-missing-but-dunno-what?”
before it came along :o) My current creativity is RavelNotes, an ObjC/Swift hybrid iPad app
and under construction is a 3D game app with Swift/SceneKit for Apple TV.
I am 65: Now that I am retiring, app development will be the main thing for me to do
because it is fun and also because I simply cannot stop making software…
and I have lots of time for it.. without any quality compromising deadlines...

I am quite happy with Swift! It connects really good with the stuff that is already inside
my head allowing me to transfer my ideas and creativity into the virtual world without
too much hassle in-between. Swift is still new, but right from the start it has everything
I need and could wish for! There is really not that much missing, if any.
Thanks to Chris Lattner and team: you guys did a really amazing job!

Thanks for reading..
still here? OK
--------------------------------------------------------------------------------

1. On removing (possibly) superfluous language elements

IMHO it is nearly impossible to estimate whether or not a certain language element
will or will not useful now or in the future. It's like Lego. So don't remove bricks from the
box that you might think will not be used anymore, just because a quick scan in the
neighborhood in GitCity reveals that at the moment no others are using this kinda bricks...

So why not keep these things like i++ i--
and the C-style for loop? More important imho: do the best to maintain downward compatibility
e.g. save us from modifying 10000 source files written in 2016 in say 2021 when Swift 8.0 arrives..

--------------------------------------------------------------------------------

2: Swift "for" statement: why not add this variants:
(to get rid of reverse, strides, generators..and other indirect stuff)

for i in 12 to 0 by -2

for v:Double in x1 to -2.8 by -0.1

Swift could handle that implicitly. Most languages have these loops.
--------------------------------------------------------------------------------

3 . I beg you! Please, please, please implement implicit conversion between Floats/CGFloats/Double..
Why does one need to convert floating point vars explicitly?
Yes, one can loose precision, but we know that, don't we?
--------------------------------------------------------------------------------

4 Tuples: please take a look at this code:

static var z: UInt8 = 0 // initalize tuple with 256 UInt8 values, bytes:
    
    // Silly: why not an array instead of this.. a tuple is needed.. length must be exact 256..
    // know of no other way to create a tuple with 256 elements...
    var midiDataTuple = (z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z,
z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z,
z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z,
z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z,
z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z,
z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z)

    func midiSend(status: Int, val1: Int, val2 :Int)
    {
        var midipacket = MIDIPacket()
        
        midipacket.timeStamp = 0
        midipacket.length = 3
        midipacket.data = midiDataTuple //<-=<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        
        midipacket.data.0 = UInt8(status)
        midipacket.data.1 = UInt8(val1 )
        midipacket.data.2 = UInt8(val2 )
        
        var midipacketlist = MIDIPacketList(numPackets: 1, packet: midipacket)
        
        MIDIReceived(midiSource, &midipacketlist)
    }
    
I can't treat tuples as an array, which in this case would be handy to initialize all the tuple elements.

IfTrue: Why are tuples the only data type to use for unmanaged byte arrays?

--------------------------------------------------------------------------------

Sideways: fun & inspiration: take a look at APL.. [I'll wait here] this language uses mostly symbols for operations.
E.g. it has an excellent set of chars for Set operations. To experiment with this, replaced Xcode's source font with
an APLFont and then define operator extensions for some APL characters like e.g. Ro to get or alter the shape
of a multi dimensional array or +/ to sum a vector.

Regards
Ted

Standard library sorcerers may also hang there.

···

on Wed Feb 17 2016, "Ted F.A. van Gaalen via swift-evolution" <swift-evolution@swift.org> wrote:

Swift-dev is for compiler magicians, which I deeply respect.

--
-Dave

Hello Johan, thanks for your reply.
It would be nice if there was some kind of attribute like
var bytes: [UInt8] contiguous = ... // to declare a contiguous array.

Ok then, I'd suggest creating a new Swift type with an internal fixed length
and contiguous byte array inside. Perhaps to be implemented in a Swift library.

This swift class example here is just a Model
because to a programmer [Swift any type] arrays are undefined whether to be
be contiguous or not. No error handling is implemented in this examplel)

final class ByteArray // Note: Just a model for a lower level implementation!
{
    private var bytebuffer = [UInt8]() // Not! Must be an internal contiguous byte array.
    
    init(contents: [UInt8]) // Constructor.
    {
        for byte in contents // load bytes into contiguous!! buffer
        {
            bytebuffer.append(byte)
        }
    }
    
    func setByte(index index: Int, byte: UInt8) // set a byte in the buffer
    {
       if index.outsideRange(0..<bytebuffer.count) // should be valid index
       {
          // Aarrggh! throw: subscripting error
       }
       bytebuffer[index] = byte
    }

    // other functions could be added to clear, shift etc.
    
    func address() -> Any
    {
        return bytebuffer // use as someExternalFunc(bytes.address,…)
    }

    func length() -> Int
    {
        return bytebuffer.count
    }
}

// usage example:

let bytes = ByteArray(contents: someUInt8array )

// or -other constructor- make a byte array of 512 zero bytes:

let bytes = ByteArray(length: 512, fillWith: 0)

bytes.setByte(index: 0, byte: midiCommandByte)
bytes.setByte(index: 1, byte: midiStatByte)

// define subscriptors? so that we could just write: bytes[idx] = value

var offset = 2 // load databytes
for b in midiData
{
    bytes.setByte(index: offset, byte: b)
   offset++
}

// pass the address of the contiguous byte buffer within (this instance of) ByteArray to some external function:

someExternalFunc(&bytes.address, . . . )

Regards
Ted

···

On 14.02.2016, at 20:26, Johan Jensen <jj@johanjensen.dk> wrote:

4 Tuples: please take a look at this code:

static var z: UInt8 = 0 // initalize tuple with 256 UInt8 values, bytes:
    
    // Silly: why not an array instead of this.. a tuple is needed.. length must be exact 256..
    // know of no other way to create a tuple with 256 elements...
    var midiDataTuple = (z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z,
z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z,
z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z,
z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z,
z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z,
z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z)

    func midiSend(status: Int, val1: Int, val2 :Int)
    {
        var midipacket = MIDIPacket()
        
        midipacket.timeStamp = 0
        midipacket.length = 3
        midipacket.data = midiDataTuple //<-=<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        
        midipacket.data.0 = UInt8(status)
        midipacket.data.1 = UInt8(val1 )
        midipacket.data.2 = UInt8(val2 )
        
        var midipacketlist = MIDIPacketList(numPackets: 1, packet: midipacket)
        
        MIDIReceived(midiSource, &midipacketlist)
    }
    
I can't treat tuples as an array, which in this case would be handy to initialize all the tuple elements.

IfTrue: Why are tuples the only data type to use for unmanaged byte arrays?

There's currently some work on a proposal for Contiguous Variables (A.K.A. Fixed Sized Array Type) <https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160125/007984.html&gt; which should help with the creation of tuples. (It can be read in a better fashion on gmane <http://thread.gmane.org/gmane.comp.lang.swift.evolution/4809&gt;\)

— Johan

Unless you create an Array by bridging from NSArray, Swift's Array is
guaranteed to have contiguous storage.

Dmitri

···

On Wed, Feb 17, 2016 at 6:53 AM, Ted F.A. van Gaalen via swift-evolution <swift-evolution@swift.org> wrote:

Hello Johan, thanks for your reply.
It would be nice if there was some kind of attribute like
var bytes: [UInt8] contiguous = ... // to declare a contiguous array.

--
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr@gmail.com>*/

Unless you create an Array by bridging from NSArray, Swift's Array is
guaranteed to have contiguous storage.

And if you want to guarantee contiguous storage, `ContiguousArray` does that.

···

--
Brent Royal-Gordon
Architechies

ok, thank you, didn’t know that.

Ted

···

On 17.02.2016, at 19:34, Dmitri Gribenko <gribozavr@gmail.com> wrote:

On Wed, Feb 17, 2016 at 6:53 AM, Ted F.A. van Gaalen via > swift-evolution <swift-evolution@swift.org> wrote:

Hello Johan, thanks for your reply.
It would be nice if there was some kind of attribute like
var bytes: [UInt8] contiguous = ... // to declare a contiguous array.

Unless you create an Array by bridging from NSArray, Swift's Array is
guaranteed to have contiguous storage.

Dmitri

--
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr@gmail.com>*/