Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
The sizes of basic types are orthogonal to the layout rules, though. I agree that the decision to make Int track the machine word size came with obvious memory-use penalties, but we still made that decision, and there's no use second-guessing it every time we see those penalties in action. It's easy enough for users to use a smaller type internally when the memory use matters.

For what it's worth, we were never seriously considering making Int a fixed 32-bit type: we wanted to encourage using a single Int "currency" type, and if that was going to be fixed across platforms, it was going to have to be 64-bit. I had a major hand in shooting that down in favor of the less portable solution, for memory and interop reasons.

Adbot
ADBOT LOVES YOU

sarehu
Apr 20, 2007

(call/cc call/cc)
Man, the way you're seriously answering that comment about sizeof(Int), I think I remember your posting history better than you do.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
Heh, maybe. I'm kind of light-headed today; home sick.

Simulated
Sep 28, 2001
Lowtax giveth, and Lowtax taketh away.
College Slice

sarehu posted:

Okay on that note I'll retract calling sizeof(T) "wrong." On the other hand if you're optimizing struct packing like this, sizeof(Int) could stand to be 4 bytes to save some cache line space...

You're typically operating on an UnsafePointer which knows the size/stride anyway. Swift doesn't currently have an easy way to deal with packed values at all.

sarehu
Apr 20, 2007

(call/cc call/cc)
I went on a car trip recently and saw one of the trucks where the Swift developers live and work, like in Knight Rider.

Doctor w-rw-rw-
Jun 24, 2008
Some commentary on dynamism in Swift: http://mjtsai.com/blog/2016/05/21/dynamic-swift-2/

Echo Video
Jan 17, 2004

I'm looking forward to the swift UI framework that will get these people to stop writing these hand-wringing blog posts

Toady
Jan 12, 2009

I've never had the impression that the Swift team was opposed to dynamism. They just haven't finished designing Swift's answers to those problems that demand dynamism.

Simulated
Sep 28, 2001
Lowtax giveth, and Lowtax taketh away.
College Slice

quote:

No one doubts that this team will produce a great language for writing a compiler—the question is how it will handle apps. Apple is attempting something very ambitious: a single language that can span the spectrum. They are in uncharted territory, which is why people don’t know exactly what to expect.

Members of the Swift core team have explicitly said they have no plans to rewrite Swift in Swift right now precisely because that leads to language suited to building compilers. None of them have expressed a disregard or distain for the problems of app developers. I just think Swift is being ambitious in trying to be a systems programming language and app development language.

Almost every example of something that requires dynamism is not a real example, it's just Apple developers thinking only in terms of how the existing SDKs and Interface Builder work. The complaints about code generation are because they've never used good code generation. Visual Studio represents all visually designable elements as code and it works really well. It injects stored properties and statically typed event handlers through extensions. Interface Builder could do the same thing and instead of having to drag between IB and code it could just happen automatically.


Swift needs a good safe macro/metaprogramming system. It needs good reflection. It does not need objc_msgSend or swizzling.

Carthag Tuek
Oct 15, 2005

Tider skal komme,
tider skal henrulle,
slægt skal følge slægters gang



that reminds me, are there plans to make swift more expressive?

smackfu
Jun 7, 2004

It's interesting to see all this heated Swift debate coming up now. Wonder what triggered it?

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

Snapchat A Titty posted:

that reminds me, are there plans to make swift more expressive?

No, it's too expressive as it is and we need to cut back.

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.

smackfu posted:

It's interesting to see all this heated Swift debate coming up now. Wonder what triggered it?

Seems to go in waves. Often a :airquote: Respected Member of the Community :airquote: will write a variously dismissive thing that sets everyone off.

Toady
Jan 12, 2009

John Gruber seemed interested in linking every article he could find this week. Wil Shipley is treating it as an overreaction.

Carthag Tuek
Oct 15, 2005

Tider skal komme,
tider skal henrulle,
slægt skal følge slægters gang



rjmccall posted:

No, it's too expressive as it is and we need to cut back.

Nice :c00lbert:

WORLDS BEST BABY
Aug 26, 2006

Toady posted:

John Gruber seemed interested in linking every article he could find this week. Wil Shipley is treating it as an overreaction.

The worst bit of this is that John Gruber’s influence is so outsized compared to most of the people he's linking to, and that he's basing the merits of dynamism in Swift on who is arguing for it — namely his friends.

Not that I'm worried anyone working on a successor to UIKit is taking notes from Daring Fireball, thank christ.

Yesterday Brent Simmons posted about how Elm (a ML language primarily designed for writing UIs) handles this case, and he doesn't like it because switch statements.

Personally, I really like the way Elm (and its intellectual offshoots like Redux) handles inputs, and think it's a great fit for a static, funcitonal setting. I don't find the argument that it's too much boilerplate particularly convincing, and it's weird to read all these Objective-C programmers who are apparently very squeamish in the face of verbosity.

emoji
Jun 4, 2004
Nothing fancy but I thought the ability to do this was super nice
code:
func | <T: OptionSetType> (left: T , right: T) -> T {
    return left.union(right)
}

...
let myShortcut = MASShortcut(keyCode: UInt(keyCode), modifierFlags: (NSEventModifierFlags.CommandKeyMask | .ShiftKeyMask).rawValue)
...
myControl.sendActionOn(Int((NSEventMask.LeftMouseDraggedMask | .LeftMouseDownMask).rawValue))

ultramiraculous
Nov 12, 2003

"No..."
Grimey Drawer
Officially a Swift contributor, I guess. Probably about the easiest contribution possible, but:



:swoon:

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
Congrats!

HaB
Jan 5, 2001

What are the odds?
So. Question about how to best store something. I am converting an old program of mine from Obj-C and I never liked the way I did it there. Wondering if Swift has a better option. I am generating a table of 26 different alphabets, which I then need to be able to access individual elements of, by row/column - but the row/column indices are also alphabets.

So basically this:

code:

   A B C D E F G H I J K L M N O P
-------------------------------------------------------------
A| A B C D E F G H I J K L M N O P
B| B C D E F G H I J K L M N O P Q
C| C D E F G H I J K L M N O P Q R


etc.
 
So row C, column E gives me 'G' etc.

I have simplified for this example, but the alphabets are all scrambled, not merely shifted a character to the right. I'd like to be able to basically go:

code:
let nextLetter = table[row][column]
or something similar.

Suggestions?

emoji
Jun 4, 2004
You can define custom subscript accessors for your data structure.

https://developer.apple.com/library...4097-CH16-ID305

Simulated
Sep 28, 2001
Lowtax giveth, and Lowtax taketh away.
College Slice

emoji posted:

You can define custom subscript accessors for your data structure.

https://developer.apple.com/library...4097-CH16-ID305

I would add that subscripts can have multiple indices so this works just fine:

code:
subscript(first: Character, second: Character) -> Character { }
...
let result = s[A, B]
Then the internal storage characteristics are irrelevant to users of the type.

You're constructing a matrix so using a two-dimensional array or offsets into a one-dimensional array would work just fine.

HaB
Jan 5, 2001

What are the odds?

Ender.uNF posted:

I would add that subscripts can have multiple indices so this works just fine:

code:
subscript(first: Character, second: Character) -> Character { }
...
let result = s[A, B]
Then the internal storage characteristics are irrelevant to users of the type.

You're constructing a matrix so using a two-dimensional array or offsets into a one-dimensional array would work just fine.

Edit: figured out how to declare it. Are dictionary keys optional by default?

code:
var table = [Character : [Character : Character]]()
    init() {
        self.table["a"] = ["a" : "z", "b" : "y", "c" : "x", "d" : "w", "e" : "v"]
        self.table["b"] = ["a" : "y", "b" : "x", "c" : "w", "d" : "v", "e" : "u"]
        self.table["c"] = ["a" : "x", "b" : "w", "c" : "v", "d" : "u", "e" : "t"]
        self.table["d"] = ["a" : "w", "b" : "v", "c" : "u", "d" : "t", "e" : "s"]
        self.table["e"] = ["a" : "v", "b" : "u", "c" : "t", "d" : "s", "e" : "r"]
    }
    
    subscript(row: Character, column: Character) -> Character {
        get {
            if let rowIndex = table[row] {
                if let columIndex = table[row][column] {
                    return self.table[row][column]
                }
            }
        }
    }
The getter is complaining about "error: value of optional type '[Character : Character]?' not unwrapped; did you mean to use '!' or '?'?"

HaB fucked around with this message at 14:36 on Jun 9, 2016

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
The result of subscripting into a dictionary is optional to account for the possibility that there isn't an entry for that key.

toiletbrush
May 17, 2010
I've written an extension method on Int called '.asRgbColor' so you can do...
code:
0xfff.asRgbColor
...and get a UIColor back, but calling it like that doesn't compile, I get Expected ';' separator. Weirdly, specifying the number as a decimal, octal or binary works fine - have I done something really silly or is there a bug there?

Edit: I filed a bug, looks like the parser is confused because the 'a' at the start of asRgbColor is valid in a hex string, so it tries to parse it as a floating point hex literal! Changing the var name to toRgbColor fixes the issue...

toiletbrush fucked around with this message at 00:16 on Jun 10, 2016

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

toiletbrush posted:

I've written an extension method on Int called '.asRgbColor' so you can do...
code:
0xfff.asRgbColor
...and get a UIColor back, but calling it like that doesn't compile, I get Expected ';' separator. Weirdly, specifying the number as a decimal, octal or binary works fine - have I done something really silly or is there a bug there?

It's a bug in the lexing of hex literals. The problem is that there is a floating-point hex literal format, and what follows the dot is, in fact, more hex digits; so we're slurping up the '.' and the 'a' and then failing to backtrack correctly when we see the 's'.

There's an unsolvable ambiguity here if the entire property / method name is composed of valid hex digits (or 'p'), but since we're already Doing The Right Thing when the first character after the dot isn't a hex digit, there's really no justification for not Doing The Right Thing when any of the characters isn't a hex digit.

Choadmaster
Oct 7, 2004

I don't care how snug they fit, you're nuts!
I haven't tried it, but wouldn't conforming UIColor to IntegerLiteralConvertible and using "let thing: UIColor = 0xfff" and "self.backgroundColor = 0xfff" be more convenient anyway?

ultramiraculous
Nov 12, 2003

"No..."
Grimey Drawer

Choadmaster posted:

I haven't tried it, but wouldn't conforming UIColor to IntegerLiteralConvertible and using "let thing: UIColor = 0xfff" and "self.backgroundColor = 0xfff" be more convenient anyway?

Yeah we've got that in our codebase it's p sweet.

Malcolm XML
Aug 8, 2009

I always knew it would end like this.

rjmccall posted:

It's a bug in the lexing of hex literals. The problem is that there is a floating-point hex literal format, and what follows the dot is, in fact, more hex digits; so we're slurping up the '.' and the 'a' and then failing to backtrack correctly when we see the 's'.



:psyduck: how often does a floating point number in hex notation come up? maybe FastInvSqrt?

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
That was my first question, but apparently it is super common in numerics.

HaB
Jan 5, 2001

What are the odds?
So if I am creating an extension on say - String. What magic must I perform to keep that in its own file?

I'd like to be able to use it on ALL strings in my app, so I don't want it in just one class or whatever.

brap
Aug 23, 2004

Grimey Drawer
Could you just wrap the literal in parens or something to make the member access unambiguous?

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.

HaB posted:

So if I am creating an extension on say - String. What magic must I perform to keep that in its own file?

I'd like to be able to use it on ALL strings in my app, so I don't want it in just one class or whatever.

I don't understand. The usual access modifiers (private, internal, public) should do it for you?

HaB
Jan 5, 2001

What are the odds?

pokeyman posted:

I don't understand. The usual access modifiers (private, internal, public) should do it for you?

They don't appear to be.

Extension declaration, in own file:

code:
import Foundation

extension String {
    public mutating func rotateLeft() -> String {
        let first = self.characters.first!
        self = String(self.characters.dropFirst(1))
        self.append(first)
        return self
    }
}
but in another separated class file:

code:
var str = "derp"
derp.rotateLeft()  <--- value of type String has no member 'rotateLeft'

lord funk
Feb 16, 2004

Think you might want public extension String

dizzywhip
Dec 23, 2005

afaik, public extension just applies public to all of the members of the extension, so that wouldn't make a difference.

It looks like it should work based on what you posted, so something else must be going on. If the extension is in a different module, you need to import the module where you want to use it. Otherwise, maybe check your target inclusions and make sure that both files are included in the target you're building.

HaB
Jan 5, 2001

What are the odds?

lord funk posted:

Think you might want public extension String

this fixed it.

Thanks.

Flobbster
Feb 17, 2005

"Cadet Kirk, after the way you cheated on the Kobayashi Maru test I oughta punch you in tha face!"

HaB posted:

this fixed it.

Thanks.

That's odd—in my experience, setting the visibility of the member should have been enough. Visibility and extensions seems to be an odd spot in the language.

Don't force-unwrap your optionals though. Do this instead:

code:
        if let first = self.characters.first {
          self = String(self.characters.dropFirst(1))
          self.append(first)
        }
        return self
Now you can also rotate the empty string without it crashing!

Carthag Tuek
Oct 15, 2005

Tider skal komme,
tider skal henrulle,
slægt skal følge slægters gang



rjmccall posted:

That was my first question, but apparently it is super common in numerics.

im gonna tell on you dogg

Adbot
ADBOT LOVES YOU

Carthag Tuek
Oct 15, 2005

Tider skal komme,
tider skal henrulle,
slægt skal følge slægters gang



theres really good docs on rjmccall leaking stuff imo

  • Locked thread