Skip to content

Instantly share code, notes, and snippets.

@lovely-error
Last active January 8, 2021 13:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lovely-error/622d1f99c0a0d7fa9e2510c77622e13b to your computer and use it in GitHub Desktop.
Save lovely-error/622d1f99c0a0d7fa9e2510c77622e13b to your computer and use it in GitHub Desktop.
the way

Swift is peice of crap. Since it's conception in 2013 no important features was added, and no problems was resolved. I regret about it, since there are points about which I am happy. It is one thing actually, visual aesthetic. Here are the list of features that would make swift the best.

  • Ban struct and explicit (fixed) generic signature: Remove <...> to designate a type variables. Instead take an approach of protocol associated types.
class Box {
  associatedtype Value
  var value: Value
}
  • Allow closures to be generic
func produce (_: <T>(T) -> <K>(K) -> T) -> Int {...}

This makes the extesnion truly powerful since new generic types can be added

extension Box {
  associatedtype Deallocator: ValueDealocationProtocol where TargetValue == Self.Value
  var dealocationDelegate: Deallocator
}
  • Use regions to handle deallocation of objects with crossreference
class P { var ps: [P] = [] }
region T {
  var a = P(), b = P(ps:a) //crossreferences inside a region do not retain
  var l = [a, b]
}
print(l[1])
//after this point, no references are left to any instance in T,
//all objects are deallocated
  • Expand the semantics of a where clause by making it a predicate on term level
func swap (_ a: Box, _ b: Box) -> Box where { this in 
  a.Value == b.Value && return.Value == a.Value
} {
  let temp = a.value
  a.value = b.value
  b.value = temp
}

It may work for extensions to define a subtype extension, but decidebility will be a problem. There would be a need to enable runtime cheks, in cases where values cannot be deduced at compiletime.

extension Collection where { this in 
  this.Element is (Optional & Equatable) 
  && this.allSatisfy {$0? != nil ?? false} 
} {
  var materializedSelf: Collection where { this in this.Element == Self.Element } {
    return self.map { $0! }
  }
}
  • Enable mixin conformances and stored properties inside protocols: Lang does not allow to expand existential types. If youd want to extend any collection to some specific protocol, you'd woudnt be able to do it.
protocol A {}
extension Collection: A {} //error
extension A { var a = 0 } //error
  • Add explicit mutability signifiers: shared - for values that cant be mutated. For var and class declaration unique - for values that can be mutated, 'move-only' type ref - can be read and written box - can be only read val - can be read, can escape
class Box {}
var box: unique Box = .init(value: 10)
func mutate (_ a: ref Box) { a.reset() }
  • Bring back modules! They were removed in earlier version of the language. Why? I have no idea Some genious probably thought that they were too hard to learn. But 1ML showed that they can be awesome.

  • Add two modes of compilation: One for static file (executable) - and apply more optimizing strategies to it One for library (dyn linking and stuff) - account for retroactive extension.

  • Add the ability to inspect syntax tree Add the @astInpector(applicant:) with the requirement to type to have a function inspect ast

@astInspector
class AlwaysTerminates {
  static func inspect(ast: Swift.AST) -> InspectionResult {
    ...
  }
}
@AlwaysTerminates
func justPrintHi () { print("Hi") }
  • Add type cases
case Empty of Box where { (($0.Value as? Optional) == Optional.some) ?? false } { 
  fn fill (with value: Value) {...}
  hide func deallocate
}
var nothing: unique Empty Box with [Value=Optional with [Wrapped=String]] = .init(value: .none)
case Filled of not Empty Box {}

  • Allow extensions to indtruduce generic types
extension <T: Image> T: Matrix {} //any image is a matrix  
  • Allow named extension
extension <T: Identifieble> HashableByIdentity = T: Hashable {} // any identifieble object can be hashed
extension <T: HasName> HashableByName = T: Hashable {} // no collisions!

This feature is inspired by Familia Language and is intended to be used to achieve more use.

class Theta: Identifieble {}
var set = Set<HashableByIdentity Theta>()
  • Allow generic types to be used as a constraint
class Tau { associatedtype A, B }
func project <T: Tau> () -> T.A {} 
  • Steal Kotlin's delegation mechanism
class Bag<Item>: Collection by storage {
  var storage: Array<Item>
  substitute func map <T> (_ closure: (Item) -> T) -> Array<T> { ... }
}
  • Add new feature: Context. The analog is Scala's implicits.
@rerouter struct K<T> { 
  var storage: Array<T>
  func func map <T> (_ closure: (Item) -> T) -> Array<T> { ... }
}
var a = [1, 2, 3]
var thing: @K = a 

// it can work with named extensions
var b: @ContextuallyRerouted HashableByIdentity Type
  • Add type operators
class Zeta {
  associatedtype T
  associatedtype K
}
typealias Alpha = Zeta <T: Int> // Alpha == Zeta<T: Int, K: _>
typealias Omega = Alpha <K: String>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment